Skip to content

Source generator in System.Text.Json does not handle shadowed members correctly #97621

Closed

Description

@garryxiao

Description

C# 12, source generator failed for 'new' or 'override' properties.

Reproduction Steps

public record PlaceBase
{
    public string? Region { get; init; }
}
public record PlaceCommon : PlaceBase
{
    public new required string Region { get; init; }
}

[JsonSerializable(typeof(PlaceBase))]
[JsonSerializable(typeof(PlaceCommon))]
public partial class ApiModelJsonSerializerContext : JsonSerializerContext
{
}

Expected behavior

PlaceCommon's 'Region' should be generated with its own Place property and ignore PlaceBase's 'Region' property.

Actual behavior

Double 'Region' property generated with "Error CS1912 Duplicate initialization of member 'Region‘"

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. ghost added
    untriaged
    on Jan 28, 2024
  2. garryxiao commented on Jan 28, 2024

    @garryxiao
    Author

    Luckly there is a intesting way to overcome it, chang the line "public string? Region { get; init; }" to "public string? Region { get; set; }", it works again.

  3. added a commit that references this issue on Jan 28, 2024
  4. eiriktsarpalis commented on Jan 29, 2024

    @eiriktsarpalis
    Member

    Presumably you meant to have PlaceCommon : PlaceBase? I can reproduce the issue.

  5. removed
    untriaged
    on Jan 29, 2024
  6. garryxiao commented on Jan 29, 2024

    @garryxiao
    Author

    Presumably you meant to have PlaceCommon : PlaceBase? I can reproduce the issue.

    Sorry, Yes. :)

  7. added this to the Future milestone on Jan 29, 2024
  8. added
    source-generator
    on Jan 29, 2024
  9. eiriktsarpalis commented on Jan 29, 2024

    @eiriktsarpalis
    Member

    Luckly there is a intesting way to overcome it, chang the line "public string? Region { get; init; }" to "public string? Region { get; set; }", it works again.

    Yes, this is related to how required and/or init properties are being handled by the source generator. Removing either should result in the duplicate code not being generated.

  10. changed the title [-]Source generator in System.Text.Json fails due to nullability override of property in inheritance[/-] [+]Source generator in System.Text.Json does not handle shadowed members correctly[/+] on Jan 29, 2024
  11. ghost added
    in-pr
    on Jan 30, 2024
  12. ghost removed
    in-pr
    on Feb 5, 2024
  13. modified the milestones: Future, 9.0.0 on Feb 5, 2024
  14. locked and limited conversation to collaborators on Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.Jsonbugsource-generator

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions