Skip to content

Commit 3d448d1

Browse files
[release/9.0-staging] Fix regression in constructor parameter binding logic. (#109813)
* Fix regression in constructor parameter binding logic. * Update servicing version --------- Co-authored-by: Eirik Tsarpalis <[email protected]>
1 parent ba72b6d commit 3d448d1

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

src/libraries/System.Text.Json/src/System.Text.Json.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<IncludeInternalObsoleteAttribute>true</IncludeInternalObsoleteAttribute>
1010
<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile>
1111
<IsPackable>true</IsPackable>
12+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
13+
<ServicingVersion>1</ServicingVersion>
1214
<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.
1315

1416
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>

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,8 @@ internal void ConfigureConstructorParameters()
12091209
continue;
12101210
}
12111211

1212-
ParameterLookupKey paramKey = new(propertyInfo.PropertyType, propertyInfo.Name);
1212+
string propertyName = propertyInfo.MemberName ?? propertyInfo.Name;
1213+
ParameterLookupKey paramKey = new(propertyInfo.PropertyType, propertyName);
12131214
if (!parameterIndex.TryAdd(paramKey, parameterInfo))
12141215
{
12151216
// Multiple object properties cannot bind to the same constructor parameter.

src/libraries/System.Text.Json/tests/Common/ConstructorTests/ConstructorTests.ParameterMatching.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,5 +1668,32 @@ public async Task RespectRequiredConstructorParameters_NoParameterMissing_Succee
16681668
Assert.Equal(2, result.Y);
16691669
Assert.Equal(3, result.Z);
16701670
}
1671+
1672+
[Fact]
1673+
public async Task ClassWithConflictingCaseInsensitiveProperties_Succeeds_When_CaseSensitive()
1674+
{
1675+
// Regression test for https://github.com/dotnet/runtime/issues/109768
1676+
1677+
string json = """{"a": "lower", "A": "upper"}""";
1678+
ClassWithConflictingCaseInsensitiveProperties result = await Serializer.DeserializeWrapper<ClassWithConflictingCaseInsensitiveProperties>(json);
1679+
Assert.Equal("lower", result.From);
1680+
Assert.Equal("upper", result.To);
1681+
}
1682+
1683+
public class ClassWithConflictingCaseInsensitiveProperties
1684+
{
1685+
[JsonPropertyName("a")]
1686+
public string From { get; set; }
1687+
1688+
[JsonPropertyName("A")]
1689+
public string To { get; set; }
1690+
1691+
[JsonConstructor]
1692+
public ClassWithConflictingCaseInsensitiveProperties(string from, string to)
1693+
{
1694+
From = from;
1695+
To = to;
1696+
}
1697+
}
16711698
}
16721699
}

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/Serialization/ConstructorTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ protected ConstructorTests_Metadata(JsonSerializerWrapper stringWrapper)
154154
[JsonSerializable(typeof(TypeWithEnumParameters))]
155155
[JsonSerializable(typeof(ClassWithIgnoredPropertyDefaultParam))]
156156
[JsonSerializable(typeof(ClassWithCustomConverterOnCtorParameter))]
157+
[JsonSerializable(typeof(ClassWithConflictingCaseInsensitiveProperties))]
157158
internal sealed partial class ConstructorTestsContext_Metadata : JsonSerializerContext
158159
{
159160
}
@@ -303,6 +304,7 @@ public ConstructorTests_Default(JsonSerializerWrapper jsonSerializer) : base(jso
303304
[JsonSerializable(typeof(TypeWithEnumParameters))]
304305
[JsonSerializable(typeof(ClassWithIgnoredPropertyDefaultParam))]
305306
[JsonSerializable(typeof(ClassWithCustomConverterOnCtorParameter))]
307+
[JsonSerializable(typeof(ClassWithConflictingCaseInsensitiveProperties))]
306308
internal sealed partial class ConstructorTestsContext_Default : JsonSerializerContext
307309
{
308310
}

0 commit comments

Comments
 (0)