You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
System.Text.Json code generator creates metadata for classes that are explicitly ignored #76937
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:
[JsonSerializable(typeof(MyObject))][JsonSourceGenerationOptions(GenerationMode=JsonSourceGenerationMode.Metadata,IgnoreReadOnlyFields=true)]internalpartialclassMyObjectSerializationContext:JsonSerializerContext{}publicclassMyObject{[JsonPropertyName("test")]publicstring?Test{get;set;}[JsonIgnore]privatereadonlyComplexObject?_data;// Metadata generated for this type. Why?!?}publicclassComplexObject{privateprotectedobject_thisLock=newobject();// This causes code gen problem}
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
1>E:\sources.tmp\TrimTest\ClassLibrary1\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyObjectSerializationContext.ComplexObject.g.cs(58,85,58,94): error CS0122: 'ComplexObject._thisLock' is inaccessible due to its protection level
1>E:\sources.tmp\TrimTest\ClassLibrary1\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyObjectSerializationContext.ComplexObject.g.cs(59,92,59,101): error CS0122: 'ComplexObject._thisLock' is inaccessible due to its protection level
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.
Activity
eiriktsarpalis commentedon Oct 12, 2022
Thanks for opening the issue. It looks like @krwq beat you to it and opened another issue based on your twitter report:
#76919
With that, I'm going close this one as a duplicate. Feel free to continue the conversation in that other thread.
dotMorten commentedon Oct 12, 2022
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 :-)