Skip to content

System.Text.Json source generator does not ignore delegate properties #62354

Closed

Description

@mphilipp622

Description

When using the dotnet 6 System.Text.Json source generator on a class or record that contains a delegate type (Func, Action), a CS0149 Method Name Expected error is thrown from the generated g.cs file. This happens even if using the [JsonIgnore] property.

Reproduction Steps

Create a dotnet 6 project and create a new class/record with the source generator. The following code will reproduce the problem minimally:

public record TestRecord(string Test, Func<string, bool> Callback);

[JsonSerializable(typeof(TestRecord))]
public partial class TestRecordGenerationContext : JsonSerializerContext { }

Expected behavior

The expected behavior is that the g.cs files are generated without error and the [JsonIgnore] attribute is properly used for delegate types.

Actual behavior

Currently, the g.cs files are generated but throw a CS0149 Method Name Expected error.

Regression?

No response

Known Workarounds

Only workaround I've used so far is to completely remove the delegate types as member properties or member variables from the class or converting them to a method (if possible for your requirements) so the source generator ignores it.

Configuration

  • Which version of .NET is the code running on? dotnet 6.0
  • What OS and version, and what distro if applicable? Windows 10 64-bit
  • What is the architecture (x64, x86, ARM, ARM64)? x64
  • Do you know whether it is specific to that configuration? Unsure
  • If you're using Blazor, which web browser(s) do you see this issue in? I am using Blazor, but this isn't a runtime issue

Other information

For where the issue might be, perhaps the source generator does properly ignore the member for the generated serialization code, but it doesn't ignore it when traversing the member hierarchy to determine which classes need serialization code generated. Not sure.

Activity

  1. ghost added
    untriaged
    on Dec 3, 2021
  2. eiriktsarpalis commented on Dec 6, 2021

    @eiriktsarpalis
    Member

    Minimal repro:

    public class TestClass
    {
        [JsonIgnore] 
        Func<string, bool> Callback { get; set; }
    }
    
    [JsonSerializable(typeof(TestClass))]
    public partial class TestRecordGenerationContext : JsonSerializerContext { }

    The source generator appears to be trying to deserialize delegates using the Func(System.Object, IntPtr) constructor:

    ObjectWithParameterizedConstructorCreator = static (args) => new global::System.Func<global::System.String, global::System.Boolean>((global::System.Object)args[0], (global::System.IntPtr)args[1]),

    which is what produces the compiler error. Adding JsonIgnoreAttribute doesn't appear to suppress generation from the time. This suggests to me that this is a bug we might want to service in 6.

  3. added and removed
    untriaged
    on Dec 6, 2021
  4. added this to the 6.0.x milestone on Dec 6, 2021
  5. ericstj commented on Dec 6, 2021

    @ericstj
    Member

    This works with the runtime-serializer (no source gen), so that's a workaround. It seems that both metadata and fast-path generator produce the bad code.

    Honoring JsonIgnore here seems like a good safety mechanism. Folks should have this as a tool to workaround cases where we can't handle properties. It's similar reasoning to why we took 784687f.

    I'd also like us to look at why the member discovery is different between source-gen and runtime here, since even without JsonIgnore source gen tries to serialize this private property.

  6. fbrosseau commented on Dec 31, 2021

    @fbrosseau

    Even when ignored types/properties do generate valid code, it results in very bloated deadcode that ends up in the final assembly, for instance I just encountered a JsonIgnored' X509Certificate2, which apparently can be source-generated fine, but comes with an enormous tree of deadcode for all of the various properties that that type contains. Plus the obsolete warnings due to some obsolete attributes somewhere in there. (bug #62076)

  7. ghost added
    in-pr
    on Jan 7, 2022
  8. layomia commented on Jan 7, 2022

    @layomia
    Contributor

    I opened a fix for this issue - #63495.

    I'd also like us to look at why the member discovery is different between source-gen and runtime here, since even without JsonIgnore source gen tries to serialize this private property.

    Source gen doesn't try to serialize the property at runtime, but includes/generates metadata for the property to be inspected by the serializer at runtime so that it can handle things like property name collisions due to runtime-specified settings such as options.PropertyNamingPolicy.

    Even when ignored types/properties do generate valid code, it results in very bloated deadcode that ends up in the final assembly, for instance I just encountered a JsonIgnored' X509Certificate2, which apparently can be source-generated fine, but comes with an enormous tree of deadcode for all of the various properties that that type contains.

    @fbrosseau as mentioned right above, the current strategy is to generate metadata for ignored properties and their corresponding data types since this information might be needed at runtime.

    Can you create a separate issue for your scenario? In 7.0/future, we might be able to move more of the property resolution phase of the serializer to compile time and possibly generate less code for ignored members.

  9. ghost removed
    in-pr
    on Jan 7, 2022
  10. layomia commented on Jan 8, 2022

    @layomia
    Contributor

    Reopening to consider for 6.0 servicing - #63514.

  11. reopened this on Jan 8, 2022
  12. layomia commented on Jan 10, 2022

    @layomia
    Contributor

    Fixed for 6.0.2 in #63514.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions