Skip to content

System.Text.Json code generator creates metadata for classes that are explicitly ignored #76937

Closed

Description

@dotMorten

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:

    [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 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.

image

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

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Oct 12, 2022
  2. eiriktsarpalis commented on Oct 12, 2022

    @eiriktsarpalis
    Member

    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.

  3. ghost removed
    untriagedNew issue has not been triaged by the area owner
    on Oct 12, 2022
  4. dotMorten commented on Oct 12, 2022

    @dotMorten
    Author

    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 :-)

  5. ghost locked as resolved and limited conversation to collaborators on Nov 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions