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

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

Open
Tracked by #73255
tanveerbadar opened this issue Feb 18, 2024 · 2 comments
Open
Tracked by #73255

Comments

@tanveerbadar
Copy link

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

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Feb 18, 2024
@ghost
Copy link

ghost commented Feb 18, 2024

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

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

Author: tanveerbadar
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@tanveerbadar tanveerbadar changed the title STJ Source Generator Generates invalid code in some cases STJ Source Generator generates invalid code in some cases Feb 18, 2024
@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Feb 19, 2024

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

@eiriktsarpalis eiriktsarpalis added bug and removed untriaged New issue has not been triaged by the area owner labels Feb 19, 2024
@eiriktsarpalis eiriktsarpalis added this to the Future milestone Feb 19, 2024
@eiriktsarpalis eiriktsarpalis changed the title STJ Source Generator generates invalid code in some cases STJ fast-path generator does not always deduplicate shadowed members correctly. Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants