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.82
Regression?
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: 4822e3c3aa
Other information
No response