[release/10.0-preview6] JSON: Don't mark unmapped properties as read #116828
+25
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Backport of #116807 to release/10.0-preview6
/cc @PranavSenthilnathan
Customer Impact
Found in #116801 and blocking dotnet/aspnetcore#53467 / dotnet/aspnetcore#62112 (passkey support for ASP.NET). If a class with a constructor and/or required properties is being deserialized into and the JSON payload contains an unmapped property, then there will be a
NullReferenceException
. Here is the reported example:Regression
Yes, this was introduced as part a new feature but part of the changes in that PR regressed old behavior. Introduced in #115856.
During deserialization we keep track of the properties that are read in order to do duplicate detection and required property validation. When a parametrized constructor is specified for deserialization, there are four types of JSON properties:
(The last two cases are only differentiated by whether the class has a property with the ExtensionData attribute.)
#115856 added duplicate detection by keeping track of properties that were assigned. Paths to handle cases 1, 2 and 3 were added but case 4 was an edge case that was not considered. So when this case was hit we would try to find the mapping like we do for case 1 or 2 when we actually don't have one. This PR fixes this case by explicitly checking for it and skipping duplicate detection for it.
Testing
The example in the reported issue was added as a test case and now passes with the new fix. This tests both source generation and reflection modes. The reported issue is linked in the test case.
Risk
Low, the fix is essentially preventing a
NullReferenceException
by checking that the object is a valid to dereference.