Repository navigation
Conversation
Json turned invalid JSON, a JSON scalar and an empty body alike into an empty input, so an API could not answer 400 for a malformed payload. The decode error is now recorded and exposed through hasError() and getError(); an empty body is still not an error. The $GLOBALS['HTTP_RAW_POST_DATA'] fallback is removed: PHP removed that variable in 7.0 and php://input can be read repeatedly, so the branch was only reachable from the unit test, which now feeds php://input through a stream wrapper instead. The docs also explain why Content-Type should be checked for cookie-authenticated JSON endpoints. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary of Changes
Jsondecodes the request body on construction and turns invalid JSON, a JSON scalar (42,null) and an empty body alike into an empty input. An API built on it cannot tell "malformed payload" from "no fields sent" and therefore cannot answer400 Bad Request.This change:
JSON_THROW_ON_ERROR, plus "not a JSON object or array" for scalars) and exposes it through the newhasError()andgetError(). An empty or whitespace-only body is not an error. The data is still an empty input in all of these cases, so nothing changes for existing callers;$GLOBALS['HTTP_RAW_POST_DATA']fallback. PHP removed that variable in 7.0 andphp://inputcan be read more than once since 5.6, so on PHP ^8.3 the branch is only reachable when something sets the global by hand — which only the unit test did. Code that injected a body that way gets an empty input now; I consider that unlikely, but it is a behaviour change;Content-Type: a cross-sitetext/plain"simple" request can carry a JSON body without a CORS preflight, so cookie-authenticated JSON endpoints should check the header. Enforcing it in the class would be a BC break and is not done.Testing Instructions
JsonTestnow feedsphp://inputthrough a small stream wrapper (PhpInputStream) instead ofHTTP_RAW_POST_DATA. New tests:test__constructIgnoresRawPostData(),testEmptyBodyIsNoError()(2 cases) andtestInvalidBodyIsReported()(syntax error, scalar,null);test__constructReadingFromRequestBody()also checks that a valid body is no error. They fail before the change and pass after it.Documentation Changes Required
docs/overview.md("JSON bodies"):hasError()/getError()with an example, and theContent-Typenote.Related to #60 (both change the "JSON bodies" section and the end of
JsonTest; expect a small conflict when the second is merged).🤖 Generated with Claude Code