Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional tests for Generics in System.Text.Json.SourceGeneration #72449

Merged
merged 2 commits into from
Jul 22, 2022
Merged
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
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 { }
pos777 marked this conversation as resolved.
Show resolved Hide resolved

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; }
}
}
}
}