Skip to content

Obsoletions with a DiagnosticId still get warnings in generated code #101738

Open
@eerhardt

Description

@eerhardt

Description

This issue is tracking updating the System.Text.Json and ConfigurationBinder source generators to take advantage of dotnet/roslyn#73292 when it is implemented.

Original Description

When the System.Text.Json and ConfigurationBinder source generators generate code against types that have properties with [Obsolete("message", DiagnosticId = "ID01")] attributes, the generated code is emitting warnings that I can't suppress and can't control. The only thing I can do is suppress the warning globally, which isn't ideal because I still want those warnings for my "hand written" code.

Both of those generators already suppress "normal" obsoletions with #pragma warning disable CS0612, CS0618. But since these obsoletion warnings get their own DiagnosticID, those suppressions don't work.

(Note the case I hit was with the Configuration Binder source generator against a class that had an X509Certificate2 property - which has 3 properties with different DiagnosticIDs - SYSLIB0026;SYSLIB0027;SYSLIB0028.)

Reproduction Steps

dotnet build the following project:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
  </ItemGroup>

</Project>
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System.Text.Json.Serialization;

var builder = Host.CreateApplicationBuilder(args);

var c = new MyClass();
builder.Configuration.Bind(c);

[JsonSerializable(typeof(MyClass))]
public partial class JsonContext : JsonSerializerContext
{
}

public class MyClass
{
    [Obsolete("message", DiagnosticId = "EE01")]
    public string? Name { get; set; }
}

Expected behavior

I shouldn't get warnings that I can't take action against.

Actual behavior

I get the following warnings:

\ConsoleApp119\obj\Debug\net8.0\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(65,17): warning EE01: 'MyClass.Name' is obsolete: 'message' [C:\Users\eerhardt\source\repos\ConsoleApp119\ConsoleApp119\ConsoleApp119.csproj]
\ConsoleApp119\obj\Debug\net8.0\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonContext.MyClass.g.cs(53,36): warning EE01: 'MyClass.Name' is obsolete: 'message' [C:\Users\eerhardt\source\repos\ConsoleApp119\ConsoleApp119\ConsoleApp119.csproj]
\ConsoleApp119\obj\Debug\net8.0\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonContext.MyClass.g.cs(54,45): warning EE01: 'MyClass.Name' is obsolete: 'message' [C:\Users\eerhardt\source\repos\ConsoleApp119\ConsoleApp119\ConsoleApp119.csproj]
\ConsoleApp119\obj\Debug\net8.0\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonContext.MyClass.g.cs(80,43): warning EE01: 'MyClass.Name' is obsolete: 'message' [C:\Users\eerhardt\source\repos\ConsoleApp119\ConsoleApp119\ConsoleApp119.csproj]

Regression?

No response

Known Workarounds

globally <NoWarn> these warnings.

Configuration

No response

Other information

cc @ericstj @eiriktsarpalis @tarekgh

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions