Skip to content

Handle JsonExceptions in RequestDelegateFactory #36589

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
Sep 17, 2021
Merged

Handle JsonExceptions in RequestDelegateFactory #36589

merged 2 commits into from
Sep 17, 2021

Conversation

halter73
Copy link
Member

@halter73 halter73 commented Sep 16, 2021

This change causes minimal API route handlers to respond with a "400 Bad Request" instead of "500 Internal Server Error" when given a request body with an application/json Content-Type but malformed JSON.

Before, we expected this to throw an InvalidDataException but malformed JSON results in a JsonException instead.

I plan to backport this to release/6.0-rc2.

Fixes #36499

@Tratcher
Copy link
Member

InvalidDataException is what's thrown by the form parser. Was this accidentally copied from that code path?

@rafikiassumani-msft
Copy link
Contributor

@halter73 are you returning 400 to be consistent with MVC? Any reason why we are not returning 422 ?

@halter73
Copy link
Member Author

@halter73 are you returning 400 to be consistent with MVC? Any reason why we are not returning 422 ?

I mostly did this to be consistent with MVC and our current InvalidDataException behavior.

Also, the rfc claims

The 422 (Unprocessable Entity) status code means the server
understands the content type of the request entity (hence a
415(Unsupported Media Type) status code is inappropriate), and the
syntax of the request entity is correct (thus a 400 (Bad Request)
status code is inappropriate) but was unable to process the contained
instructions. For example, this error condition may occur if an XML
request body contains well-formed (i.e., syntactically correct), but
semantically erroneous, XML instructions.

And I think JsonExceptions indicate the JSON is not "well-formed". A 422 would be more appropriate if the request was missing a required property or something like that (assuming that was something we supported).

@halter73 halter73 changed the title Handle JsonExceptions like InvalidDataExceptions in RequestDelegateFactory Handle JsonExceptions in RequestDelegateFactory Sep 16, 2021
@halter73
Copy link
Member Author

/backport to release/6.0-rc2

@github-actions
Copy link
Contributor

Started backporting to release/6.0-rc2: https://github.com/dotnet/aspnetcore/actions/runs/1243143334

@github-actions
Copy link
Contributor

@halter73 backporting to release/6.0-rc2 failed, the patch most likely resulted in conflicts:

$ git am --3way --ignore-whitespace --keep-non-patch changes.patch

Applying: Handle JsonExceptions like InvalidDataExceptions in RequestDelegateFactory
Using index info to reconstruct a base tree...
M	src/Http/Http.Extensions/src/RequestDelegateFactory.cs
M	src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs
Falling back to patching base and 3-way merge...
Auto-merging src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs
Auto-merging src/Http/Http.Extensions/src/RequestDelegateFactory.cs
Applying: Don't catch InvalidDataExceptions when reading JSON
Using index info to reconstruct a base tree...
M	src/Http/Http.Extensions/src/RequestDelegateFactory.cs
M	src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs
Falling back to patching base and 3-way merge...
Auto-merging src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs
CONFLICT (content): Merge conflict in src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs
Auto-merging src/Http/Http.Extensions/src/RequestDelegateFactory.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0002 Don't catch InvalidDataExceptions when reading JSON
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

@halter73
Copy link
Member Author

@captainsafia Can you approve this PR too? Thanks!

@halter73 halter73 merged commit a94189e into main Sep 17, 2021
@halter73 halter73 deleted the halter73/36499 branch September 17, 2021 16:48
@ghost ghost modified the milestones: 6.0-rc2, 7.0-preview1 Sep 17, 2021
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Route handlers throw System.Text.Json.JsonReader exception for requests with invalid JSON body rather than returning 422
7 participants