Skip to content

Commit c585026

Browse files
Only mark mapped properties (#116807)
1 parent ffa6be5 commit c585026

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,8 @@ protected static bool TryLookupConstructorParameter(
628628
out bool useExtensionProperty,
629629
createExtensionProperty: false);
630630

631-
// Mark the property as read from the payload if required.
632-
if (!useExtensionProperty)
631+
// Mark the property as read from the payload if it is mapped to a non-extension member.
632+
if (!useExtensionProperty && jsonPropertyInfo != JsonPropertyInfo.s_missingProperty)
633633
{
634634
state.Current.MarkPropertyAsRead(jsonPropertyInfo);
635635
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1611,7 +1611,7 @@ public async Task TestClassWithManyDefaultParams()
16111611
Class_With_Parameters_Default_Values result = await Serializer.DeserializeWrapper<Class_With_Parameters_Default_Values>(json);
16121612
result.Verify();
16131613
}
1614-
1614+
16151615
[Fact]
16161616
public async Task TestClassWithCustomConverterOnCtorParameter_ShouldPassCorrectTypeToConvertParameter()
16171617
{
@@ -1821,5 +1821,25 @@ public record class Class_ManyParameters_ExtraProperty_ExtData(int P0, int P1, i
18211821
[JsonExtensionData]
18221822
public Dictionary<string, object> ExtensionData { get; set; }
18231823
}
1824+
1825+
[Fact]
1826+
public async Task RequiredMemberWithUnmappedMember()
1827+
{
1828+
// https://github.com/dotnet/runtime/issues/116801
1829+
string json = """
1830+
{
1831+
"Bar": "asdf",
1832+
"Baz": "hello"
1833+
}
1834+
""";
1835+
1836+
ClassWithRequiredProperty obj = await Serializer.DeserializeWrapper<ClassWithRequiredProperty>(json);
1837+
Assert.Equal("asdf", obj.Bar);
1838+
}
1839+
1840+
public class ClassWithRequiredProperty
1841+
{
1842+
public required string? Bar { get; set; }
1843+
}
18241844
}
18251845
}

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
@@ -158,6 +158,7 @@ protected ConstructorTests_Metadata(JsonSerializerWrapper stringWrapper)
158158
[JsonSerializable(typeof(Class_ExtraProperty_ExtData))]
159159
[JsonSerializable(typeof(Class_ExtraProperty_JsonElementDictionaryExtData))]
160160
[JsonSerializable(typeof(Class_ManyParameters_ExtraProperty_ExtData))]
161+
[JsonSerializable(typeof(ClassWithRequiredProperty))]
161162
internal sealed partial class ConstructorTestsContext_Metadata : JsonSerializerContext
162163
{
163164
}
@@ -311,6 +312,7 @@ public ConstructorTests_Default(JsonSerializerWrapper jsonSerializer) : base(jso
311312
[JsonSerializable(typeof(Class_ExtraProperty_ExtData))]
312313
[JsonSerializable(typeof(Class_ExtraProperty_JsonElementDictionaryExtData))]
313314
[JsonSerializable(typeof(Class_ManyParameters_ExtraProperty_ExtData))]
315+
[JsonSerializable(typeof(ClassWithRequiredProperty))]
314316
internal sealed partial class ConstructorTestsContext_Default : JsonSerializerContext
315317
{
316318
}

0 commit comments

Comments
 (0)