-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON source generator emits CS0618 warnings if serialized types use [Obsolete] #62076
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsDescriptionIf an application uses the .NET 6 source generator for JSON with type(s) that have one or more properties decorated with the 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 Reproduction StepsCompile the following application using the .NET 6.0.100 SDK with 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 behaviorThe application compiles with zero warnings/errors due to the obsolete properties not being used by user code. Actual behaviorThe application emits three > 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 WorkaroundsNo response ConfigurationPartial output from > 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 informationNo response
|
Curious, why would you consider this to be an issue? Marking a property obsolete does not prevent it from being serialized, this holds for both the reflection and source gen serializers. The warning simply reflects the fact that source gen accesses the property at compile time. If you need to prevent the property from being serialized, I would recommend adding a |
The problems this caused in the application I found this in are:
I've been investigating plugging the new source generator into Refit for our I'll file separate issue(s) for those problems if/when I can come up with a minimal repro (the error is |
We can just emit #pragma warning disable at the top of the generated file for any warnings we want suppressed for the generated code in that file. That would be separate from any global setting. |
Yeah, a "quick fix" for this could be to just emit a |
One concern is that we can only suppress warnings for codes we can predict are going to be an issue in source generated code. |
Since this issue is manifest, it seems reasonable to always suppress obsolete-usage warnings in the generated code. I can't think of a user scenario where it is desirable to surface them. Other codes could be considered as needed. I think it's reasonable to service a fix here given the current behavior can cause a non-trivial nuisance in user apps. |
I just encountered this issue, and one thing that makes it a lot worse, is that [JsonIgnore] does not help. For instance, the following code: public class ClassWithObsolete
{
[Obsolete("This is a test")]
public bool Test { get; set; }
}
public class MyJson
{
[JsonIgnore]
public ClassWithObsolete O { get; set; }
} The code still emits the obsolete warnings, even though the code generator should not have touched the type at all. Worse, if the Obsolete attribute has warning-as-error, the build breaks. I think this may be a bigger bug than suppressing warnings in generated code? Since there shouldn't have been generated code in the first place for this example? Right now the only workaround I see is to avoid using properties altogether and to use methods for those "members" that shouldn't be touched by serialization, and type-erase the backing field into object. Edit: the JsonIgnore part probably belongs to bug #62354 |
@fbrosseau as I suggested in #62354 (comment), it would be great if you could create a new dedicated issue for |
Re-opening for 6.0 consideration - #63501. |
Fixed for 6.0 in #63501. |
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, threeCS0618
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
.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.Regression?
No.
Known Workarounds
No response
Configuration
Partial output from
dotnet --info
:Other information
No response
The text was updated successfully, but these errors were encountered: