Skip to content

System.Text.Json SourceGenerator results in a compile error with a List<> of enums with a JsonConverter #61860

Closed

Description

@AshleighAdams

Description

Attempting to define a JsonSerializerContext against a type that has a List<T> where T ultimately contains an enum with JsonConverter defined results in the following compile error:

TestSerializationContext.TestEnum.g.cs(19,90,19,113): error CS0103: The name 'GetConverterFromFactory' does not exist in the current context

Reproduction Steps

Test.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 dotnet build or build within Visual Studio (2022).

Expected behavior

Expected the build to succeed.

Actual behavior

Build fails with:

Build started...
1>------ Build started: Project: Test, Configuration: Debug Any CPU ------
1>C:\Git\Test\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\TestSerializationContext.TestEnum.g.cs(19,90,19,113): error CS0103: The name 'GetConverterFromFactory' does not exist in the current context
1>Done building project "Test.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

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

$ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.100
 Commit:    9e8b04bbff

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19043
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.100\

Host (useful for support):
  Version: 6.0.0
  Commit:  4822e3c3aa

.NET SDKs installed:
  6.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other information

No response

Activity

  1. ghost added
    untriaged
    on Nov 20, 2021
  2. added this to the 7.0.0 milestone on Nov 20, 2021
  3. ghost removed
    untriaged
    on Nov 20, 2021
  4. removed this from the 7.0.0 milestone on Nov 20, 2021
  5. added
    untriaged
    on Nov 22, 2021
  6. eiriktsarpalis commented on Dec 2, 2021

    @eiriktsarpalis
    Member

    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 GetConverterFromFactory helper method being generated in the context. However, the fact that TestEnum is only introduced as generic parameter in the JsonSerializable declarations appears to fool the generator into believing that such a helper is not needed.

    As a temporary workaround you could try declaring the enum type itself as JsonSerializable and everything should work as expected.

    Related to #62079 and #58267. We should fix this in conjunction with #62079.

  7. removed
    untriaged
    on Dec 2, 2021
  8. added this to the 6.0.x milestone on Dec 2, 2021
  9. ghost added
    in-pr
    on Dec 9, 2021
  10. ghost removed
    in-pr
    on Dec 10, 2021
  11. ghost locked as resolved and limited conversation to collaborators on Jan 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions