Skip to content

Commit

Permalink
[PHP] check if json_decode was able to decode response (#16879)
Browse files Browse the repository at this point in the history
* [PHP] check if json_decode was able to decode response

* use try/catch to check if json_decode failed
  • Loading branch information
individual-it committed Oct 31, 2023
1 parent 08d5183 commit 6cd73eb
Show file tree
Hide file tree
Showing 11 changed files with 692 additions and 134 deletions.
32 changes: 28 additions & 4 deletions modules/openapi-generator/src/main/resources/php/api.mustache
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 \{{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}}
Expand Down Expand Up @@ -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}}
Expand Down Expand Up @@ -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
);
}
}
}

Expand All @@ -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
);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function getConfig()
* @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
*/
Expand All @@ -148,7 +148,7 @@ public function call123TestSpecialTags($client, string $contentType = self::cont
* @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)
*/
Expand Down Expand Up @@ -198,7 +198,19 @@ public function call123TestSpecialTagsWithHttpInfo($client, string $contentType
} 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
);
}
}
}

Expand All @@ -215,7 +227,19 @@ public function call123TestSpecialTagsWithHttpInfo($client, string $contentType
} 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
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function getConfig()
*
* @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
*/
Expand All @@ -142,7 +142,7 @@ public function fooGet(string $contentType = self::contentTypes['fooGet'][0])
*
* @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)
*/
Expand Down Expand Up @@ -192,7 +192,19 @@ public function fooGetWithHttpInfo(string $contentType = self::contentTypes['foo
} 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
);
}
}
}

Expand All @@ -209,7 +221,19 @@ public function fooGetWithHttpInfo(string $contentType = self::contentTypes['foo
} 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 6cd73eb

Please sign in to comment.