diff --git a/src/libraries/System.Text.Json/src/System.Text.Json.csproj b/src/libraries/System.Text.Json/src/System.Text.Json.csproj index 8d0da4abcfc9a..707a643b24c46 100644 --- a/src/libraries/System.Text.Json/src/System.Text.Json.csproj +++ b/src/libraries/System.Text.Json/src/System.Text.Json.csproj @@ -9,6 +9,8 @@ true false true + true + 1 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. diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs index 00c4245a4c87b..bd3e3d9241f85 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs @@ -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. diff --git a/src/libraries/System.Text.Json/tests/Common/ConstructorTests/ConstructorTests.ParameterMatching.cs b/src/libraries/System.Text.Json/tests/Common/ConstructorTests/ConstructorTests.ParameterMatching.cs index e134d42286de9..d46253c65761e 100644 --- a/src/libraries/System.Text.Json/tests/Common/ConstructorTests/ConstructorTests.ParameterMatching.cs +++ b/src/libraries/System.Text.Json/tests/Common/ConstructorTests/ConstructorTests.ParameterMatching.cs @@ -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(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; + } + } } } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/Serialization/ConstructorTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/Serialization/ConstructorTests.cs index 1f2fe901f456e..06ff3b6e39bb1 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/Serialization/ConstructorTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/Serialization/ConstructorTests.cs @@ -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 { } @@ -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 { }