diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.cs index c6de4fba0e1a0b..4323ebab4b6e1c 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.cs @@ -628,8 +628,8 @@ protected static bool TryLookupConstructorParameter( out bool useExtensionProperty, createExtensionProperty: false); - // Mark the property as read from the payload if required. - if (!useExtensionProperty) + // Mark the property as read from the payload if it is mapped to a non-extension member. + if (!useExtensionProperty && jsonPropertyInfo != JsonPropertyInfo.s_missingProperty) { state.Current.MarkPropertyAsRead(jsonPropertyInfo); } 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 86d8119afb121a..6fd5c9735f41ad 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 @@ -1611,7 +1611,7 @@ public async Task TestClassWithManyDefaultParams() Class_With_Parameters_Default_Values result = await Serializer.DeserializeWrapper(json); result.Verify(); } - + [Fact] public async Task TestClassWithCustomConverterOnCtorParameter_ShouldPassCorrectTypeToConvertParameter() { @@ -1821,5 +1821,25 @@ public record class Class_ManyParameters_ExtraProperty_ExtData(int P0, int P1, i [JsonExtensionData] public Dictionary ExtensionData { get; set; } } + + [Fact] + public async Task RequiredMemberWithUnmappedMember() + { + // https://github.com/dotnet/runtime/issues/116801 + string json = """ + { + "Bar": "asdf", + "Baz": "hello" + } + """; + + ClassWithRequiredProperty obj = await Serializer.DeserializeWrapper(json); + Assert.Equal("asdf", obj.Bar); + } + + public class ClassWithRequiredProperty + { + public required string? Bar { get; set; } + } } } 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 12a65d54bf8bb7..0a4c1a21f19fde 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 @@ -158,6 +158,7 @@ protected ConstructorTests_Metadata(JsonSerializerWrapper stringWrapper) [JsonSerializable(typeof(Class_ExtraProperty_ExtData))] [JsonSerializable(typeof(Class_ExtraProperty_JsonElementDictionaryExtData))] [JsonSerializable(typeof(Class_ManyParameters_ExtraProperty_ExtData))] + [JsonSerializable(typeof(ClassWithRequiredProperty))] internal sealed partial class ConstructorTestsContext_Metadata : JsonSerializerContext { } @@ -311,6 +312,7 @@ public ConstructorTests_Default(JsonSerializerWrapper jsonSerializer) : base(jso [JsonSerializable(typeof(Class_ExtraProperty_ExtData))] [JsonSerializable(typeof(Class_ExtraProperty_JsonElementDictionaryExtData))] [JsonSerializable(typeof(Class_ManyParameters_ExtraProperty_ExtData))] + [JsonSerializable(typeof(ClassWithRequiredProperty))] internal sealed partial class ConstructorTestsContext_Default : JsonSerializerContext { }