Skip to content

[release/10.0-preview6] JSON: Don't mark unmapped properties as read #116828

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

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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1611,7 +1611,7 @@ public async Task TestClassWithManyDefaultParams()
Class_With_Parameters_Default_Values result = await Serializer.DeserializeWrapper<Class_With_Parameters_Default_Values>(json);
result.Verify();
}

[Fact]
public async Task TestClassWithCustomConverterOnCtorParameter_ShouldPassCorrectTypeToConvertParameter()
{
Expand Down Expand Up @@ -1821,5 +1821,25 @@ public record class Class_ManyParameters_ExtraProperty_ExtData(int P0, int P1, i
[JsonExtensionData]
public Dictionary<string, object> 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<ClassWithRequiredProperty>(json);
Assert.Equal("asdf", obj.Bar);
}

public class ClassWithRequiredProperty
{
public required string? Bar { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
Expand Down Expand Up @@ -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
{
}
Expand Down
Loading