Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHP-NG] check if json_decode was able to decode response #17120

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading