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

[release/9.0-staging] Fix regression in constructor parameter binding logic. #109813

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/libraries/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<IncludeInternalObsoleteAttribute>true</IncludeInternalObsoleteAttribute>
<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile>
<IsPackable>true</IsPackable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<ServicingVersion>1</ServicingVersion>
<PackageDescription>Provides high-performance and low-allocating types that serialize objects to JavaScript Object Notation (JSON) text and deserialize JSON text to objects, with UTF-8 support built-in. Also provides types to read and write JSON text encoded as UTF-8, and to create an in-memory document object model (DOM), that is read-only, for random access of the JSON elements within a structured view of the data.

The System.Text.Json library is built-in as part of the shared framework in .NET Runtime. The package can be installed when you need to use it in other target frameworks.</PackageDescription>
Expand Down
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
Loading