Skip to content

Commit

Permalink
[release/7.0] Disable nullability warnings in JSON source generator (#…
Browse files Browse the repository at this point in the history
…74861)

* Disable nullability warnings in JSON source generator

* Add testcase for #61734

Co-authored-by: Krzysztof Wicher <kwicher@microsoft.com>
  • Loading branch information
github-actions[bot] and krwq committed Sep 1, 2022
1 parent 6309bdc commit 608da95
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ private void AddSource(string fileName, string source, bool isRootContextDef = f
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,37 @@ 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);
}

// 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
{
Expand All @@ -394,7 +425,16 @@ internal partial class GenericParameterWithCustomConverterFactoryContext : JsonS
[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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Primitives;

namespace System.Text.Json.SourceGeneration.Tests.RepeatedTypes
{
Expand Down Expand Up @@ -275,4 +276,15 @@ public class PublicTestClass
{
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; }
}
}

0 comments on commit 608da95

Please sign in to comment.