-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
System.Text.Json code generator creates metadata for classes that are explicitly ignored #76937
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionI have a few classes I'm trying to convert to using System.Text.Json codegenerator to better support trimming. Issue is likely related to this .net7 specific issue: #76807 Reproduction StepsAdd the following code to a .NET 6 app/library: [JsonSerializable(typeof(MyObject))]
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata, IgnoreReadOnlyFields = true)]
internal partial class MyObjectSerializationContext : JsonSerializerContext
{
}
public class MyObject
{
[JsonPropertyName("test")]
public string? Test { get; set; }
[JsonIgnore]
private readonly ComplexObject? _data; // Metadata generated for this type. Why?!?
}
public class ComplexObject
{
private protected object _thisLock = new object(); // This causes code gen problem
} Compile and observe the build errors and the classes that metadata is generated for. Expected behaviorProject builds with out errors. No metadata generated for Actual behavior
I don't get why it is even trying to generate anything for In my specific case, Regression?No response Known WorkaroundsI could add [JsonIgnore] on Configuration.NET 6.0.404 Other informationNo response
|
Thanks I totally missed that. Was tweeting hoping I'd wake up to someone telling me I'm doing it wrong, and if not, I was going to log it :-) |
Description
I have a few classes I'm trying to convert to using System.Text.Json codegenerator to better support trimming.
However I'm having trouble getting the code generator to not create metadata for classes that are ignored. This causes sometimes 100s of classes to get metadata generated which would never be used, as well as often hitting code it can't even generate correct code for.
Issue is likely related to this .net7 specific issue: #76807
Reproduction Steps
Add the following code to a .NET 6 app/library:
Compile and observe the build errors and the classes that metadata is generated for.
Expected behavior
Project builds with out errors. No metadata generated for
ComplexObject
or anything else ignored.Actual behavior
I don't get why it is even trying to generate anything for
ComplexObject
- I explicitly told it to ignore that field. This happens with .net6 also though (also tested 6.0.6 nuget package).In my specific case,
ComplexObject
is potentially a huge object graph and the result is I'm getting metadata generated for hundreds of classes.Regression?
No response
Known Workarounds
I could add [JsonIgnore] on
_thisLock
, but that only addresses this specific class - I don't really want to add json ignore to every field in all my classes that causes problems, when these classes aren't even meant for JSON serialization, and I don't want to bloat my assembly size with all this extraneous metadata generated.Configuration
.NET 6.0.404
Other information
No response
The text was updated successfully, but these errors were encountered: