Skip to content
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 @@ -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
Comment thread
krwq marked this conversation as resolved.
Comment thread
krwq marked this conversation as resolved.
#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
Comment thread
krwq marked this conversation as resolved.
[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; }
}
}