Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions ReflectorNet.Tests/src/SchemaTests/TestSchemaTypeId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void GetSchemaTypeId_SimpleArray_ShouldAppendArray()
var result = type.GetSchemaTypeId();

// Assert
Assert.Equal($"System.Int32{TypeUtils.ArraySuffix}", result);
Assert.Equal("System.Int32%5B%5D", result);
_output.WriteLine($"int[] -> {result}");
Comment on lines 20 to 22
}

Expand All @@ -32,7 +32,7 @@ public void GetSchemaTypeId_NestedArray_ShouldAppendMultipleArrays()
var result = type.GetSchemaTypeId();

// Assert
Assert.Equal($"System.Int32{TypeUtils.ArraySuffix}{TypeUtils.ArraySuffix}", result);
Assert.Equal("System.Int32%5B%5D%5B%5D", result);
_output.WriteLine($"int[][] -> {result}");
}

Expand All @@ -46,7 +46,7 @@ public void GetSchemaTypeId_TripleNestedArray_ShouldAppendThreeArrays()
var result = type.GetSchemaTypeId();

// Assert
Assert.Equal($"System.Int32{TypeUtils.ArraySuffix}{TypeUtils.ArraySuffix}{TypeUtils.ArraySuffix}", result);
Assert.Equal("System.Int32%5B%5D%5B%5D%5B%5D", result);
_output.WriteLine($"int[][][] -> {result}");
}

Expand All @@ -60,7 +60,7 @@ public void GetSchemaTypeId_StringArray_ShouldWorkForAnyType()
var result = type.GetSchemaTypeId();

// Assert
Assert.Equal($"System.String{TypeUtils.ArraySuffix}", result);
Assert.Equal("System.String%5B%5D", result);
_output.WriteLine($"string[] -> {result}");
}

Expand Down Expand Up @@ -102,7 +102,7 @@ public void GetSchemaTypeId_NullableArrayType_ShouldHandleUnderlyingArrayType()
var result = type.GetSchemaTypeId();

// Assert
Assert.Equal($"System.Int32{TypeUtils.ArraySuffix}", result);
Assert.Equal("System.Int32%5B%5D", result);
_output.WriteLine($"int?[] -> {result}");
}

Expand All @@ -116,7 +116,7 @@ public void GetSchemaTypeId_IEnumerableOfInt_ShouldReturnGenericFormat()
var result = type.GetSchemaTypeId();

// Assert
Assert.Equal("System.Collections.Generic.IEnumerable<System.Int32>", result);
Assert.Equal("System.Collections.Generic.IEnumerable%3CSystem.Int32%3E", result);
_output.WriteLine($"IEnumerable<int> -> {result}");
}

Expand All @@ -130,7 +130,7 @@ public void GetSchemaTypeId_ICollectionOfString_ShouldReturnGenericFormat()
var result = type.GetSchemaTypeId();

// Assert
Assert.Equal("System.Collections.Generic.ICollection<System.String>", result);
Assert.Equal("System.Collections.Generic.ICollection%3CSystem.String%3E", result);
_output.WriteLine($"ICollection<string> -> {result}");
}

Expand All @@ -144,7 +144,7 @@ public void GetSchemaTypeId_IListOfInt_ShouldReturnGenericFormat()
var result = type.GetSchemaTypeId();

// Assert
Assert.Equal("System.Collections.Generic.IList<System.Int32>", result);
Assert.Equal("System.Collections.Generic.IList%3CSystem.Int32%3E", result);
_output.WriteLine($"IList<int> -> {result}");
}

Expand Down Expand Up @@ -172,7 +172,7 @@ public void GetSchemaTypeId_ArrayOfCustomClass_ShouldAppendArray()
var result = type.GetSchemaTypeId();

// Assert
Assert.Equal($"com.IvanMurzak.ReflectorNet.Tests.SchemaTests.TestType{TypeUtils.ArraySuffix}", result);
Assert.Equal("com.IvanMurzak.ReflectorNet.Tests.SchemaTests.TestType%5B%5D", result);
_output.WriteLine($"TestType[] -> {result}");
}
}
Expand Down
16 changes: 14 additions & 2 deletions ReflectorNet/src/Utils/TypeUtils.Name.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,20 @@ public static string GetTypeId(Type type)
return Sanitize(type);
}

public static string GetSchemaTypeId<T>() => GetTypeId(typeof(T));
public static string GetSchemaTypeId(Type type) => GetTypeId(type);
public static string GetSchemaTypeId<T>() => GetSchemaTypeId(typeof(T));
public static string GetSchemaTypeId(Type type)
{
var typeId = GetTypeId(type);
return SanitizeForJsonSchemaRef(typeId);
}

private static string SanitizeForJsonSchemaRef(string typeId)
{
if (string.IsNullOrEmpty(typeId))
return typeId;
return typeId.Replace("[", "%5B").Replace("]", "%5D")
.Replace("<", "%3C").Replace(">", "%3E");
Comment on lines +129 to +130
}

public static bool IsNameMatch(Type? type, string? typeName)
{
Expand Down
Loading