Skip to content

STJ fast-path generator does not always deduplicate shadowed members correctly. #98634

Closed

Description

@tanveerbadar

Description

System.Text.Json source generator is generating code which fails to compile if the following conditions are met:

  • using polymorphic serialization
  • [JsonSourceGenerationOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
  • same property name is present in at least two types in an inheritance hierarchy
  • their json property names differ, by having different [JsonPropertyName] attributes applied or customized in only some of the types.

Reproduction Steps

Following code demonstrates the problem

using System.Text.Json.Serialization;

[JsonPolymorphic]
[JsonDerivedType(typeof(DerivedType))]
partial class BaseType
{
    [JsonPropertyName("MoreData")]
    public string MoreData { get; set; }
}

partial class DerivedType : BaseType
{
    [JsonPropertyName("MoreData2")]
    public new string MoreData { get; set; }
}

[JsonSerializable(typeof(BaseType))]
[JsonSourceGenerationOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
partial class MyContext : JsonSerializerContext
{
}

Expected behavior

Generated code should have no compilation errors.

Actual behavior

Generated code has 2 locals with the same name.

    // Intentionally not a static method because we create a delegate to it. Invoking delegates to instance
    // methods is almost as fast as virtual calls. Static methods need to go through a shuffle thunk.
    private void DerivedTypeSerializeHandler(global::System.Text.Json.Utf8JsonWriter writer, global::DerivedType? value)
    {
        if (value == null)
        {
            writer.WriteNullValue();
            return;
        }
        
        writer.WriteStartObject();

        string __value_MoreData = ((global::DerivedType)value).MoreData;
        if (__value_MoreData != null)
        {
            writer.WriteString(PropName_MoreData2, __value_MoreData);
        }
        string __value_MoreData = ((global::BaseType)value).MoreData; // Error in this line.
        if (__value_MoreData != null)
        {
            writer.WriteString(PropName_MoreData, __value_MoreData);
        }

        writer.WriteEndObject();
    }

Regression?

Not sure

Known Workarounds

No response

Configuration

Using .net 8, Windows 10, x64.

Other information

No response

Activity

  1. ghost added
    untriaged
    on Feb 18, 2024
  2. changed the title [-]STJ Source Generator Generates invalid code in some cases[/-] [+]STJ Source Generator generates invalid code in some cases[/+] on Feb 18, 2024
  3. eiriktsarpalis commented on Feb 19, 2024

    @eiriktsarpalis
    Member

    This isn't related to polymorphism per se, here's a minimal reproduction:

    partial class BaseType
    {
        public string? MoreData { get; set; }
    }
    
    partial class DerivedType : BaseType
    {
        [JsonPropertyName("MoreData2")]
        public new string? MoreData { get; set; }
    }
    
    [JsonSerializable(typeof(DerivedType))]
    [JsonSourceGenerationOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
    partial class MyContext : JsonSerializerContext;

    TL;DR the fast-path serializer isn't correctly deduplicating shadowed members when the shadowing type uses a custom property name.

    Related to #97621

  4. added and removed
    untriaged
    on Feb 19, 2024
  5. added this to the Future milestone on Feb 19, 2024
  6. changed the title [-]STJ Source Generator generates invalid code in some cases[/-] [+]STJ fast-path generator does not always deduplicate shadowed members correctly.[/+] on Feb 19, 2024
  7. added
    in-pr
    on Nov 2, 2026
  8. added a commit that references this issue on Nov 4, 2026
    03ae64b
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.Jsonbugin-pr

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions