Skip to content

Return 400 on JSON parse failure for application/json#9784

Open
glen-84 wants to merge 4 commits into
mainfrom
gai/graphql-over-http-bcf8-latest-only
Open

Return 400 on JSON parse failure for application/json#9784
glen-84 wants to merge 4 commits into
mainfrom
gai/graphql-over-http-bcf8-latest-only

Conversation

@glen-84
Copy link
Copy Markdown
Member

@glen-84 glen-84 commented May 27, 2026

Summary

  • The graphql-over-http audit warning BCF8 ("SHOULD use 400 status code on JSON parsing failure when accepting application/json") fired because HotChocolate returned 200 instead of 400 when a request body is unparseable JSON and the client accepts application/json.
  • DefaultHttpResponseFormatter now honors a proposed 400 for the application/json response content-type on the latest transport (the §6.4.1.1.1 / §6.4.1.1.2 "cannot interpret the request" cases). Every other case, including field errors and unexpected server errors, stays 200 per §6.4.1. The Legacy transport keeps its pre-spec "always 200" behavior for backward compatibility.
  • HttpPostMiddlewareBase now distinguishes request-interpretation failures (invalid JSON, missing query) which propose 400 per §6.4.1.1.1, from GraphQL document syntax errors, which leave the status unset so application/json stays 200 (§6.4.1.1.3) and application/graphql-response+json stays 400.

Behavior matrix

Transport Accept Failure Status
Latest application/json JSON parse / invalid parameters 400 (was 200)
Latest application/json document parse / validation / coercion / field / unexpected error 200
Latest application/graphql-response+json any pre-exec 400
Legacy application/json any 200

Test plan

  • Full HotChocolate.AspNetCore.Tests on net10.0: 411 passed, 2 skipped, 0 failed.
  • Query_No_Body updated so only the Latest + application/json row asserts 400; all Legacy rows remain 200. ValidationError / ValidationError2 unchanged and green.

Copilot AI review requested due to automatic review settings May 27, 2026 08:15
@glen-84 glen-84 changed the title Return 400 on JSON parse failure for application/json (latest) Return 400 on JSON parse failure for application/json May 27, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates HotChocolate’s latest GraphQL-over-HTTP transport behavior to return HTTP 400 (instead of 200) when a request body cannot be parsed as JSON and the client accepts application/json, aligning with the graphql-over-http spec guidance while keeping legacy transport behavior unchanged for backward compatibility.

Changes:

  • Honor middleware-proposed HTTP status codes for application/json responses on the latest transport (legacy remains “always 200” for application/json).
  • Distinguish document syntax errors from request-interpretation failures so document syntax errors can keep application/json at 200 while application/graphql-response+json stays 400.
  • Update spec tests to assert 400 for latest + application/json when the request body is unparseable JSON.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/HotChocolate/AspNetCore/test/AspNetCore.Tests/GraphQLOverHttpSpecTests.cs Updates expectations for status codes in the spec matrix tests (latest + application/json invalid JSON now 400).
src/HotChocolate/AspNetCore/src/AspNetCore.Pipeline/HttpPostMiddlewareBase.cs Differentiates request-interpretation failures vs document syntax errors when proposing status codes.
src/HotChocolate/AspNetCore/src/AspNetCore.Pipeline/Formatters/DefaultHttpResponseFormatter.cs Applies proposed status codes for application/json on latest transport, preserving legacy behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@glen-84 glen-84 marked this pull request as draft May 27, 2026 08:37
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread src/HotChocolate/AspNetCore/src/AspNetCore.Pipeline/HttpPostMiddlewareBase.cs Outdated
@glen-84 glen-84 marked this pull request as ready for review May 27, 2026 09:10
@glen-84 glen-84 requested a review from michaelstaib May 27, 2026 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants