Skip to content

Commit

Permalink
Fix bug in handling of duplicate properties in the fast-path source g…
Browse files Browse the repository at this point in the history
…enerator. (dotnet#105628)
  • Loading branch information
eiriktsarpalis authored Jul 29, 2024
1 parent f251b17 commit f2d2fac
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ private static void AddPropertyWithConflictResolution(
{
// Overwrite previously cached property since it has [JsonIgnore].
state.AddedProperties[propertySpec.EffectiveJsonPropertyName] = (propertySpec, memberInfo, index);
state.Properties[index] = state.Properties.Count;
state.Properties[index] = propertyIndex;
state.IsPropertyOrderSpecified |= propertySpec.Order != 0;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ namespace System.Text.Json.SourceGeneration.Tests
[JsonSerializable(typeof(PolymorphicClass))]
[JsonSerializable(typeof(PocoWithNumberHandlingAttr))]
[JsonSerializable(typeof(PocoWithMixedVisibilityMembers))]
[JsonSerializable(typeof(ClassWithConflictingIgnoredProperties))]
internal partial class SerializationContext : JsonSerializerContext, ITestContext
{
public JsonSourceGenerationMode JsonSourceGenerationMode => JsonSourceGenerationMode.Serialization;
Expand Down Expand Up @@ -556,6 +557,31 @@ public override void NullableStruct()

Assert.Throws<InvalidOperationException>(() => JsonSerializer.Deserialize(json, DefaultContext.NullablePersonStruct));
}

[Fact]
public void ClassWithConflictingIgnoredProperties_FastPathSerialization()
{
// Regression test for https://github.com/dotnet/runtime/issues/99988

ClassWithConflictingIgnoredProperties value = new()
{
SystemTextJsonUserList = ["root"],
SystemTextJsonUserGroupsList = ["wheel"],
SystemTextJsonIPAddresses = ["127.0.0.1"],
SystemTextJsonQueryParams = ["key=value"],
};

string json = JsonSerializer.Serialize(value, SerializationContext.Default.ClassWithConflictingIgnoredProperties);

JsonTestHelper.AssertJsonEqual("""
{
"userlist" : ["root"],
"usergroupslist" : ["wheel"],
"SystemTextJsonIPAddresses" : ["127.0.0.1"],
"queryparams" : ["key=value"]
}
""", json);
}
}

public sealed class SerializationWithPerTypeAttributeContextTests : SerializationContextTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,28 @@ public class PocoWithMixedVisibilityMembers : PocoWithMixedVisibilityMembersBase

public new int ShadowProperty { get; set; }
}

public sealed class ClassWithConflictingIgnoredProperties
{
[JsonIgnore]
public List<string>? UserList { get; set; }

[JsonPropertyName("userlist")]
public List<string>? SystemTextJsonUserList { get; set; }

[JsonIgnore]
public List<string>? UserGroupsList { get; set; }

[JsonPropertyName("usergroupslist")]
public List<string>? SystemTextJsonUserGroupsList { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public List<string>? SystemTextJsonIPAddresses { get; set; }

[JsonIgnore]
public List<object>? QueryParams { get; set; }

[JsonPropertyName("queryparams")]
public List<object>? SystemTextJsonQueryParams { get; set; }
}
}

0 comments on commit f2d2fac

Please sign in to comment.