Repository navigation
Closed
Description
Description
If a object graph used with the JSON source generator has a child object with a property whose type is an enumeration which is decorated with a [JsonConverter] attribute, compilation will fail with a CS1061 error.
error CS1061: 'MyJsonContext' does not contain a definition for 'GetConverterFromFactory' and no accessible extension method 'GetConverterFromFactory' accepting a first argument of type 'MyJsonContext' could be found (are you missing a using directive or an assembly reference?)
If the [JsonConverter] attribute is removed, or if the property is not part of a child of the root node of the object graph, the code compiles.
Reproduction Steps
To reproduce run dotnet build using the .NET 6.0.100 SDK for the below application.
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
var value = new Parent { Child = new() { Value = MyEnum.Value2 } };
var bytes = JsonSerializer.SerializeToUtf8Bytes(value, typeof(Parent), new MyJsonContext(new (JsonSerializerDefaults.Web)));
var json = Encoding.UTF8.GetString(bytes);
Console.WriteLine(json);
public enum MyEnum
{
Value1,
Value2,
}
public class Parent
{
public Child? Child { get; set; }
}
public class Child
{
// Remove the attribute below and compilation will succeed
[JsonConverter(typeof(JsonStringEnumConverter))]
public MyEnum Value { get; set; }
}
[JsonSerializable(typeof(Parent))]
public partial class MyJsonContext : JsonSerializerContext
{
}<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<NoWarn>$(NoWarn);CA1050</NoWarn>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
</Project>Expected behavior
The application compiles successfully.
Actual behavior
The application fails to compile.
> dotnet build
Microsoft (R) Build Engine version 17.0.0+c9eb9dd64 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
All projects are up-to-date for restore.
C:\Coding\martincostello\JsonSourceGeneratorObsoleteProperties\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyJsonContext.Child.g.cs(51,41): error CS1061: 'MyJsonContext' does not contain a definition for 'GetConverterFromFactory' and no accessible extension method 'GetConverterFromFactory' accepting a first argument of type 'MyJsonContext' could be found (are you missing a using directive or an assembly reference?) [C:\Coding\martincostello\JsonSourceGeneratorObsoleteProperties\JsonSourceGeneratorObsoleteProperties.csproj]
Build FAILED.
C:\Coding\martincostello\JsonSourceGeneratorObsoleteProperties\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyJsonContext.Child.g.cs(51,41): error CS1061: 'MyJsonContext' does not contain a definition for 'GetConverterFromFactory' and no accessible extension method 'GetConverterFromFactory' accepting a first argument of type 'MyJsonContext' could be found (are you missing a using directive or an assembly reference?) [C:\Coding\martincostello\JsonSourceGeneratorObsoleteProperties\JsonSourceGeneratorObsoleteProperties.csproj]
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:01.82Regression?
No.
Known Workarounds
No response
Configuration
Partial output from dotnet --info:
> 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: 4822e3c3aaOther information
No response
Activity
eiriktsarpalis commentedon Nov 26, 2021
I can reproduce. This is related to #58267: it looks like we fixed
JsonConverterdeclarations at the type level but missed property level declarations. cc @layomia @steveharter@martincostello in the meantime you might be able to work around the bug by declaring the converter at the type level. If that's not possible it should still be possible to register the converter with your context like so:
martincostello commentedon Nov 26, 2021
Thanks @eiriktsarpalis - we use those in our app already so we should be good will the attribute commented out for me trying out the source generator. We just also used the attribute to ensure that other apps consuming the models via our NuGet packages would get the right behaviour out-of-the-box without having to specifically set up the
JsonSerializerOptions.