Skip to content
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

Closed
dotMorten opened this issue Oct 12, 2022 · 3 comments

Comments

@dotMorten
Copy link

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

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Oct 12, 2022
@ghost
Copy link

ghost commented Oct 12, 2022

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

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

Author: dotMorten
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@eiriktsarpalis
Copy link
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.

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Oct 12, 2022
@dotMorten
Copy link
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 :-)

@ghost ghost locked as resolved and limited conversation to collaborators Nov 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants