Skip to content

Use correct path for NewtonsoftJson ModelState errors #39058

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 2 commits into from
Jan 6, 2022
Merged

Conversation

halter73
Copy link
Member

Use correct path for NewtonsoftJson ModelState errors

The NewtonsoftJsonInputFormatter has logic that's intended to append the names of missing required properties to the ModelStateDictionary key. Normally, just the ModelName and ErrorContext.Path is used for this key, but ErrorContext.Path does not include the missing required property name like we want it to. For example, given the following class and input missing the required "Name" property:

class Person
{
    [JsonProperty(Required = Required.Always)]
    public string Name { get; set; }
}

We will see the following ErrorContext:

Error   {"Required property 'Name' not found in JSON. Path 'Person'..."} System.Exception {Newtonsoft.Json.JsonSerializationException}
Member  "Name"   object {string}
Path    "Person" string

So the path used for the ModelStateDictionary key is updated to be "Person.Name" instead of just "Person".
See aspnet/Mvc#8509

However, this logic is caused problems with different JSON deserilization errors that include the member in the path but not at the end. For example, given the following classes and invalid input like { "b": { "c": { "d": abc } } }:

class A
{
    public B B { get; set; }
}
class B 
{
    public C C { get; set; }
}
class C 
{
    public string D { get; set; }
}

We will see the following ErrorContext:

Error   {"Unexpected character encountered while parsing value: b. Path 'b.c.d'..."} System.Exception {Newtonsoft.Json.JsonReaderException}
Member  "c"     object {string}
Path    "b.c.d" string

Notice that Member "c" is in the middle of the Path "b.c.d". The error handler gets invoked for each level of nesting. null, "b", "c" and "d" are each a Member in different ErrorContexts all reporting the same parsing error.

The parsing error is reported as a JsonReaderException instead of as a JsonSerializationException like for missing required properties. We use the exception type to filter out these errors and keep the path used for the ModelStateDictionary key as "b.c.d" instead of "b.c.d.c" like before.

Fixes #33451

@halter73 halter73 added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Dec 15, 2021
@halter73 halter73 requested a review from javiercn as a code owner December 15, 2021 18:44
@halter73
Copy link
Member Author

I created a new issue for the single quote escaping and linked to it from the skipped test. I hoping to get this approved today since I'm OOF starting tomorrow.

@halter73 halter73 enabled auto-merge (squash) December 17, 2021 20:01
Copy link
Contributor

@pranavkm pranavkm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the very detailed PR description!

@halter73 halter73 merged commit 962f55e into main Jan 6, 2022
@halter73 halter73 deleted the halter73/33451 branch January 6, 2022 19:17
@ghost ghost added this to the 7.0-preview1 milestone Jan 6, 2022
@TanayParikh TanayParikh mentioned this pull request Jan 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ModelState errors reported by Microsoft.AspNetCore.Mvc.NewtonsoftJson
5 participants