Skip to content

[PHP] Replace deprecated Guzzle Utils::jsonEncode with native json_encode - #24688

Merged
wing328 merged 1 commit into
OpenAPITools:masterfrom
arjan-12b:fix/php-guzzle-json-encode-deprecation
Aug 12, 2026
Merged

[PHP] Replace deprecated Guzzle Utils::jsonEncode with native json_encode#24688
wing328 merged 1 commit into
OpenAPITools:masterfrom
arjan-12b:fix/php-guzzle-json-encode-deprecation

Conversation

@arjan-12b

@arjan-12b arjan-12b commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

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:

-$httpBody = \GuzzleHttp\Utils::jsonEncode(ObjectSerializer::sanitizeForSerialization($pet));
+try {
+    $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($pet), JSON_THROW_ON_ERROR);
+} catch (\JsonException $e) {
+    throw new \InvalidArgumentException('json_encode error: ' . $e->getMessage(), 0, $e);
+}
  • modules/openapi-generator/src/main/resources/php/api.mustache
  • modules/openapi-generator/src/main/resources/php-nextgen/api.mustache

JSON_THROW_ON_ERROR is safe here: 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 encoding and decoding are now consistent.

This is fully backwards compatible

Utils::jsonEncode() was not a bare json_encode() — it checked json_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 \JsonException and 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:

Case Old (Utils::jsonEncode) New (generated)
object / nested / unicode / slashes / empty identical JSON bytes identical JSON bytes
invalid UTF-8 InvalidArgumentException: json_encode error: Malformed UTF-8 characters, possibly incorrectly encoded same class, same message
INF InvalidArgumentException: json_encode error: Inf and NaN cannot be JSON encoded same class, same message

The message reproduces verbatim because Guzzle used json_last_error_msg() and JsonException::getMessage() returns the same text. And since GuzzleHttp\Exception\InvalidArgumentException extends SPL \InvalidArgumentException, existing catch (\InvalidArgumentException) blocks keep working — which is also the type the generated @throws docblocks already declare, so no PHPDoc changes are needed.

Notes

  • No Utils::jsonDecode() usages existed in the templates, so only the encode side needed changing.
  • Utils::tryFopen() / Utils::streamFor() in ObjectSerializer.mustache and FormDataProcessor.mustache are GuzzleHttp\Psr7\Utils, which is not deprecated — left untouched.
  • Out of scope: composer.mustache still pins guzzlehttp/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

  • Regenerated samples for 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.
  • Behavior parity against Guzzle 7.15 verified as tabled above.

PR checklist

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 19 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread modules/openapi-generator/src/main/resources/php-nextgen/api.mustache Outdated
Comment thread samples/client/echo_api/php-nextgen-streaming/src/Api/BodyApi.php Outdated
Comment thread samples/client/echo_api/php-nextgen/src/Api/BodyApi.php Outdated
Comment thread samples/client/petstore/php/OpenAPIClient-php/lib/Api/PetApi.php Outdated
Comment thread modules/openapi-generator/src/main/resources/php/api.mustache Outdated
…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
arjan-12b force-pushed the fix/php-guzzle-json-encode-deprecation branch from 94392ac to 3490407 Compare August 12, 2026 09:54
@wing328 wing328 added this to the 7.25.0 milestone Aug 12, 2026
@wing328
wing328 merged commit 0071a53 into OpenAPITools:master Aug 12, 2026
32 checks passed
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.

[REQ] Guzzle::json_encode is deprecated since Guzzle 7.15

2 participants