Skip to content

Commit

Permalink
Additional tests for Generics in System.Text.Json.SourceGeneration (#…
Browse files Browse the repository at this point in the history
…72449)

* generic types tests

* test nested generic class serialization
  • Loading branch information
pos777 authored Jul 22, 2022
1 parent a5e678d commit ea2c907
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ public partial class ContextGenericContainer<T>
{
[JsonSerializable(typeof(JsonMessage))]
public partial class NestedInGenericContainerContext : JsonSerializerContext { }

[JsonSerializable(typeof(JsonMessage))]
public partial class NestedGenericInGenericContainerContext<T1> : JsonSerializerContext { }

public partial class NestedGenericContainer<T1>
{
[JsonSerializable(typeof(JsonMessage))]
public partial class NestedInNestedGenericContainerContext : JsonSerializerContext { }

[JsonSerializable(typeof(JsonMessage))]
public partial class NestedGenericInNestedGenericContainerContext<T2> : JsonSerializerContext { }
}
}

[JsonSerializable(typeof(MyContainingClass.MyNestedClass.MyNestedNestedClass))]
Expand All @@ -136,5 +148,6 @@ public partial class NestedInGenericContainerContext : JsonSerializerContext { }
[JsonSerializable(typeof(MyContainingGenericClass<int>.MyNestedClass.MyNestedNestedGenericClass<int>))]
[JsonSerializable(typeof(MyContainingGenericClass<int>.MyNestedGenericClass<int>.MyNestedGenericNestedClass))]
[JsonSerializable(typeof(MyContainingGenericClass<int>.MyNestedGenericClass<int>.MyNestedGenericNestedGenericClass<int>))]
[JsonSerializable(typeof(MyContainingGenericClass<MyContainingGenericClass<int>.MyNestedGenericClass<int>.MyNestedGenericNestedGenericClass<int>>.MyNestedGenericClass<int>.MyNestedGenericNestedGenericClass<int>))]
internal partial class NestedGenericTypesContext : JsonSerializerContext { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,33 @@ public static void VariousNestingAndVisibilityLevelsAreSupported()
[Fact]
public static void VariousGenericsAreSupported()
{
Assert.NotNull(GenericContext<object>.Default);
Assert.NotNull(ContextGenericContainer<object>.NestedInGenericContainerContext.Default);
AssertGenericContext(GenericContext<int>.Default);
AssertGenericContext(ContextGenericContainer<int>.NestedInGenericContainerContext.Default);
AssertGenericContext(ContextGenericContainer<int>.NestedGenericInGenericContainerContext<int>.Default);
AssertGenericContext(ContextGenericContainer<int>.NestedGenericContainer<int>.NestedInNestedGenericContainerContext.Default);
AssertGenericContext(ContextGenericContainer<int>.NestedGenericContainer<int>.NestedGenericInNestedGenericContainerContext<int>.Default);

Assert.NotNull(NestedGenericTypesContext.Default);
var original = new MyContainingGenericClass<int>.MyNestedGenericClass<int>.MyNestedGenericNestedGenericClass<int>()
{
DataT = 1,
DataT1 = 10,
DataT2 = 100
};
Type type = typeof(MyContainingGenericClass<int>.MyNestedGenericClass<int>.MyNestedGenericNestedGenericClass<int>);
string json = JsonSerializer.Serialize(original, type, NestedGenericTypesContext.Default);
var deserialized = (MyContainingGenericClass<int>.MyNestedGenericClass<int>.MyNestedGenericNestedGenericClass<int>)JsonSerializer.Deserialize(json, type, NestedGenericTypesContext.Default);
Assert.Equal(1, deserialized.DataT);
Assert.Equal(10, deserialized.DataT1);
Assert.Equal(100, deserialized.DataT2);

static void AssertGenericContext(JsonSerializerContext context)
{
Assert.NotNull(context);
string json = JsonSerializer.Serialize(new JsonMessage { Message = "Hi" }, typeof(JsonMessage), context);
JsonMessage deserialized = (JsonMessage)JsonSerializer.Deserialize(json, typeof(JsonMessage), context);
Assert.Equal("Hi", deserialized.Message);
}
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,12 @@ public class MyNestedNestedGenericClass<T1> { }
public class MyNestedGenericClass<T1>
{
public class MyNestedGenericNestedClass { }
public class MyNestedGenericNestedGenericClass<T2> { }
public class MyNestedGenericNestedGenericClass<T2>
{
public T DataT { get; set; }
public T1 DataT1 { get; set; }
public T2 DataT2 { get; set; }
}
}
}
}

0 comments on commit ea2c907

Please sign in to comment.