Skip to content

[4.x] Report JSON decode errors in Json and remove the HTTP_RAW_POST_DATA fallback - #61

Open
Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-json-decode-errors
Open

Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-json-decode-errors

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

Json decodes 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 answer 400 Bad Request.

This change:

  • records why the body could not be decoded (JSON_THROW_ON_ERROR, plus "not a JSON object or array" for scalars) and exposes it through the new hasError() and getError(). 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;
  • removes the $GLOBALS['HTTP_RAW_POST_DATA'] fallback. PHP removed that variable in 7.0 and php://input can 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;
  • documents that the body is decoded regardless of Content-Type: a cross-site text/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

JsonTest now feeds php://input through a small stream wrapper (PhpInputStream) instead of HTTP_RAW_POST_DATA. New tests: test__constructIgnoresRawPostData(), testEmptyBodyIsNoError() (2 cases) and testInvalidBodyIsReported() (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 the Content-Type note.

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

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant