From 6cd73eba2bbab2fd8385e8741b97665ee3809115 Mon Sep 17 00:00:00 2001 From: Artur Neumann Date: Tue, 31 Oct 2023 13:37:33 +0545 Subject: [PATCH] [PHP] check if json_decode was able to decode response (#16879) * [PHP] check if json_decode was able to decode response * use try/catch to check if json_decode failed --- .../src/main/resources/php/api.mustache | 32 +- .../lib/Api/AnotherFakeApi.php | 32 +- .../OpenAPIClient-php/lib/Api/DefaultApi.php | 32 +- .../php/OpenAPIClient-php/lib/Api/FakeApi.php | 304 ++++++++++++++---- .../lib/Api/FakeClassnameTags123Api.php | 32 +- .../php/OpenAPIClient-php/lib/Api/PetApi.php | 176 ++++++++-- .../OpenAPIClient-php/lib/Api/StoreApi.php | 100 +++++- .../php/OpenAPIClient-php/lib/Api/UserApi.php | 88 +++-- .../php/OpenAPIClient-php/tests/AuthTest.php | 3 + .../OpenAPIClient-php/tests/HeadersTest.php | 2 + .../tests/ResponseTypesTest.php | 25 ++ 11 files changed, 692 insertions(+), 134 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/php/api.mustache b/modules/openapi-generator/src/main/resources/php/api.mustache index 9f36a517588..ef964c535d8 100644 --- a/modules/openapi-generator/src/main/resources/php/api.mustache +++ b/modules/openapi-generator/src/main/resources/php/api.mustache @@ -162,7 +162,7 @@ use {{invokerPackage}}\ObjectSerializer; {{/servers}} * @param string $contentType The value for the Content-Type header. Check self::contentTypes['{{{operationId}}}'] to see the possible values for this operation * - * @throws \{{invokerPackage}}\ApiException on non-2xx response + * @throws \{{invokerPackage}}\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return {{#returnType}}{{#responses}}{{#dataType}}{{^-first}}|{{/-first}}{{/dataType}}{{{dataType}}}{{/responses}}{{/returnType}}{{^returnType}}void{{/returnType}} {{#isDeprecated}} @@ -221,7 +221,7 @@ use {{invokerPackage}}\ObjectSerializer; {{/servers}} * @param string $contentType The value for the Content-Type header. Check self::contentTypes['{{{operationId}}}'] to see the possible values for this operation * - * @throws \{{invokerPackage}}\ApiException on non-2xx response + * @throws \{{invokerPackage}}\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of {{#returnType}}{{#responses}}{{#dataType}}{{^-first}}|{{/-first}}{{/dataType}}{{{dataType}}}{{/responses}}{{/returnType}}{{^returnType}}null{{/returnType}}, HTTP status code, HTTP response headers (array of strings) {{#isDeprecated}} @@ -279,7 +279,19 @@ use {{invokerPackage}}\ObjectSerializer; } else { $content = (string) $response->getBody(); if ('{{{dataType}}}' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -300,7 +312,19 @@ use {{invokerPackage}}\ObjectSerializer; } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/AnotherFakeApi.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/AnotherFakeApi.php index d93207e06b3..542a2764409 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/AnotherFakeApi.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/AnotherFakeApi.php @@ -130,7 +130,7 @@ class AnotherFakeApi * @param \OpenAPI\Client\Model\Client $client client model (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['call123TestSpecialTags'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return \OpenAPI\Client\Model\Client */ @@ -148,7 +148,7 @@ class AnotherFakeApi * @param \OpenAPI\Client\Model\Client $client client model (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['call123TestSpecialTags'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of \OpenAPI\Client\Model\Client, HTTP status code, HTTP response headers (array of strings) */ @@ -198,7 +198,19 @@ class AnotherFakeApi } else { $content = (string) $response->getBody(); if ('\OpenAPI\Client\Model\Client' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -215,7 +227,19 @@ class AnotherFakeApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/DefaultApi.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/DefaultApi.php index e947bae0eeb..ebc8359b016 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/DefaultApi.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/DefaultApi.php @@ -127,7 +127,7 @@ class DefaultApi * * @param string $contentType The value for the Content-Type header. Check self::contentTypes['fooGet'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return \OpenAPI\Client\Model\FooGetDefaultResponse */ @@ -142,7 +142,7 @@ class DefaultApi * * @param string $contentType The value for the Content-Type header. Check self::contentTypes['fooGet'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of \OpenAPI\Client\Model\FooGetDefaultResponse, HTTP status code, HTTP response headers (array of strings) */ @@ -192,7 +192,19 @@ class DefaultApi } else { $content = (string) $response->getBody(); if ('\OpenAPI\Client\Model\FooGetDefaultResponse' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -209,7 +221,19 @@ class DefaultApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeApi.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeApi.php index a1bb31cbe6f..32077a34406 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeApi.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeApi.php @@ -185,7 +185,7 @@ class FakeApi * * @param string $contentType The value for the Content-Type header. Check self::contentTypes['fakeBigDecimalMap'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return \OpenAPI\Client\Model\FakeBigDecimalMap200Response */ @@ -200,7 +200,7 @@ class FakeApi * * @param string $contentType The value for the Content-Type header. Check self::contentTypes['fakeBigDecimalMap'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of \OpenAPI\Client\Model\FakeBigDecimalMap200Response, HTTP status code, HTTP response headers (array of strings) */ @@ -250,7 +250,19 @@ class FakeApi } else { $content = (string) $response->getBody(); if ('\OpenAPI\Client\Model\FakeBigDecimalMap200Response' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -267,7 +279,19 @@ class FakeApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -442,7 +466,7 @@ class FakeApi * * @param string $contentType The value for the Content-Type header. Check self::contentTypes['fakeHealthGet'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return \OpenAPI\Client\Model\HealthCheckResult */ @@ -459,7 +483,7 @@ class FakeApi * * @param string $contentType The value for the Content-Type header. Check self::contentTypes['fakeHealthGet'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of \OpenAPI\Client\Model\HealthCheckResult, HTTP status code, HTTP response headers (array of strings) */ @@ -509,7 +533,19 @@ class FakeApi } else { $content = (string) $response->getBody(); if ('\OpenAPI\Client\Model\HealthCheckResult' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -526,7 +562,19 @@ class FakeApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -708,7 +756,7 @@ class FakeApi * @param string $header_1 header parameter (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['fakeHttpSignatureTest'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -727,7 +775,7 @@ class FakeApi * @param string $header_1 header parameter (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['fakeHttpSignatureTest'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -957,7 +1005,7 @@ class FakeApi * @param bool $body Input boolean as post body (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['fakeOuterBooleanSerialize'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return bool */ @@ -973,7 +1021,7 @@ class FakeApi * @param bool $body Input boolean as post body (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['fakeOuterBooleanSerialize'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of bool, HTTP status code, HTTP response headers (array of strings) */ @@ -1023,7 +1071,19 @@ class FakeApi } else { $content = (string) $response->getBody(); if ('bool' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -1040,7 +1100,19 @@ class FakeApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -1225,7 +1297,7 @@ class FakeApi * @param \OpenAPI\Client\Model\OuterComposite $outer_composite Input composite as post body (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['fakeOuterCompositeSerialize'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return \OpenAPI\Client\Model\OuterComposite */ @@ -1241,7 +1313,7 @@ class FakeApi * @param \OpenAPI\Client\Model\OuterComposite $outer_composite Input composite as post body (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['fakeOuterCompositeSerialize'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of \OpenAPI\Client\Model\OuterComposite, HTTP status code, HTTP response headers (array of strings) */ @@ -1291,7 +1363,19 @@ class FakeApi } else { $content = (string) $response->getBody(); if ('\OpenAPI\Client\Model\OuterComposite' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -1308,7 +1392,19 @@ class FakeApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -1493,7 +1589,7 @@ class FakeApi * @param float $body Input number as post body (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['fakeOuterNumberSerialize'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return float */ @@ -1509,7 +1605,7 @@ class FakeApi * @param float $body Input number as post body (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['fakeOuterNumberSerialize'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of float, HTTP status code, HTTP response headers (array of strings) */ @@ -1559,7 +1655,19 @@ class FakeApi } else { $content = (string) $response->getBody(); if ('float' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -1576,7 +1684,19 @@ class FakeApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -1761,7 +1881,7 @@ class FakeApi * @param string $body Input string as post body (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['fakeOuterStringSerialize'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return string */ @@ -1777,7 +1897,7 @@ class FakeApi * @param string $body Input string as post body (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['fakeOuterStringSerialize'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of string, HTTP status code, HTTP response headers (array of strings) */ @@ -1827,7 +1947,19 @@ class FakeApi } else { $content = (string) $response->getBody(); if ('string' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -1844,7 +1976,19 @@ class FakeApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -2029,7 +2173,7 @@ class FakeApi * @param \OpenAPI\Client\Model\OuterObjectWithEnumProperty $outer_object_with_enum_property Input enum (int) as post body (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['fakePropertyEnumIntegerSerialize'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return \OpenAPI\Client\Model\OuterObjectWithEnumProperty */ @@ -2045,7 +2189,7 @@ class FakeApi * @param \OpenAPI\Client\Model\OuterObjectWithEnumProperty $outer_object_with_enum_property Input enum (int) as post body (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['fakePropertyEnumIntegerSerialize'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of \OpenAPI\Client\Model\OuterObjectWithEnumProperty, HTTP status code, HTTP response headers (array of strings) */ @@ -2095,7 +2239,19 @@ class FakeApi } else { $content = (string) $response->getBody(); if ('\OpenAPI\Client\Model\OuterObjectWithEnumProperty' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -2112,7 +2268,19 @@ class FakeApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -2309,7 +2477,7 @@ class FakeApi * @param string $http_debug_option http debug option (to test parameter naming option) (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getParameterNameMapping'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -2330,7 +2498,7 @@ class FakeApi * @param string $http_debug_option http debug option (to test parameter naming option) (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getParameterNameMapping'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -2602,7 +2770,7 @@ class FakeApi * @param \SplFileObject $body image to upload (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testBodyWithBinary'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -2617,7 +2785,7 @@ class FakeApi * @param \SplFileObject $body image to upload (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testBodyWithBinary'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -2822,7 +2990,7 @@ class FakeApi * @param \OpenAPI\Client\Model\FileSchemaTestClass $file_schema_test_class file_schema_test_class (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testBodyWithFileSchema'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -2837,7 +3005,7 @@ class FakeApi * @param \OpenAPI\Client\Model\FileSchemaTestClass $file_schema_test_class (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testBodyWithFileSchema'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -3043,7 +3211,7 @@ class FakeApi * @param \OpenAPI\Client\Model\User $user user (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testBodyWithQueryParams'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -3059,7 +3227,7 @@ class FakeApi * @param \OpenAPI\Client\Model\User $user (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testBodyWithQueryParams'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -3285,7 +3453,7 @@ class FakeApi * @param \OpenAPI\Client\Model\Client $client client model (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testClientModel'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return \OpenAPI\Client\Model\Client */ @@ -3303,7 +3471,7 @@ class FakeApi * @param \OpenAPI\Client\Model\Client $client client model (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testClientModel'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of \OpenAPI\Client\Model\Client, HTTP status code, HTTP response headers (array of strings) */ @@ -3353,7 +3521,19 @@ class FakeApi } else { $content = (string) $response->getBody(); if ('\OpenAPI\Client\Model\Client' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -3370,7 +3550,19 @@ class FakeApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -3580,7 +3772,7 @@ class FakeApi * @param string $callback None (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testEndpointParameters'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -3610,7 +3802,7 @@ class FakeApi * @param string $callback None (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testEndpointParameters'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -3999,7 +4191,7 @@ class FakeApi * @param string $enum_form_string Form parameter enum test (string) (optional, default to '-efg') * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testEnumParameters'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -4024,7 +4216,7 @@ class FakeApi * @param string $enum_form_string Form parameter enum test (string) (optional, default to '-efg') * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testEnumParameters'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -4325,7 +4517,7 @@ class FakeApi * @param int $int64_group Integer in group parameters (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testGroupParameters'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -4349,7 +4541,7 @@ class FakeApi * @param int $int64_group Integer in group parameters (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testGroupParameters'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -4647,7 +4839,7 @@ class FakeApi * @param array $request_body request body (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testInlineAdditionalProperties'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -4664,7 +4856,7 @@ class FakeApi * @param array $request_body request body (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testInlineAdditionalProperties'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -4875,7 +5067,7 @@ class FakeApi * @param \OpenAPI\Client\Model\TestInlineFreeformAdditionalPropertiesRequest $test_inline_freeform_additional_properties_request request body (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testInlineFreeformAdditionalProperties'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -4892,7 +5084,7 @@ class FakeApi * @param \OpenAPI\Client\Model\TestInlineFreeformAdditionalPropertiesRequest $test_inline_freeform_additional_properties_request request body (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testInlineFreeformAdditionalProperties'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -5104,7 +5296,7 @@ class FakeApi * @param string $param2 field2 (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testJsonFormData'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -5122,7 +5314,7 @@ class FakeApi * @param string $param2 field2 (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testJsonFormData'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -5348,7 +5540,7 @@ class FakeApi * @param array $language language (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testQueryParameterCollectionFormat'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -5369,7 +5561,7 @@ class FakeApi * @param array $language (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testQueryParameterCollectionFormat'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeClassnameTags123Api.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeClassnameTags123Api.php index 965f6491cd5..6fc003b613d 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeClassnameTags123Api.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeClassnameTags123Api.php @@ -130,7 +130,7 @@ class FakeClassnameTags123Api * @param \OpenAPI\Client\Model\Client $client client model (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testClassname'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return \OpenAPI\Client\Model\Client */ @@ -148,7 +148,7 @@ class FakeClassnameTags123Api * @param \OpenAPI\Client\Model\Client $client client model (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['testClassname'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of \OpenAPI\Client\Model\Client, HTTP status code, HTTP response headers (array of strings) */ @@ -198,7 +198,19 @@ class FakeClassnameTags123Api } else { $content = (string) $response->getBody(); if ('\OpenAPI\Client\Model\Client' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -215,7 +227,19 @@ class FakeClassnameTags123Api } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/PetApi.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/PetApi.php index 735bd078f5c..6a9174d3b31 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/PetApi.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/PetApi.php @@ -174,7 +174,7 @@ class PetApi * @param array $variables Associative array of variables to pass to the host. Defaults to empty array. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['addPet'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -209,7 +209,7 @@ class PetApi * @param array $variables Associative array of variables to pass to the host. Defaults to empty array. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['addPet'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -531,7 +531,7 @@ class PetApi * @param string $api_key api_key (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['deletePet'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -549,7 +549,7 @@ class PetApi * @param string $api_key (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['deletePet'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -773,7 +773,7 @@ class PetApi * @param string[] $status Status values that need to be considered for filter (required) (deprecated) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['findPetsByStatus'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return \OpenAPI\Client\Model\Pet[] */ @@ -791,7 +791,7 @@ class PetApi * @param string[] $status Status values that need to be considered for filter (required) (deprecated) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['findPetsByStatus'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of \OpenAPI\Client\Model\Pet[], HTTP status code, HTTP response headers (array of strings) */ @@ -841,7 +841,19 @@ class PetApi } else { $content = (string) $response->getBody(); if ('\OpenAPI\Client\Model\Pet[]' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -858,7 +870,19 @@ class PetApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -1061,7 +1085,7 @@ class PetApi * @param string[] $tags Tags to filter by (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['findPetsByTags'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return \OpenAPI\Client\Model\Pet[] * @deprecated @@ -1080,7 +1104,7 @@ class PetApi * @param string[] $tags Tags to filter by (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['findPetsByTags'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of \OpenAPI\Client\Model\Pet[], HTTP status code, HTTP response headers (array of strings) * @deprecated @@ -1131,7 +1155,19 @@ class PetApi } else { $content = (string) $response->getBody(); if ('\OpenAPI\Client\Model\Pet[]' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -1148,7 +1184,19 @@ class PetApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -1354,7 +1402,7 @@ class PetApi * @param int $pet_id ID of pet to return (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getPetById'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return \OpenAPI\Client\Model\Pet */ @@ -1372,7 +1420,7 @@ class PetApi * @param int $pet_id ID of pet to return (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getPetById'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of \OpenAPI\Client\Model\Pet, HTTP status code, HTTP response headers (array of strings) */ @@ -1422,7 +1470,19 @@ class PetApi } else { $content = (string) $response->getBody(); if ('\OpenAPI\Client\Model\Pet' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -1439,7 +1499,19 @@ class PetApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -1660,7 +1732,7 @@ class PetApi * @param array $variables Associative array of variables to pass to the host. Defaults to empty array. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['updatePet'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -1695,7 +1767,7 @@ class PetApi * @param array $variables Associative array of variables to pass to the host. Defaults to empty array. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['updatePet'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -2018,7 +2090,7 @@ class PetApi * @param string $status Updated status of the pet (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['updatePetWithForm'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -2037,7 +2109,7 @@ class PetApi * @param string $status Updated status of the pet (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['updatePetWithForm'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -2271,7 +2343,7 @@ class PetApi * @param \SplFileObject $file file to upload (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['uploadFile'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return \OpenAPI\Client\Model\ApiResponse */ @@ -2291,7 +2363,7 @@ class PetApi * @param \SplFileObject $file file to upload (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['uploadFile'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of \OpenAPI\Client\Model\ApiResponse, HTTP status code, HTTP response headers (array of strings) */ @@ -2341,7 +2413,19 @@ class PetApi } else { $content = (string) $response->getBody(); if ('\OpenAPI\Client\Model\ApiResponse' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -2358,7 +2442,19 @@ class PetApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -2586,7 +2682,7 @@ class PetApi * @param string $additional_metadata Additional data to pass to server (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['uploadFileWithRequiredFile'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return \OpenAPI\Client\Model\ApiResponse */ @@ -2606,7 +2702,7 @@ class PetApi * @param string $additional_metadata Additional data to pass to server (optional) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['uploadFileWithRequiredFile'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of \OpenAPI\Client\Model\ApiResponse, HTTP status code, HTTP response headers (array of strings) */ @@ -2656,7 +2752,19 @@ class PetApi } else { $content = (string) $response->getBody(); if ('\OpenAPI\Client\Model\ApiResponse' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -2673,7 +2781,19 @@ class PetApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/StoreApi.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/StoreApi.php index 712c7fbb623..35f37aac10d 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/StoreApi.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/StoreApi.php @@ -139,7 +139,7 @@ class StoreApi * @param string $order_id ID of the order that needs to be deleted (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['deleteOrder'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -156,7 +156,7 @@ class StoreApi * @param string $order_id ID of the order that needs to be deleted (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['deleteOrder'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -367,7 +367,7 @@ class StoreApi * * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getInventory'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array */ @@ -384,7 +384,7 @@ class StoreApi * * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getInventory'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of array, HTTP status code, HTTP response headers (array of strings) */ @@ -434,7 +434,19 @@ class StoreApi } else { $content = (string) $response->getBody(); if ('array' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -451,7 +463,19 @@ class StoreApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -636,7 +660,7 @@ class StoreApi * @param int $order_id ID of pet that needs to be fetched (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getOrderById'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return \OpenAPI\Client\Model\Order */ @@ -654,7 +678,7 @@ class StoreApi * @param int $order_id ID of pet that needs to be fetched (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getOrderById'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of \OpenAPI\Client\Model\Order, HTTP status code, HTTP response headers (array of strings) */ @@ -704,7 +728,19 @@ class StoreApi } else { $content = (string) $response->getBody(); if ('\OpenAPI\Client\Model\Order' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -721,7 +757,19 @@ class StoreApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -925,7 +973,7 @@ class StoreApi * @param \OpenAPI\Client\Model\Order $order order placed for purchasing the pet (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['placeOrder'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return \OpenAPI\Client\Model\Order */ @@ -943,7 +991,7 @@ class StoreApi * @param \OpenAPI\Client\Model\Order $order order placed for purchasing the pet (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['placeOrder'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of \OpenAPI\Client\Model\Order, HTTP status code, HTTP response headers (array of strings) */ @@ -993,7 +1041,19 @@ class StoreApi } else { $content = (string) $response->getBody(); if ('\OpenAPI\Client\Model\Order' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -1010,7 +1070,19 @@ class StoreApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/UserApi.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/UserApi.php index d808edb0f88..3b95824cfa4 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/UserApi.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/UserApi.php @@ -151,7 +151,7 @@ class UserApi * @param \OpenAPI\Client\Model\User $user Created user object (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['createUser'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -168,7 +168,7 @@ class UserApi * @param \OpenAPI\Client\Model\User $user Created user object (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['createUser'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -379,7 +379,7 @@ class UserApi * @param \OpenAPI\Client\Model\User[] $user List of user object (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['createUsersWithArrayInput'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -396,7 +396,7 @@ class UserApi * @param \OpenAPI\Client\Model\User[] $user List of user object (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['createUsersWithArrayInput'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -607,7 +607,7 @@ class UserApi * @param \OpenAPI\Client\Model\User[] $user List of user object (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['createUsersWithListInput'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -624,7 +624,7 @@ class UserApi * @param \OpenAPI\Client\Model\User[] $user List of user object (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['createUsersWithListInput'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -835,7 +835,7 @@ class UserApi * @param string $username The name that needs to be deleted (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['deleteUser'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -852,7 +852,7 @@ class UserApi * @param string $username The name that needs to be deleted (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['deleteUser'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -1064,7 +1064,7 @@ class UserApi * @param string $username The name that needs to be fetched. Use user1 for testing. (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getUserByName'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return \OpenAPI\Client\Model\User */ @@ -1082,7 +1082,7 @@ class UserApi * @param string $username The name that needs to be fetched. Use user1 for testing. (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getUserByName'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of \OpenAPI\Client\Model\User, HTTP status code, HTTP response headers (array of strings) */ @@ -1132,7 +1132,19 @@ class UserApi } else { $content = (string) $response->getBody(); if ('\OpenAPI\Client\Model\User' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -1149,7 +1161,19 @@ class UserApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -1348,7 +1372,7 @@ class UserApi * @param string $password The password for login in clear text (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['loginUser'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return string */ @@ -1367,7 +1391,7 @@ class UserApi * @param string $password The password for login in clear text (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['loginUser'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of string, HTTP status code, HTTP response headers (array of strings) */ @@ -1417,7 +1441,19 @@ class UserApi } else { $content = (string) $response->getBody(); if ('string' !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -1434,7 +1470,19 @@ class UserApi } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { - $content = json_decode($content); + try { + $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new ApiException( + sprintf( + 'Error JSON decoding server response (%s)', + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $content + ); + } } } @@ -1651,7 +1699,7 @@ class UserApi * * @param string $contentType The value for the Content-Type header. Check self::contentTypes['logoutUser'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -1667,7 +1715,7 @@ class UserApi * * @param string $contentType The value for the Content-Type header. Check self::contentTypes['logoutUser'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ @@ -1862,7 +1910,7 @@ class UserApi * @param \OpenAPI\Client\Model\User $user Updated user object (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['updateUser'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return void */ @@ -1880,7 +1928,7 @@ class UserApi * @param \OpenAPI\Client\Model\User $user Updated user object (required) * @param string $contentType The value for the Content-Type header. Check self::contentTypes['updateUser'] to see the possible values for this operation * - * @throws \OpenAPI\Client\ApiException on non-2xx response + * @throws \OpenAPI\Client\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of null, HTTP status code, HTTP response headers (array of strings) */ diff --git a/samples/client/petstore/php/OpenAPIClient-php/tests/AuthTest.php b/samples/client/petstore/php/OpenAPIClient-php/tests/AuthTest.php index daafb50ede7..8da485b3f0a 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/tests/AuthTest.php +++ b/samples/client/petstore/php/OpenAPIClient-php/tests/AuthTest.php @@ -2,10 +2,12 @@ namespace OpenAPI\Client; +use GuzzleHttp\Psr7\Response; use OpenAPI\Client\Api\FakeApi; use OpenAPI\Client\Api\PetApi; use OpenAPI\Client\Model\Pet; use PHPUnit\Framework\TestCase; +use Psr\Http\Message\ResponseInterface; require_once __DIR__ . '/FakeHttpClient.php'; @@ -17,6 +19,7 @@ class AuthTest extends TestCase $authConfig->setApiKey('api_key', '123qwe'); $fakeHttpClient = new FakeHttpClient(); + $fakeHttpClient->setResponse(new Response(200, [], json_encode([]))); $api = new PetApi($fakeHttpClient, $authConfig); $api->getPetById(123); diff --git a/samples/client/petstore/php/OpenAPIClient-php/tests/HeadersTest.php b/samples/client/petstore/php/OpenAPIClient-php/tests/HeadersTest.php index 989516cc0c6..8f5cc48564d 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/tests/HeadersTest.php +++ b/samples/client/petstore/php/OpenAPIClient-php/tests/HeadersTest.php @@ -2,6 +2,7 @@ namespace OpenAPI\Client; +use GuzzleHttp\Psr7\Response; use PHPUnit\Framework\TestCase; require_once __DIR__ . '/FakeHttpClient.php'; @@ -14,6 +15,7 @@ class HeadersTest extends TestCase public function setUp(): void { $this->fakeHttpClient = new FakeHttpClient(); + $this->fakeHttpClient->setResponse(new Response(200, [], json_encode([]))); } public function testUserAgent() diff --git a/samples/client/petstore/php/OpenAPIClient-php/tests/ResponseTypesTest.php b/samples/client/petstore/php/OpenAPIClient-php/tests/ResponseTypesTest.php index 8d65396ccbc..1dd5a618b59 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/tests/ResponseTypesTest.php +++ b/samples/client/petstore/php/OpenAPIClient-php/tests/ResponseTypesTest.php @@ -89,4 +89,29 @@ class ResponseTypesTest extends TestCase $this->assertNull($result); } + + public function invalidJSONResponseProvider() + { + return [ + 'status 200, content empty' => [200, ''], + 'status 200, content leading comma' => [200, '{"key": "value",}'], + 'status 200, content just text' => [200, 'invalid JSON'], + 'status 200, content null' => [200, null], + 'status 204, content empty' => [204, ''], + 'status 204, content leading comma' => [204, '{"key": "value",}'], + 'status 204, content just text' => [204, 'invalid JSON'], + 'status 204, content null' => [204, null], + ]; + } + /** + * @dataProvider invalidJSONResponseProvider + */ + public function testNotJSONResponse($statusCode, $responseBody) + { + $this->expectExceptionCode($statusCode); + $this->expectException(\OpenAPI\Client\ApiException::class); + + $this->fakeHttpClient->setResponse(new Response($statusCode, [], $responseBody)); + $this->api->getPetById(123); + } }