Skip to content

JSON source generator emits CS0618 warnings if serialized types use [Obsolete] #62076

@martincostello

Description

@martincostello

Description

If an application uses the .NET 6 source generator for JSON with type(s) that have one or more properties decorated with the [Obsolete] property, three CS0618 warnings will be emitted by the compiler for each such property.

If the application does not control the code of all of the types it (de)serializes these warnings cannot be worked around by removing the properties' obsolete attributes (for example when using shared models distributed via NuGet).

If the application uses TreatWarningsAsErrors or any obsolete properties are marked as an error to use, the application fails to compile.

Reproduction Steps

Compile the following application using the .NET 6.0.100 SDK with dotnet build.

using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

var value = new MyObject { NewValue = "Hello World" };
var bytes = JsonSerializer.SerializeToUtf8Bytes(value, typeof(MyObject), new MyJsonContext(new (JsonSerializerDefaults.Web)));
var json = Encoding.UTF8.GetString(bytes);

Console.WriteLine(json);

public class MyObject
{
    public string? NewValue {get; set; }

    [Obsolete("Use NewValue instead")]
    public string? OldValue {get; set; }
}

[JsonSerializable(typeof(MyObject))]
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 with zero warnings/errors due to the obsolete properties not being used by user code.

Actual behavior

The application emits three CS0618 warnings per obsolete property from usages in the generated code.

> 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.MyObject.g.cs(72,42): warning CS0618: 'MyObject.OldValue' is obsolete: 'Use NewValue instead' [C:\Coding\martincostello\JsonSourceGeneratorObsoleteProperties\JsonSourceGeneratorObsoleteProperties.csproj]
C:\Coding\martincostello\JsonSourceGeneratorObsoleteProperties\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyJsonContext.MyObject.g.cs(73,49): warning CS0618: 'MyObject.OldValue' is obsolete: 'Use NewValue instead' [C:\Coding\martincostello\JsonSourceGeneratorObsoleteProperties\JsonSourceGeneratorObsoleteProperties.csproj]
C:\Coding\martincostello\JsonSourceGeneratorObsoleteProperties\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyJsonContext.MyObject.g.cs(97,51): warning CS0618: 'MyObject.OldValue' is obsolete: 'Use NewValue instead' [C:\Coding\martincostello\JsonSourceGeneratorObsoleteProperties\JsonSourceGeneratorObsoleteProperties.csproj]
  JsonSourceGeneratorObsoleteProperties -> C:\Coding\martincostello\JsonSourceGeneratorObsoleteProperties\bin\Debug\net6.0\JsonSourceGeneratorObsoleteProperties.dll

Build succeeded.

C:\Coding\martincostello\JsonSourceGeneratorObsoleteProperties\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyJsonContext.MyObject.g.cs(72,42): warning CS0618: 'MyObject.OldValue' is obsolete: 'Use NewValue instead' [C:\Coding\martincostello\JsonSourceGeneratorObsoleteProperties\JsonSourceGeneratorObsoleteProperties.csproj]
C:\Coding\martincostello\JsonSourceGeneratorObsoleteProperties\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyJsonContext.MyObject.g.cs(73,49): warning CS0618: 'MyObject.OldValue' is obsolete: 'Use NewValue instead' [C:\Coding\martincostello\JsonSourceGeneratorObsoleteProperties\JsonSourceGeneratorObsoleteProperties.csproj]
C:\Coding\martincostello\JsonSourceGeneratorObsoleteProperties\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyJsonContext.MyObject.g.cs(97,51): warning CS0618: 'MyObject.OldValue' is obsolete: 'Use NewValue instead' [C:\Coding\martincostello\JsonSourceGeneratorObsoleteProperties\JsonSourceGeneratorObsoleteProperties.csproj]
    3 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.99

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions