Skip to content

Commit 94392ac

Browse files
arjan-12bclaude
andcommitted
[PHP] replace deprecated Guzzle Utils::jsonEncode with native json_encode
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 `json_encode(..., JSON_THROW_ON_ERROR)`, as directed by the Guzzle 8 upgrade guide. Both generators already require PHP ^8.1, and the response path in the same templates already decodes with `json_decode($content, false, 512, JSON_THROW_ON_ERROR)`, so this makes encoding and decoding consistent. Note this changes the exception type thrown on encoding failure from `GuzzleHttp\Exception\InvalidArgumentException` to `\JsonException`, which is the documented Guzzle migration path. Fixes #24641 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 73bdf41 commit 94392ac

19 files changed

Lines changed: 190 additions & 190 deletions

File tree

modules/openapi-generator/src/main/resources/php-nextgen/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ use {{invokerPackage}}\ObjectSerializer;
762762
if (isset(${{paramName}})) {
763763
if (stripos($headers['Content-Type'], 'application/json') !== false) {
764764
# if Content-Type contains "application/json", json_encode the body
765-
$httpBody = \GuzzleHttp\Utils::jsonEncode(ObjectSerializer::sanitizeForSerialization(${{paramName}}));
765+
$httpBody = json_encode(ObjectSerializer::sanitizeForSerialization(${{paramName}}), JSON_THROW_ON_ERROR);
766766
} else {
767767
$httpBody = ${{paramName}};
768768
}
@@ -787,7 +787,7 @@ use {{invokerPackage}}\ObjectSerializer;
787787

788788
} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
789789
# if Content-Type contains "application/json", json_encode the form parameters
790-
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
790+
$httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
791791
} else {
792792
// for HTTP post (form)
793793
$httpBody = ObjectSerializer::buildQuery($formParams);

modules/openapi-generator/src/main/resources/php/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ use {{invokerPackage}}\ObjectSerializer;
677677
if (isset(${{paramName}})) {
678678
if (stripos($headers['Content-Type'], 'application/json') !== false) {
679679
# if Content-Type contains "application/json", json_encode the body
680-
$httpBody = \GuzzleHttp\Utils::jsonEncode(ObjectSerializer::sanitizeForSerialization(${{paramName}}));
680+
$httpBody = json_encode(ObjectSerializer::sanitizeForSerialization(${{paramName}}), JSON_THROW_ON_ERROR);
681681
} else {
682682
$httpBody = ${{paramName}};
683683
}
@@ -702,7 +702,7 @@ use {{invokerPackage}}\ObjectSerializer;
702702

703703
} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
704704
# if Content-Type contains "application/json", json_encode the form parameters
705-
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
705+
$httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
706706
} else {
707707
// for HTTP post (form)
708708
$httpBody = ObjectSerializer::buildQuery($formParams);

samples/client/echo_api/php-nextgen-streaming/src/Api/BodyApi.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ public function testBodyApplicationOctetstreamBinaryRequest(
598598
if (isset($body)) {
599599
if (stripos($headers['Content-Type'], 'application/json') !== false) {
600600
# if Content-Type contains "application/json", json_encode the body
601-
$httpBody = \GuzzleHttp\Utils::jsonEncode(ObjectSerializer::sanitizeForSerialization($body));
601+
$httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($body), JSON_THROW_ON_ERROR);
602602
} else {
603603
$httpBody = $body;
604604
}
@@ -619,7 +619,7 @@ public function testBodyApplicationOctetstreamBinaryRequest(
619619

620620
} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
621621
# if Content-Type contains "application/json", json_encode the form parameters
622-
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
622+
$httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
623623
} else {
624624
// for HTTP post (form)
625625
$httpBody = ObjectSerializer::buildQuery($formParams);
@@ -896,7 +896,7 @@ public function testBodyMultipartFormdataArrayOfBinaryRequest(
896896

897897
} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
898898
# if Content-Type contains "application/json", json_encode the form parameters
899-
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
899+
$httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
900900
} else {
901901
// for HTTP post (form)
902902
$httpBody = ObjectSerializer::buildQuery($formParams);
@@ -1167,7 +1167,7 @@ public function testBodyMultipartFormdataSingleBinaryRequest(
11671167

11681168
} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
11691169
# if Content-Type contains "application/json", json_encode the form parameters
1170-
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
1170+
$httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
11711171
} else {
11721172
// for HTTP post (form)
11731173
$httpBody = ObjectSerializer::buildQuery($formParams);
@@ -1415,7 +1415,7 @@ public function testEchoBodyAllOfPetRequest(
14151415
if (isset($pet)) {
14161416
if (stripos($headers['Content-Type'], 'application/json') !== false) {
14171417
# if Content-Type contains "application/json", json_encode the body
1418-
$httpBody = \GuzzleHttp\Utils::jsonEncode(ObjectSerializer::sanitizeForSerialization($pet));
1418+
$httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($pet), JSON_THROW_ON_ERROR);
14191419
} else {
14201420
$httpBody = $pet;
14211421
}
@@ -1436,7 +1436,7 @@ public function testEchoBodyAllOfPetRequest(
14361436

14371437
} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
14381438
# if Content-Type contains "application/json", json_encode the form parameters
1439-
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
1439+
$httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
14401440
} else {
14411441
// for HTTP post (form)
14421442
$httpBody = ObjectSerializer::buildQuery($formParams);
@@ -1684,7 +1684,7 @@ public function testEchoBodyFreeFormObjectResponseStringRequest(
16841684
if (isset($body)) {
16851685
if (stripos($headers['Content-Type'], 'application/json') !== false) {
16861686
# if Content-Type contains "application/json", json_encode the body
1687-
$httpBody = \GuzzleHttp\Utils::jsonEncode(ObjectSerializer::sanitizeForSerialization($body));
1687+
$httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($body), JSON_THROW_ON_ERROR);
16881688
} else {
16891689
$httpBody = $body;
16901690
}
@@ -1705,7 +1705,7 @@ public function testEchoBodyFreeFormObjectResponseStringRequest(
17051705

17061706
} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
17071707
# if Content-Type contains "application/json", json_encode the form parameters
1708-
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
1708+
$httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
17091709
} else {
17101710
// for HTTP post (form)
17111711
$httpBody = ObjectSerializer::buildQuery($formParams);
@@ -1953,7 +1953,7 @@ public function testEchoBodyPetRequest(
19531953
if (isset($pet)) {
19541954
if (stripos($headers['Content-Type'], 'application/json') !== false) {
19551955
# if Content-Type contains "application/json", json_encode the body
1956-
$httpBody = \GuzzleHttp\Utils::jsonEncode(ObjectSerializer::sanitizeForSerialization($pet));
1956+
$httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($pet), JSON_THROW_ON_ERROR);
19571957
} else {
19581958
$httpBody = $pet;
19591959
}
@@ -1974,7 +1974,7 @@ public function testEchoBodyPetRequest(
19741974

19751975
} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
19761976
# if Content-Type contains "application/json", json_encode the form parameters
1977-
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
1977+
$httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
19781978
} else {
19791979
// for HTTP post (form)
19801980
$httpBody = ObjectSerializer::buildQuery($formParams);
@@ -2222,7 +2222,7 @@ public function testEchoBodyPetResponseStringRequest(
22222222
if (isset($pet)) {
22232223
if (stripos($headers['Content-Type'], 'application/json') !== false) {
22242224
# if Content-Type contains "application/json", json_encode the body
2225-
$httpBody = \GuzzleHttp\Utils::jsonEncode(ObjectSerializer::sanitizeForSerialization($pet));
2225+
$httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($pet), JSON_THROW_ON_ERROR);
22262226
} else {
22272227
$httpBody = $pet;
22282228
}
@@ -2243,7 +2243,7 @@ public function testEchoBodyPetResponseStringRequest(
22432243

22442244
} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
22452245
# if Content-Type contains "application/json", json_encode the form parameters
2246-
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
2246+
$httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
22472247
} else {
22482248
// for HTTP post (form)
22492249
$httpBody = ObjectSerializer::buildQuery($formParams);
@@ -2491,7 +2491,7 @@ public function testEchoBodyStringEnumRequest(
24912491
if (isset($body)) {
24922492
if (stripos($headers['Content-Type'], 'application/json') !== false) {
24932493
# if Content-Type contains "application/json", json_encode the body
2494-
$httpBody = \GuzzleHttp\Utils::jsonEncode(ObjectSerializer::sanitizeForSerialization($body));
2494+
$httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($body), JSON_THROW_ON_ERROR);
24952495
} else {
24962496
$httpBody = $body;
24972497
}
@@ -2512,7 +2512,7 @@ public function testEchoBodyStringEnumRequest(
25122512

25132513
} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
25142514
# if Content-Type contains "application/json", json_encode the form parameters
2515-
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
2515+
$httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
25162516
} else {
25172517
// for HTTP post (form)
25182518
$httpBody = ObjectSerializer::buildQuery($formParams);
@@ -2760,7 +2760,7 @@ public function testEchoBodyTagResponseStringRequest(
27602760
if (isset($tag)) {
27612761
if (stripos($headers['Content-Type'], 'application/json') !== false) {
27622762
# if Content-Type contains "application/json", json_encode the body
2763-
$httpBody = \GuzzleHttp\Utils::jsonEncode(ObjectSerializer::sanitizeForSerialization($tag));
2763+
$httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($tag), JSON_THROW_ON_ERROR);
27642764
} else {
27652765
$httpBody = $tag;
27662766
}
@@ -2781,7 +2781,7 @@ public function testEchoBodyTagResponseStringRequest(
27812781

27822782
} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
27832783
# if Content-Type contains "application/json", json_encode the form parameters
2784-
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
2784+
$httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
27852785
} else {
27862786
// for HTTP post (form)
27872787
$httpBody = ObjectSerializer::buildQuery($formParams);

samples/client/echo_api/php-nextgen-streaming/src/Api/FormApi.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ public function testFormIntegerBooleanStringRequest(
395395

396396
} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
397397
# if Content-Type contains "application/json", json_encode the form parameters
398-
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
398+
$httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
399399
} else {
400400
// for HTTP post (form)
401401
$httpBody = ObjectSerializer::buildQuery($formParams);
@@ -672,7 +672,7 @@ public function testFormObjectMultipartRequest(
672672

673673
} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
674674
# if Content-Type contains "application/json", json_encode the form parameters
675-
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
675+
$httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
676676
} else {
677677
// for HTTP post (form)
678678
$httpBody = ObjectSerializer::buildQuery($formParams);
@@ -998,7 +998,7 @@ public function testFormOneofRequest(
998998

999999
} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
10001000
# if Content-Type contains "application/json", json_encode the form parameters
1001-
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
1001+
$httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
10021002
} else {
10031003
// for HTTP post (form)
10041004
$httpBody = ObjectSerializer::buildQuery($formParams);

0 commit comments

Comments
 (0)