-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
System.Text.Json SourceGenerator results in a compile error with a List<> of enums with a JsonConverter #61860
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsDescriptionAttempting to define a serialization context against a type that has a
Reproduction StepsTest.csproj: <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project> Program.cs: using System.Text.Json.Serialization;
using System.Collections.Generic;
return;
[JsonSourceGenerationOptions(
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
GenerationMode = JsonSourceGenerationMode.Serialization | JsonSourceGenerationMode.Metadata,
WriteIndented = false)]
[JsonSerializable(typeof(TestStruct))]
internal partial class TestSerializationContext : JsonSerializerContext
{
}
internal struct TestStruct
{
public List<TestEnum> Children { get; set; }
}
[JsonConverter(typeof(JsonStringEnumConverter))]
internal enum TestEnum
{
Aye, Bee, Cee
} Then Expected behaviorExpected the build to succeed. Actual behaviorBuild fails with:
Regression?I don't think this is applicable, however, converting the test sample to .NET 5 in the repro steps results in the same error (after adding a reference to System.Text.Json 6.0.0.0). Known WorkaroundsRemoving the Configuration
Other informationNo response
|
Minimal reproduction: using System.Text.Json.Serialization;
using System.Collections.Generic;
System.Console.WriteLine();
[JsonSerializable(typeof(TestEnum))] // comment out to induce error
[JsonSerializable(typeof(List<TestEnum>))]
internal partial class TestSerializationContext : JsonSerializerContext
{
}
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum TestEnum
{
Aye, Bee, Cee
} Generated code for types using converter factories depend on a As a temporary workaround you could try declaring the enum type itself as Related to #62079 and #58267. We should fix this in conjunction with #62079. |
Description
Attempting to define a
JsonSerializerContext
against a type that has aList<T>
whereT
ultimately contains an enum withJsonConverter
defined results in the following compile error:Reproduction Steps
Test.csproj:
Program.cs:
Then
dotnet build
or build within Visual Studio (2022).Expected behavior
Expected the build to succeed.
Actual behavior
Build fails with:
Regression?
I don't think this is applicable, however, converting the test sample to .NET 5 in the repro steps results in the same error (after adding a reference to System.Text.Json 6.0.0.0).
Known Workarounds
Removing the
JsonConverter
attribute, however it results in different json output, using integers instead of strings, which is okay for my case as I control both producing and consuming, I did not search for other workarounds beyond this.Configuration
Other information
No response
The text was updated successfully, but these errors were encountered: