Skip to content

Commit

Permalink
Fix regression in constructor parameter binding logic. (#109786)
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriktsarpalis authored Nov 14, 2024
1 parent 1c4c009 commit d3f4de1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,8 @@ internal void ConfigureConstructorParameters()
continue;
}

ParameterLookupKey paramKey = new(propertyInfo.PropertyType, propertyInfo.Name);
string propertyName = propertyInfo.MemberName ?? propertyInfo.Name;
ParameterLookupKey paramKey = new(propertyInfo.PropertyType, propertyName);
if (!parameterIndex.TryAdd(paramKey, parameterInfo))
{
// Multiple object properties cannot bind to the same constructor parameter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1668,5 +1668,32 @@ public async Task RespectRequiredConstructorParameters_NoParameterMissing_Succee
Assert.Equal(2, result.Y);
Assert.Equal(3, result.Z);
}

[Fact]
public async Task ClassWithConflictingCaseInsensitiveProperties_Succeeds_When_CaseSensitive()
{
// Regression test for https://github.com/dotnet/runtime/issues/109768

string json = """{"a": "lower", "A": "upper"}""";
ClassWithConflictingCaseInsensitiveProperties result = await Serializer.DeserializeWrapper<ClassWithConflictingCaseInsensitiveProperties>(json);
Assert.Equal("lower", result.From);
Assert.Equal("upper", result.To);
}

public class ClassWithConflictingCaseInsensitiveProperties
{
[JsonPropertyName("a")]
public string From { get; set; }

[JsonPropertyName("A")]
public string To { get; set; }

[JsonConstructor]
public ClassWithConflictingCaseInsensitiveProperties(string from, string to)
{
From = from;
To = to;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ protected ConstructorTests_Metadata(JsonSerializerWrapper stringWrapper)
[JsonSerializable(typeof(TypeWithEnumParameters))]
[JsonSerializable(typeof(ClassWithIgnoredPropertyDefaultParam))]
[JsonSerializable(typeof(ClassWithCustomConverterOnCtorParameter))]
[JsonSerializable(typeof(ClassWithConflictingCaseInsensitiveProperties))]
internal sealed partial class ConstructorTestsContext_Metadata : JsonSerializerContext
{
}
Expand Down Expand Up @@ -303,6 +304,7 @@ public ConstructorTests_Default(JsonSerializerWrapper jsonSerializer) : base(jso
[JsonSerializable(typeof(TypeWithEnumParameters))]
[JsonSerializable(typeof(ClassWithIgnoredPropertyDefaultParam))]
[JsonSerializable(typeof(ClassWithCustomConverterOnCtorParameter))]
[JsonSerializable(typeof(ClassWithConflictingCaseInsensitiveProperties))]
internal sealed partial class ConstructorTestsContext_Default : JsonSerializerContext
{
}
Expand Down

0 comments on commit d3f4de1

Please sign in to comment.