From d8c2e4e9f1f7273ea13cf6a24b5bb0dad8259a49 Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Tue, 30 Aug 2022 12:10:47 +0200 Subject: [PATCH 1/2] Disable nullability warnings in JSON source generator --- .../gen/JsonSourceGenerator.Emitter.cs | 3 ++- .../JsonSerializerContextTests.cs | 19 +++++++++++++++++++ ...m.Text.Json.SourceGeneration.Tests.targets | 4 ++++ .../TestClasses.cs | 6 ++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs index d9698c71ce3f00..92d55420c77064 100644 --- a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs +++ b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs @@ -152,7 +152,8 @@ private void AddSource(string fileName, string source, bool isRootContextDef = f bool isInGlobalNamespace = @namespace == JsonConstants.GlobalNamespaceValue; StringBuilder sb = new(@"// -#nullable enable +#nullable enable annotations +#nullable disable warnings // Suppress warnings about [Obsolete] member usage in generated code. #pragma warning disable CS0618"); diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs index 7e97443adfabc4..8de6e8ad908711 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs @@ -380,6 +380,21 @@ public static void SupportsGenericParameterWithCustomConverterFactory() Assert.Equal(@"[""Cee""]", json); } + // Regression test for https://github.com/dotnet/runtime/issues/74652 + [Fact] + public static void ClassWithStringValuesRoundtrips() + { + JsonSerializerOptions options = ClassWithStringValuesContext.Default.Options; + + ClassWithStringValues obj = new() + { + StringValuesProperty = new(new[] { "abc", "def" }) + }; + + string json = JsonSerializer.Serialize(obj, options); + Assert.Equal("""{"StringValuesProperty":["abc","def"]}""", json); + } + [JsonConverter(typeof(JsonStringEnumConverter))] public enum TestEnum { @@ -394,7 +409,11 @@ internal partial class GenericParameterWithCustomConverterFactoryContext : JsonS [JsonSerializable(typeof(ClassWithPocoListDictionaryAndNullable))] internal partial class ClassWithPocoListDictionaryAndNullablePropertyContext : JsonSerializerContext { + } + [JsonSerializable(typeof(ClassWithStringValues))] + internal partial class ClassWithStringValuesContext : JsonSerializerContext + { } internal class ClassWithPocoListDictionaryAndNullable diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/System.Text.Json.SourceGeneration.Tests.targets b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/System.Text.Json.SourceGeneration.Tests.targets index a759efb7739842..1b9e0d17b199e8 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/System.Text.Json.SourceGeneration.Tests.targets +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/System.Text.Json.SourceGeneration.Tests.targets @@ -112,6 +112,10 @@ + + + + diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs index 4a52c8ce8179b0..45ec06f4428280 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Text.Json.Serialization; +using Microsoft.Extensions.Primitives; namespace System.Text.Json.SourceGeneration.Tests.RepeatedTypes { @@ -275,4 +276,9 @@ public class PublicTestClass { internal class InternalNestedClass { } } + + public sealed class ClassWithStringValues + { + public StringValues StringValuesProperty { get; set; } + } } From dd2aa95b381690a39a65faedc3e6d5c8efd18fbc Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Tue, 30 Aug 2022 16:54:22 +0200 Subject: [PATCH 2/2] Add testcase for #61734 --- .../gen/JsonSourceGenerator.Emitter.cs | 1 + .../JsonSerializerContextTests.cs | 21 +++++++++++++++++++ .../TestClasses.cs | 6 ++++++ 3 files changed, 28 insertions(+) diff --git a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs index 92d55420c77064..68b0bf76bf9757 100644 --- a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs +++ b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs @@ -152,6 +152,7 @@ private void AddSource(string fileName, string source, bool isRootContextDef = f bool isInGlobalNamespace = @namespace == JsonConstants.GlobalNamespaceValue; StringBuilder sb = new(@"// + #nullable enable annotations #nullable disable warnings diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs index 8de6e8ad908711..b89fcf34ed5ce0 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs @@ -395,6 +395,22 @@ public static void ClassWithStringValuesRoundtrips() Assert.Equal("""{"StringValuesProperty":["abc","def"]}""", json); } + // Regression test for https://github.com/dotnet/runtime/issues/61734 + [Fact] + public static void ClassWithDictionaryPropertyRoundtrips() + { + JsonSerializerOptions options = ClassWithDictionaryPropertyContext.Default.Options; + + ClassWithDictionaryProperty obj = new(new Dictionary() + { + ["foo"] = "bar", + ["test"] = "baz", + }); + + string json = JsonSerializer.Serialize(obj, options); + Assert.Equal("""{"DictionaryProperty":{"foo":"bar","test":"baz"}}""", json); + } + [JsonConverter(typeof(JsonStringEnumConverter))] public enum TestEnum { @@ -416,6 +432,11 @@ internal partial class ClassWithStringValuesContext : JsonSerializerContext { } + [JsonSerializable(typeof(ClassWithDictionaryProperty))] + internal partial class ClassWithDictionaryPropertyContext : JsonSerializerContext + { + } + internal class ClassWithPocoListDictionaryAndNullable { public uint UIntProperty { get; set; } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs index 45ec06f4428280..eb4da1df79f20e 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs @@ -281,4 +281,10 @@ public sealed class ClassWithStringValues { public StringValues StringValuesProperty { get; set; } } + + public class ClassWithDictionaryProperty + { + public ClassWithDictionaryProperty(Dictionary property) => DictionaryProperty = property; + public Dictionary DictionaryProperty { get; } + } }