Skip to content

Commit 608da95

Browse files
[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>
1 parent 6309bdc commit 608da95

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ private void AddSource(string fileName, string source, bool isRootContextDef = f
152152
bool isInGlobalNamespace = @namespace == JsonConstants.GlobalNamespaceValue;
153153

154154
StringBuilder sb = new(@"// <auto-generated/>
155-
#nullable enable
155+
156+
#nullable enable annotations
157+
#nullable disable warnings
156158
157159
// Suppress warnings about [Obsolete] member usage in generated code.
158160
#pragma warning disable CS0618");

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,37 @@ public static void SupportsGenericParameterWithCustomConverterFactory()
380380
Assert.Equal(@"[""Cee""]", json);
381381
}
382382

383+
// Regression test for https://github.com/dotnet/runtime/issues/74652
384+
[Fact]
385+
public static void ClassWithStringValuesRoundtrips()
386+
{
387+
JsonSerializerOptions options = ClassWithStringValuesContext.Default.Options;
388+
389+
ClassWithStringValues obj = new()
390+
{
391+
StringValuesProperty = new(new[] { "abc", "def" })
392+
};
393+
394+
string json = JsonSerializer.Serialize(obj, options);
395+
Assert.Equal("""{"StringValuesProperty":["abc","def"]}""", json);
396+
}
397+
398+
// Regression test for https://github.com/dotnet/runtime/issues/61734
399+
[Fact]
400+
public static void ClassWithDictionaryPropertyRoundtrips()
401+
{
402+
JsonSerializerOptions options = ClassWithDictionaryPropertyContext.Default.Options;
403+
404+
ClassWithDictionaryProperty obj = new(new Dictionary<string, object?>()
405+
{
406+
["foo"] = "bar",
407+
["test"] = "baz",
408+
});
409+
410+
string json = JsonSerializer.Serialize(obj, options);
411+
Assert.Equal("""{"DictionaryProperty":{"foo":"bar","test":"baz"}}""", json);
412+
}
413+
383414
[JsonConverter(typeof(JsonStringEnumConverter))]
384415
public enum TestEnum
385416
{
@@ -394,7 +425,16 @@ internal partial class GenericParameterWithCustomConverterFactoryContext : JsonS
394425
[JsonSerializable(typeof(ClassWithPocoListDictionaryAndNullable))]
395426
internal partial class ClassWithPocoListDictionaryAndNullablePropertyContext : JsonSerializerContext
396427
{
428+
}
397429

430+
[JsonSerializable(typeof(ClassWithStringValues))]
431+
internal partial class ClassWithStringValuesContext : JsonSerializerContext
432+
{
433+
}
434+
435+
[JsonSerializable(typeof(ClassWithDictionaryProperty))]
436+
internal partial class ClassWithDictionaryPropertyContext : JsonSerializerContext
437+
{
398438
}
399439

400440
internal class ClassWithPocoListDictionaryAndNullable

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/System.Text.Json.SourceGeneration.Tests.targets

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@
112112
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMemberTypes.cs" />
113113
</ItemGroup>
114114

115+
<ItemGroup>
116+
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Primitives\src\Microsoft.Extensions.Primitives.csproj" />
117+
</ItemGroup>
118+
115119
<Target Name="FixIncrementalCoreCompileWithAnalyzers" BeforeTargets="CoreCompile">
116120
<ItemGroup>
117121
<CustomAdditionalCompileInputs Include="@(Analyzer)" />

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.Generic;
55
using System.Text.Json.Serialization;
6+
using Microsoft.Extensions.Primitives;
67

78
namespace System.Text.Json.SourceGeneration.Tests.RepeatedTypes
89
{
@@ -275,4 +276,15 @@ public class PublicTestClass
275276
{
276277
internal class InternalNestedClass { }
277278
}
279+
280+
public sealed class ClassWithStringValues
281+
{
282+
public StringValues StringValuesProperty { get; set; }
283+
}
284+
285+
public class ClassWithDictionaryProperty
286+
{
287+
public ClassWithDictionaryProperty(Dictionary<string, object?> property) => DictionaryProperty = property;
288+
public Dictionary<string, object?> DictionaryProperty { get; }
289+
}
278290
}

0 commit comments

Comments
 (0)