Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorgithub-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>2022-09-01 10:29:14 +0300
committerGitHub <noreply@github.com>2022-09-01 10:29:14 +0300
commit608da95fedbb4686789cdd98102a94f341a41fb8 (patch)
tree90047c293d6fd0bb03c56dd47a654822229e341d /src
parent6309bdc0ada2ac3b0734252ce7ab92283a04ed8f (diff)
[release/7.0] Disable nullability warnings in JSON source generator (#74861)
* Disable nullability warnings in JSON source generator * Add testcase for #61734 Co-authored-by: Krzysztof Wicher <kwicher@microsoft.com>
Diffstat (limited to 'src')
-rw-r--r--src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs4
-rw-r--r--src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs40
-rw-r--r--src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/System.Text.Json.SourceGeneration.Tests.targets4
-rw-r--r--src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs12
4 files changed, 59 insertions, 1 deletions
diff --git a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
index d9698c71ce3..68b0bf76bf9 100644
--- a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
+++ b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
@@ -152,7 +152,9 @@ namespace System.Text.Json.SourceGeneration
bool isInGlobalNamespace = @namespace == JsonConstants.GlobalNamespaceValue;
StringBuilder sb = new(@"// <auto-generated/>
-#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 7e97443adfa..b89fcf34ed5 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,37 @@ namespace System.Text.Json.SourceGeneration.Tests
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);
+ }
+
+ // 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<string, object?>()
+ {
+ ["foo"] = "bar",
+ ["test"] = "baz",
+ });
+
+ string json = JsonSerializer.Serialize(obj, options);
+ Assert.Equal("""{"DictionaryProperty":{"foo":"bar","test":"baz"}}""", json);
+ }
+
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum TestEnum
{
@@ -394,7 +425,16 @@ namespace System.Text.Json.SourceGeneration.Tests
[JsonSerializable(typeof(ClassWithPocoListDictionaryAndNullable))]
internal partial class ClassWithPocoListDictionaryAndNullablePropertyContext : JsonSerializerContext
{
+ }
+ [JsonSerializable(typeof(ClassWithStringValues))]
+ internal partial class ClassWithStringValuesContext : JsonSerializerContext
+ {
+ }
+
+ [JsonSerializable(typeof(ClassWithDictionaryProperty))]
+ internal partial class ClassWithDictionaryPropertyContext : 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 a759efb7739..1b9e0d17b19 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 @@
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMemberTypes.cs" />
</ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Primitives\src\Microsoft.Extensions.Primitives.csproj" />
+ </ItemGroup>
+
<Target Name="FixIncrementalCoreCompileWithAnalyzers" BeforeTargets="CoreCompile">
<ItemGroup>
<CustomAdditionalCompileInputs Include="@(Analyzer)" />
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 4a52c8ce817..eb4da1df79f 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,15 @@ namespace System.Text.Json.SourceGeneration.Tests
{
internal class InternalNestedClass { }
}
+
+ public sealed class ClassWithStringValues
+ {
+ public StringValues StringValuesProperty { get; set; }
+ }
+
+ public class ClassWithDictionaryProperty
+ {
+ public ClassWithDictionaryProperty(Dictionary<string, object?> property) => DictionaryProperty = property;
+ public Dictionary<string, object?> DictionaryProperty { get; }
+ }
}