Skip to content

Commit

Permalink
[PHP-NG] check if json_decode was able to decode response (#17120)
Browse files Browse the repository at this point in the history
  • Loading branch information
individual-it committed Nov 18, 2023
1 parent 195f27d commit f81d44b
Show file tree
Hide file tree
Showing 14 changed files with 1,278 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ApiException on non-2xx response
* @throws 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}}
Expand Down Expand Up @@ -237,7 +237,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 ApiException on non-2xx response
* @throws 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}}
Expand Down Expand Up @@ -311,7 +311,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
);
}
}
}

Expand All @@ -332,7 +344,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
);
}
}
}

Expand Down
64 changes: 56 additions & 8 deletions samples/client/echo_api/php-nextgen/src/Api/AuthApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function getConfig(): Configuration
*
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['testAuthHttpBasic'] to see the possible values for this operation
*
* @throws ApiException on non-2xx response
* @throws ApiException on non-2xx response or if the response body is not in the expected format
* @throws InvalidArgumentException
* @return string
*/
Expand All @@ -152,7 +152,7 @@ public function testAuthHttpBasic(
*
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['testAuthHttpBasic'] to see the possible values for this operation
*
* @throws ApiException on non-2xx response
* @throws 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)
*/
Expand Down Expand Up @@ -204,7 +204,19 @@ public function testAuthHttpBasicWithHttpInfo(
} 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
);
}
}
}

Expand All @@ -221,7 +233,19 @@ public function testAuthHttpBasicWithHttpInfo(
} 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
);
}
}
}

Expand Down Expand Up @@ -410,7 +434,7 @@ public function testAuthHttpBasicRequest(
*
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['testAuthHttpBearer'] to see the possible values for this operation
*
* @throws ApiException on non-2xx response
* @throws ApiException on non-2xx response or if the response body is not in the expected format
* @throws InvalidArgumentException
* @return string
*/
Expand All @@ -429,7 +453,7 @@ public function testAuthHttpBearer(
*
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['testAuthHttpBearer'] to see the possible values for this operation
*
* @throws ApiException on non-2xx response
* @throws 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)
*/
Expand Down Expand Up @@ -481,7 +505,19 @@ public function testAuthHttpBearerWithHttpInfo(
} 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
);
}
}
}

Expand All @@ -498,7 +534,19 @@ public function testAuthHttpBearerWithHttpInfo(
} 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
);
}
}
}

Expand Down
Loading

0 comments on commit f81d44b

Please sign in to comment.