[PHP] Replace deprecated Guzzle Utils::jsonEncode with native json_encode - #24688
Merged
wing328 merged 1 commit intoAug 12, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
All reported issues were addressed across 19 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
…code Guzzle 7.15 deprecated `Utils::jsonEncode()` / `Utils::jsonDecode()` and Guzzle 8 removes them, so generated clients emit deprecation warnings on Guzzle >= 7.15. Replace the two `\GuzzleHttp\Utils::jsonEncode()` call sites in the `php` and `php-nextgen` api templates with native `json_encode()` using `JSON_THROW_ON_ERROR`, as directed by the Guzzle 8 upgrade guide. Both generators already require PHP ^8.1, and the response path in these same templates already decodes with `json_decode($content, false, 512, JSON_THROW_ON_ERROR)`, so this makes encoding and decoding consistent. `Utils::jsonEncode()` checked `json_last_error()` and threw `GuzzleHttp\Exception\InvalidArgumentException` on failure, so the encode calls catch `\JsonException` and rethrow it as `\InvalidArgumentException` to preserve that behavior. The contract is unchanged for callers: Guzzle's exception extends the SPL one, the `json_encode error: ` prefix combined with `JsonException::getMessage()` reproduces the old message verbatim, and the generated `@throws \InvalidArgumentException` docblocks stay accurate. There were no `Utils::jsonDecode()` usages to replace. The `Utils::tryFopen()` and `Utils::streamFor()` calls in ObjectSerializer and FormDataProcessor are `GuzzleHttp\Psr7\Utils`, which is not deprecated. Fixes OpenAPITools#24641
arjan-12b
force-pushed
the
fix/php-guzzle-json-encode-deprecation
branch
from
August 12, 2026 09:54
94392ac to
3490407
Compare
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.
Fixes #24641
Guzzle 7.15 deprecated
Utils::jsonEncode()/Utils::jsonDecode(), and Guzzle 8 removes them (upgrade guide). Generated PHP clients therefore emit deprecation warnings on Guzzle >= 7.15.Change
Replaced the two
\GuzzleHttp\Utils::jsonEncode()call sites (body param and form params) in both api templates:modules/openapi-generator/src/main/resources/php/api.mustachemodules/openapi-generator/src/main/resources/php-nextgen/api.mustacheJSON_THROW_ON_ERRORis safe here: both generators already requirephp: ^8.1, and the response path in these same templates already decodes withjson_decode($content, false, 512, JSON_THROW_ON_ERROR)— so encoding and decoding are now consistent.This is fully backwards compatible
Utils::jsonEncode()was not a barejson_encode()— it checkedjson_last_error()and threw (src/Utils.php#L817). Dropping that error handling would silently send an empty body on an encode failure, so the encode calls catch\JsonExceptionand rethrow as\InvalidArgumentException.I verified parity by replicating Guzzle 7.15's implementation and diffing it against the generated code (PHP 8.4) — all cases match, on both output and failure:
Utils::jsonEncode)InvalidArgumentException: json_encode error: Malformed UTF-8 characters, possibly incorrectly encodedINFInvalidArgumentException: json_encode error: Inf and NaN cannot be JSON encodedThe message reproduces verbatim because Guzzle used
json_last_error_msg()andJsonException::getMessage()returns the same text. And sinceGuzzleHttp\Exception\InvalidArgumentExceptionextends SPL\InvalidArgumentException, existingcatch (\InvalidArgumentException)blocks keep working — which is also the type the generated@throwsdocblocks already declare, so no PHPDoc changes are needed.Notes
Utils::jsonDecode()usages existed in the templates, so only the encode side needed changing.Utils::tryFopen()/Utils::streamFor()inObjectSerializer.mustacheandFormDataProcessor.mustacheareGuzzleHttp\Psr7\Utils, which is not deprecated — left untouched.composer.mustachestill pinsguzzlehttp/guzzle: "^7.3". Actually allowing Guzzle 8 needs that widened plus an audit of Guzzle 8's other BC breaks — happy to do that separately if wanted.Testing
php-OpenAPIClient,php-nextgen,php-nextgen-echo-api,php-nextgen-echo-api-streaming(17 files). The diff contains only the change above — no incidental regeneration churn.php -l(PHP 8.4) passes on all 17 regenerated files.PR checklist
./mvnw clean packageand regenerated the affected samples. All changed files committed.