Skip to content
Closed
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
21 changes: 18 additions & 3 deletions ReflectorNet.Tests/src/SchemaTests/TestSchemaTypeId.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using com.IvanMurzak.ReflectorNet.OuterAssembly.Model;
using com.IvanMurzak.ReflectorNet.Utils;
using Xunit.Abstractions;

Expand All @@ -8,6 +9,20 @@ public class TestSchemaTypeId : BaseTest
{
public TestSchemaTypeId(ITestOutputHelper output) : base(output) { }

[Fact]
public void GetSchemaTypeId_NestedClass_ShouldEncodePlusSymbol()
{
// Arrange
var type = typeof(ParentClass.NestedClass);

// Act
var result = type.GetSchemaTypeId();

// Assert
Assert.Equal("com.IvanMurzak.ReflectorNet.OuterAssembly.Model.ParentClass%2BNestedClass", result);
_output.WriteLine($"ParentClass.NestedClass -> {result}");
}

[Fact]
public void GetSchemaTypeId_SimpleArray_ShouldAppendArray()
{
Expand Down Expand Up @@ -116,7 +131,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 +145,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 +159,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
60 changes: 30 additions & 30 deletions ReflectorNet.Tests/src/TypeUtilsTests/GetTypeIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,22 @@ public GetTypeIdTests(ITestOutputHelper output) : base(output) { }
public static readonly Dictionary<string, Type> BuiltInArrayTypes = new Dictionary<string, Type>
{
// Simple arrays
["System.Int32[]"] = typeof(int[]),
["System.String[]"] = typeof(string[]),
["System.Boolean[]"] = typeof(bool[]),
["System.Double[]"] = typeof(double[]),
["System.Object[]"] = typeof(object[]),
["System.Byte[]"] = typeof(byte[]),
["System.DateTime[]"] = typeof(DateTime[]),
["System.Guid[]"] = typeof(Guid[]),
["System.Int32%5B%5D"] = typeof(int[]),
["System.String%5B%5D"] = typeof(string[]),
["System.Boolean%5B%5D"] = typeof(bool[]),
["System.Double%5B%5D"] = typeof(double[]),
["System.Object%5B%5D"] = typeof(object[]),
["System.Byte%5B%5D"] = typeof(byte[]),
["System.DateTime%5B%5D"] = typeof(DateTime[]),
["System.Guid%5B%5D"] = typeof(Guid[]),

// Jagged arrays
["System.Int32[][]"] = typeof(int[][]),
["System.String[][]"] = typeof(string[][]),
["System.Object[][]"] = typeof(object[][]),
["System.Int32%5B%5D%5B%5D"] = typeof(int[][]),
["System.String%5B%5D%5B%5D"] = typeof(string[][]),
["System.Object%5B%5D%5B%5D"] = typeof(object[][]),

// Triple jagged arrays
["System.Int32[][][]"] = typeof(int[][][]),
["System.Int32%5B%5D%5B%5D%5B%5D"] = typeof(int[][][]),

// Multi-dimensional arrays
["System.Int32[,]"] = typeof(int[,]),
Expand All @@ -118,11 +118,11 @@ public GetTypeIdTests(ITestOutputHelper output) : base(output) { }

// Mixed arrays (Array of 2D arrays)
// Note: C# syntax int[][,] is (int[,])[] -> System.Int32[,][]
["System.Int32[,][]"] = typeof(int[][,]),
["System.Int32[,]%5B%5D"] = typeof(int[][,]),

// Mixed arrays (2D array of arrays)
// Note: C# syntax int[,][] is (int[])[,] -> System.Int32[][,]
["System.Int32[][,]"] = typeof(int[,][]),
["System.Int32%5B%5D[,]"] = typeof(int[,][]),
};

#endregion
Expand Down Expand Up @@ -185,12 +185,12 @@ public GetTypeIdTests(ITestOutputHelper output) : base(output) { }
public static readonly Dictionary<string, Type> NestedGenericTypes = new Dictionary<string, Type>
{
// List of arrays
["System.Collections.Generic.List<System.Int32[]>"] = typeof(List<int[]>),
["System.Collections.Generic.List<System.String[]>"] = typeof(List<string[]>),
["System.Collections.Generic.List<System.Int32%5B%5D>"] = typeof(List<int[]>),
["System.Collections.Generic.List<System.String%5B%5D>"] = typeof(List<string[]>),

// Array of lists
["System.Collections.Generic.List<System.Int32>[]"] = typeof(List<int>[]),
["System.Collections.Generic.List<System.String>[]"] = typeof(List<string>[]),
["System.Collections.Generic.List<System.Int32>%5B%5D"] = typeof(List<int>[]),
["System.Collections.Generic.List<System.String>%5B%5D"] = typeof(List<string>[]),

// List of lists
["System.Collections.Generic.List<System.Collections.Generic.List<System.Int32>>"] = typeof(List<List<int>>),
Expand Down Expand Up @@ -232,8 +232,8 @@ public GetTypeIdTests(ITestOutputHelper output) : base(output) { }
["com.IvanMurzak.ReflectorNet.Tests.Model.SolarSystem+CelestialBody"] = typeof(SolarSystem.CelestialBody),

// Arrays of custom types
["com.IvanMurzak.ReflectorNet.Tests.Model.Vector3[]"] = typeof(Vector3[]),
["com.IvanMurzak.ReflectorNet.Tests.Model.GameObjectRef[]"] = typeof(GameObjectRef[]),
["com.IvanMurzak.ReflectorNet.Tests.Model.Vector3%5B%5D"] = typeof(Vector3[]),
["com.IvanMurzak.ReflectorNet.Tests.Model.GameObjectRef%5B%5D"] = typeof(GameObjectRef[]),

// Generic with custom types
["System.Collections.Generic.List<com.IvanMurzak.ReflectorNet.Tests.Model.Vector3>"] = typeof(List<Vector3>),
Expand All @@ -258,7 +258,7 @@ public GetTypeIdTests(ITestOutputHelper output) : base(output) { }
["com.IvanMurzak.ReflectorNet.Model.MethodData"] = typeof(MethodData),

// Arrays
["com.IvanMurzak.ReflectorNet.Model.SerializedMember[]"] = typeof(SerializedMember[]),
["com.IvanMurzak.ReflectorNet.Model.SerializedMember%5B%5D"] = typeof(SerializedMember[]),

// Generics with ReflectorNet types
["System.Collections.Generic.List<com.IvanMurzak.ReflectorNet.Model.SerializedMember>"] = typeof(List<SerializedMember>),
Expand Down Expand Up @@ -353,18 +353,18 @@ public GetTypeIdTests(ITestOutputHelper output) : base(output) { }
public static readonly Dictionary<string, Type> OuterAssemblyArrayTypes = new Dictionary<string, Type>
{
// Simple arrays
["com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterSimpleClass[]"] = typeof(OuterSimpleClass[]),
["com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterSimpleStruct[]"] = typeof(OuterSimpleStruct[]),
["com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterEnum[]"] = typeof(OuterEnum[]),
["com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterSimpleClass%5B%5D"] = typeof(OuterSimpleClass[]),
["com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterSimpleStruct%5B%5D"] = typeof(OuterSimpleStruct[]),
["com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterEnum%5B%5D"] = typeof(OuterEnum[]),

// Jagged arrays
["com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterSimpleClass[][]"] = typeof(OuterSimpleClass[][]),
["com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterSimpleClass%5B%5D%5B%5D"] = typeof(OuterSimpleClass[][]),

// Generic arrays
["com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterGenericClass<System.Int32>[]"] = typeof(OuterGenericClass<int>[]),
["com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterGenericClass<System.Int32>%5B%5D"] = typeof(OuterGenericClass<int>[]),

// Nested type arrays
["com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterContainer+NestedClass[]"] = typeof(OuterContainer.NestedClass[]),
["com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterContainer+NestedClass%5B%5D"] = typeof(OuterContainer.NestedClass[]),
};

#endregion
Expand Down Expand Up @@ -393,13 +393,13 @@ public GetTypeIdTests(ITestOutputHelper output) : base(output) { }
["System.Collections.Generic.List<com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterGenericClass<System.Int32>>"] = typeof(List<OuterGenericClass<int>>),

// Array of generic outer assembly type
["com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterGenericClass<System.String>[]"] = typeof(OuterGenericClass<string>[]),
["com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterGenericClass<System.String>%5B%5D"] = typeof(OuterGenericClass<string>[]),

// Generic with array type argument
["System.Collections.Generic.List<com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterSimpleClass[]>"] = typeof(List<OuterSimpleClass[]>),
["System.Collections.Generic.List<com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterSimpleClass%5B%5D>"] = typeof(List<OuterSimpleClass[]>),

// Dictionary with array value type
["System.Collections.Generic.Dictionary<System.String,com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterSimpleStruct[]>"] = typeof(Dictionary<string, OuterSimpleStruct[]>),
["System.Collections.Generic.Dictionary<System.String,com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterSimpleStruct%5B%5D>"] = typeof(Dictionary<string, OuterSimpleStruct[]>),

// Cross-assembly generic combinations
["com.IvanMurzak.ReflectorNet.OuterAssembly.Model.OuterGenericClass<com.IvanMurzak.ReflectorNet.Tests.Model.Vector3>"] = typeof(OuterGenericClass<Vector3>),
Expand Down
19 changes: 16 additions & 3 deletions ReflectorNet/src/Utils/TypeUtils.Name.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace com.IvanMurzak.ReflectorNet.Utils
{
public static partial class TypeUtils
{
public const string ArraySuffix = "[]";
public const string ArraySuffix = "%5B%5D";

/// <summary>
/// Returns the sanitized type name.
Expand Down Expand Up @@ -115,8 +115,21 @@ 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")
.Replace("+", "%2B");
Comment on lines +118 to +131
}

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