From afb528a30462c6d81e0f11d32fed349d9e9c55aa Mon Sep 17 00:00:00 2001 From: Yann Milin Date: Wed, 4 Apr 2018 15:30:58 +0200 Subject: [PATCH 1/3] [PHP] Fix deserialize ApiException as a Model --- modules/swagger-codegen/src/main/resources/php/api.mustache | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/swagger-codegen/src/main/resources/php/api.mustache b/modules/swagger-codegen/src/main/resources/php/api.mustache index cf5064c94c2..db30697748d 100644 --- a/modules/swagger-codegen/src/main/resources/php/api.mustache +++ b/modules/swagger-codegen/src/main/resources/php/api.mustache @@ -177,12 +177,16 @@ use {{invokerPackage}}\ObjectSerializer; {{/returnType}} } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { {{#responses}} {{#dataType}} {{^isWildcard}}case {{code}}:{{/isWildcard}}{{#isWildcard}}default:{{/isWildcard}} $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '{{dataType}}', $e->getResponseHeaders() ); From b131bfa55b81660b0702b41a73ee1f2db2284ebb Mon Sep 17 00:00:00 2001 From: Yann Milin Date: Wed, 4 Apr 2018 15:57:00 +0200 Subject: [PATCH 2/3] [PHP] Update sample files --- .../petstore/php/SwaggerClient-php/README.md | 1 + .../SwaggerClient-php/docs/Api/DefaultApi.md | 57 +++ .../lib/Api/AnotherFakeApi.php | 6 +- .../SwaggerClient-php/lib/Api/DefaultApi.php | 343 ++++++++++++++++++ .../php/SwaggerClient-php/lib/Api/FakeApi.php | 46 ++- .../lib/Api/FakeClassnameTags123Api.php | 6 +- .../php/SwaggerClient-php/lib/Api/PetApi.php | 40 +- .../SwaggerClient-php/lib/Api/StoreApi.php | 22 +- .../php/SwaggerClient-php/lib/Api/UserApi.php | 36 +- .../test/Api/DefaultApiTest.php | 83 +++++ 10 files changed, 624 insertions(+), 16 deletions(-) create mode 100644 samples/client/petstore/php/SwaggerClient-php/docs/Api/DefaultApi.md create mode 100644 samples/client/petstore/php/SwaggerClient-php/lib/Api/DefaultApi.php create mode 100644 samples/client/petstore/php/SwaggerClient-php/test/Api/DefaultApiTest.php diff --git a/samples/client/petstore/php/SwaggerClient-php/README.md b/samples/client/petstore/php/SwaggerClient-php/README.md index 112c5b8ab08..2fd020297c7 100644 --- a/samples/client/petstore/php/SwaggerClient-php/README.md +++ b/samples/client/petstore/php/SwaggerClient-php/README.md @@ -80,6 +80,7 @@ All URIs are relative to *http://petstore.swagger.io:80/v2* Class | Method | HTTP request | Description ------------ | ------------- | ------------- | ------------- *AnotherFakeApi* | [**testSpecialTags**](docs/Api/AnotherFakeApi.md#testspecialtags) | **PATCH** /another-fake/dummy | To test special tags +*DefaultApi* | [**testBodyWithQueryParams**](docs/Api/DefaultApi.md#testbodywithqueryparams) | **PUT** /fake/body-with-query-params | *FakeApi* | [**fakeOuterBooleanSerialize**](docs/Api/FakeApi.md#fakeouterbooleanserialize) | **POST** /fake/outer/boolean | *FakeApi* | [**fakeOuterCompositeSerialize**](docs/Api/FakeApi.md#fakeoutercompositeserialize) | **POST** /fake/outer/composite | *FakeApi* | [**fakeOuterNumberSerialize**](docs/Api/FakeApi.md#fakeouternumberserialize) | **POST** /fake/outer/number | diff --git a/samples/client/petstore/php/SwaggerClient-php/docs/Api/DefaultApi.md b/samples/client/petstore/php/SwaggerClient-php/docs/Api/DefaultApi.md new file mode 100644 index 00000000000..0185322ccf2 --- /dev/null +++ b/samples/client/petstore/php/SwaggerClient-php/docs/Api/DefaultApi.md @@ -0,0 +1,57 @@ +# Swagger\Client\DefaultApi + +All URIs are relative to *http://petstore.swagger.io:80/v2* + +Method | HTTP request | Description +------------- | ------------- | ------------- +[**testBodyWithQueryParams**](DefaultApi.md#testBodyWithQueryParams) | **PUT** /fake/body-with-query-params | + + +# **testBodyWithQueryParams** +> testBodyWithQueryParams($body, $query) + + + +### Example +```php +testBodyWithQueryParams($body, $query); +} catch (Exception $e) { + echo 'Exception when calling DefaultApi->testBodyWithQueryParams: ', $e->getMessage(), PHP_EOL; +} +?> +``` + +### Parameters + +Name | Type | Description | Notes +------------- | ------------- | ------------- | ------------- + **body** | [**\Swagger\Client\Model\User**](../Model/User.md)| | + **query** | **string**| | + +### Return type + +void (empty response body) + +### Authorization + +No authorization required + +### HTTP request headers + + - **Content-Type**: application/json + - **Accept**: Not defined + +[[Back to top]](#) [[Back to API list]](../../README.md#documentation-for-api-endpoints) [[Back to Model list]](../../README.md#documentation-for-models) [[Back to README]](../../README.md) + diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/AnotherFakeApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/AnotherFakeApi.php index f3333c2a991..37c71f8ea09 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/AnotherFakeApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/AnotherFakeApi.php @@ -165,10 +165,14 @@ public function testSpecialTagsWithHttpInfo($body) ]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\Client', $e->getResponseHeaders() ); diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/DefaultApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/DefaultApi.php new file mode 100644 index 00000000000..d7b7caee36b --- /dev/null +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/DefaultApi.php @@ -0,0 +1,343 @@ +client = $client ?: new Client(); + $this->config = $config ?: new Configuration(); + $this->headerSelector = $selector ?: new HeaderSelector(); + } + + /** + * @return Configuration + */ + public function getConfig() + { + return $this->config; + } + + /** + * Operation testBodyWithQueryParams + * + * @param \Swagger\Client\Model\User $body body (required) + * @param string $query query (required) + * + * @throws \Swagger\Client\ApiException on non-2xx response + * @throws \InvalidArgumentException + * @return void + */ + public function testBodyWithQueryParams($body, $query) + { + $this->testBodyWithQueryParamsWithHttpInfo($body, $query); + } + + /** + * Operation testBodyWithQueryParamsWithHttpInfo + * + * @param \Swagger\Client\Model\User $body (required) + * @param string $query (required) + * + * @throws \Swagger\Client\ApiException on non-2xx response + * @throws \InvalidArgumentException + * @return array of null, HTTP status code, HTTP response headers (array of strings) + */ + public function testBodyWithQueryParamsWithHttpInfo($body, $query) + { + $returnType = ''; + $request = $this->testBodyWithQueryParamsRequest($body, $query); + + try { + $options = $this->createHttpClientOption(); + try { + $response = $this->client->send($request, $options); + } catch (RequestException $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $e->getCode(), + $e->getResponse() ? $e->getResponse()->getHeaders() : null, + $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null + ); + } + + $statusCode = $response->getStatusCode(); + + if ($statusCode < 200 || $statusCode > 299) { + throw new ApiException( + sprintf( + '[%d] Error connecting to the API (%s)', + $statusCode, + $request->getUri() + ), + $statusCode, + $response->getHeaders(), + $response->getBody() + ); + } + + return [null, $statusCode, $response->getHeaders()]; + + } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } + switch ($e->getCode()) { + } + throw $e; + } + } + + /** + * Operation testBodyWithQueryParamsAsync + * + * + * + * @param \Swagger\Client\Model\User $body (required) + * @param string $query (required) + * + * @throws \InvalidArgumentException + * @return \GuzzleHttp\Promise\PromiseInterface + */ + public function testBodyWithQueryParamsAsync($body, $query) + { + return $this->testBodyWithQueryParamsAsyncWithHttpInfo($body, $query) + ->then( + function ($response) { + return $response[0]; + } + ); + } + + /** + * Operation testBodyWithQueryParamsAsyncWithHttpInfo + * + * + * + * @param \Swagger\Client\Model\User $body (required) + * @param string $query (required) + * + * @throws \InvalidArgumentException + * @return \GuzzleHttp\Promise\PromiseInterface + */ + public function testBodyWithQueryParamsAsyncWithHttpInfo($body, $query) + { + $returnType = ''; + $request = $this->testBodyWithQueryParamsRequest($body, $query); + + return $this->client + ->sendAsync($request, $this->createHttpClientOption()) + ->then( + function ($response) use ($returnType) { + return [null, $response->getStatusCode(), $response->getHeaders()]; + }, + function ($exception) { + $response = $exception->getResponse(); + $statusCode = $response->getStatusCode(); + throw new ApiException( + sprintf( + '[%d] Error connecting to the API (%s)', + $statusCode, + $exception->getRequest()->getUri() + ), + $statusCode, + $response->getHeaders(), + $response->getBody() + ); + } + ); + } + + /** + * Create request for operation 'testBodyWithQueryParams' + * + * @param \Swagger\Client\Model\User $body (required) + * @param string $query (required) + * + * @throws \InvalidArgumentException + * @return \GuzzleHttp\Psr7\Request + */ + protected function testBodyWithQueryParamsRequest($body, $query) + { + // verify the required parameter 'body' is set + if ($body === null || (is_array($body) && count($body) === 0)) { + throw new \InvalidArgumentException( + 'Missing the required parameter $body when calling testBodyWithQueryParams' + ); + } + // verify the required parameter 'query' is set + if ($query === null || (is_array($query) && count($query) === 0)) { + throw new \InvalidArgumentException( + 'Missing the required parameter $query when calling testBodyWithQueryParams' + ); + } + + $resourcePath = '/fake/body-with-query-params'; + $formParams = []; + $queryParams = []; + $headerParams = []; + $httpBody = ''; + $multipart = false; + + // query params + if ($query !== null) { + $queryParams['query'] = ObjectSerializer::toQueryValue($query); + } + + + // body params + $_tempBody = null; + if (isset($body)) { + $_tempBody = $body; + } + + if ($multipart) { + $headers = $this->headerSelector->selectHeadersForMultipart( + [] + ); + } else { + $headers = $this->headerSelector->selectHeaders( + [], + ['application/json'] + ); + } + + // for model (json/xml) + if (isset($_tempBody)) { + // $_tempBody is the method argument, if present + $httpBody = $_tempBody; + // \stdClass has no __toString(), so we should encode it manually + if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') { + $httpBody = \GuzzleHttp\json_encode($httpBody); + } + } elseif (count($formParams) > 0) { + if ($multipart) { + $multipartContents = []; + foreach ($formParams as $formParamName => $formParamValue) { + $multipartContents[] = [ + 'name' => $formParamName, + 'contents' => $formParamValue + ]; + } + // for HTTP post (form) + $httpBody = new MultipartStream($multipartContents); + + } elseif ($headers['Content-Type'] === 'application/json') { + $httpBody = \GuzzleHttp\json_encode($formParams); + + } else { + // for HTTP post (form) + $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + } + } + + + $defaultHeaders = []; + if ($this->config->getUserAgent()) { + $defaultHeaders['User-Agent'] = $this->config->getUserAgent(); + } + + $headers = array_merge( + $defaultHeaders, + $headerParams, + $headers + ); + + $query = \GuzzleHttp\Psr7\build_query($queryParams); + return new Request( + 'PUT', + $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), + $headers, + $httpBody + ); + } + + /** + * Create http client option + * + * @throws \RuntimeException on file opening failure + * @return array of http client options + */ + protected function createHttpClientOption() + { + $options = []; + if ($this->config->getDebug()) { + $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); + if (!$options[RequestOptions::DEBUG]) { + throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); + } + } + + return $options; + } +} diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeApi.php index bb692f775b6..cdfea0829c3 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeApi.php @@ -161,10 +161,14 @@ public function fakeOuterBooleanSerializeWithHttpInfo($body = null) ]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\OuterBoolean', $e->getResponseHeaders() ); @@ -408,10 +412,14 @@ public function fakeOuterCompositeSerializeWithHttpInfo($body = null) ]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\OuterComposite', $e->getResponseHeaders() ); @@ -655,10 +663,14 @@ public function fakeOuterNumberSerializeWithHttpInfo($body = null) ]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\OuterNumber', $e->getResponseHeaders() ); @@ -902,10 +914,14 @@ public function fakeOuterStringSerializeWithHttpInfo($body = null) ]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\OuterString', $e->getResponseHeaders() ); @@ -1153,10 +1169,14 @@ public function testClientModelWithHttpInfo($body) ]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\Client', $e->getResponseHeaders() ); @@ -1421,6 +1441,10 @@ public function testEndpointParametersWithHttpInfo($number, $double, $pattern_wi return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { } throw $e; @@ -1816,6 +1840,10 @@ public function testEnumParametersWithHttpInfo($enum_form_string_array = null, $ return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { } throw $e; @@ -2086,6 +2114,10 @@ public function testInlineAdditionalPropertiesWithHttpInfo($param) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { } throw $e; @@ -2308,6 +2340,10 @@ public function testJsonFormDataWithHttpInfo($param, $param2) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { } throw $e; diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeClassnameTags123Api.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeClassnameTags123Api.php index 15b5320a3a3..e6578c57023 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeClassnameTags123Api.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeClassnameTags123Api.php @@ -165,10 +165,14 @@ public function testClassnameWithHttpInfo($body) ]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\Client', $e->getResponseHeaders() ); diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php index 300734be9d8..530406bce23 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php @@ -150,6 +150,10 @@ public function addPetWithHttpInfo($body) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { } throw $e; @@ -376,6 +380,10 @@ public function deletePetWithHttpInfo($pet_id, $api_key = null) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { } throw $e; @@ -627,10 +635,14 @@ public function findPetsByStatusWithHttpInfo($status) ]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\Pet[]', $e->getResponseHeaders() ); @@ -892,10 +904,14 @@ public function findPetsByTagsWithHttpInfo($tags) ]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\Pet[]', $e->getResponseHeaders() ); @@ -1157,10 +1173,14 @@ public function getPetByIdWithHttpInfo($pet_id) ]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\Pet', $e->getResponseHeaders() ); @@ -1409,6 +1429,10 @@ public function updatePetWithHttpInfo($body) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { } throw $e; @@ -1637,6 +1661,10 @@ public function updatePetWithFormWithHttpInfo($pet_id, $name = null, $status = n return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { } throw $e; @@ -1899,10 +1927,14 @@ public function uploadFileWithHttpInfo($pet_id, $additional_metadata = null, $fi ]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\ApiResponse', $e->getResponseHeaders() ); diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php index d3e827ab863..1ed685309b7 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php @@ -150,6 +150,10 @@ public function deleteOrderWithHttpInfo($order_id) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { } throw $e; @@ -388,10 +392,14 @@ public function getInventoryWithHttpInfo() ]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, 'map[string,int]', $e->getResponseHeaders() ); @@ -638,10 +646,14 @@ public function getOrderByIdWithHttpInfo($order_id) ]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\Order', $e->getResponseHeaders() ); @@ -907,10 +919,14 @@ public function placeOrderWithHttpInfo($body) ]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\Order', $e->getResponseHeaders() ); diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/UserApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/UserApi.php index 111f4e7ea79..55dd3946a12 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/UserApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/UserApi.php @@ -150,6 +150,10 @@ public function createUserWithHttpInfo($body) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { } throw $e; @@ -370,6 +374,10 @@ public function createUsersWithArrayInputWithHttpInfo($body) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { } throw $e; @@ -590,6 +598,10 @@ public function createUsersWithListInputWithHttpInfo($body) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { } throw $e; @@ -810,6 +822,10 @@ public function deleteUserWithHttpInfo($username) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { } throw $e; @@ -1050,10 +1066,14 @@ public function getUserByNameWithHttpInfo($username) ]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\User', $e->getResponseHeaders() ); @@ -1314,10 +1334,14 @@ public function loginUserWithHttpInfo($username, $password) ]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, 'string', $e->getResponseHeaders() ); @@ -1568,6 +1592,10 @@ public function logoutUserWithHttpInfo() return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { } throw $e; @@ -1778,6 +1806,10 @@ public function updateUserWithHttpInfo($username, $body) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { + $content = $e->getResponseBody(); + if ($returnType !== 'string') { + $content = json_decode($content); + } switch ($e->getCode()) { } throw $e; diff --git a/samples/client/petstore/php/SwaggerClient-php/test/Api/DefaultApiTest.php b/samples/client/petstore/php/SwaggerClient-php/test/Api/DefaultApiTest.php new file mode 100644 index 00000000000..f5d11998adf --- /dev/null +++ b/samples/client/petstore/php/SwaggerClient-php/test/Api/DefaultApiTest.php @@ -0,0 +1,83 @@ + Date: Mon, 9 Apr 2018 09:28:53 +0200 Subject: [PATCH 3/3] [PHP] Fix deserialize for default responses. Update samples --- .../src/main/resources/php/api.mustache | 8 +-- .../lib/Api/AnotherFakeApi.php | 8 +-- .../SwaggerClient-php/lib/Api/DefaultApi.php | 4 -- .../php/SwaggerClient-php/lib/Api/FakeApi.php | 56 +++++++------------ .../lib/Api/FakeClassnameTags123Api.php | 8 +-- .../php/SwaggerClient-php/lib/Api/PetApi.php | 48 ++++++---------- .../SwaggerClient-php/lib/Api/StoreApi.php | 28 ++++------ .../php/SwaggerClient-php/lib/Api/UserApi.php | 40 +++---------- 8 files changed, 68 insertions(+), 132 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/php/api.mustache b/modules/swagger-codegen/src/main/resources/php/api.mustache index db30697748d..ecaca5467f9 100644 --- a/modules/swagger-codegen/src/main/resources/php/api.mustache +++ b/modules/swagger-codegen/src/main/resources/php/api.mustache @@ -177,14 +177,14 @@ use {{invokerPackage}}\ObjectSerializer; {{/returnType}} } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { {{#responses}} {{#dataType}} {{^isWildcard}}case {{code}}:{{/isWildcard}}{{#isWildcard}}default:{{/isWildcard}} + $content = $e->getResponseBody(); + if ('{{dataType}}' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( $content, '{{dataType}}', diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/AnotherFakeApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/AnotherFakeApi.php index 37c71f8ea09..4193042ed91 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/AnotherFakeApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/AnotherFakeApi.php @@ -165,12 +165,12 @@ public function testSpecialTagsWithHttpInfo($body) ]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\Client' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( $content, '\Swagger\Client\Model\Client', diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/DefaultApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/DefaultApi.php index d7b7caee36b..a24fd708e39 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/DefaultApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/DefaultApi.php @@ -148,10 +148,6 @@ public function testBodyWithQueryParamsWithHttpInfo($body, $query) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { } throw $e; diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeApi.php index cdfea0829c3..11397058c86 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeApi.php @@ -161,12 +161,12 @@ public function fakeOuterBooleanSerializeWithHttpInfo($body = null) ]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\OuterBoolean' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( $content, '\Swagger\Client\Model\OuterBoolean', @@ -412,12 +412,12 @@ public function fakeOuterCompositeSerializeWithHttpInfo($body = null) ]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\OuterComposite' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( $content, '\Swagger\Client\Model\OuterComposite', @@ -663,12 +663,12 @@ public function fakeOuterNumberSerializeWithHttpInfo($body = null) ]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\OuterNumber' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( $content, '\Swagger\Client\Model\OuterNumber', @@ -914,12 +914,12 @@ public function fakeOuterStringSerializeWithHttpInfo($body = null) ]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\OuterString' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( $content, '\Swagger\Client\Model\OuterString', @@ -1169,12 +1169,12 @@ public function testClientModelWithHttpInfo($body) ]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\Client' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( $content, '\Swagger\Client\Model\Client', @@ -1441,10 +1441,6 @@ public function testEndpointParametersWithHttpInfo($number, $double, $pattern_wi return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { } throw $e; @@ -1840,10 +1836,6 @@ public function testEnumParametersWithHttpInfo($enum_form_string_array = null, $ return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { } throw $e; @@ -2114,10 +2106,6 @@ public function testInlineAdditionalPropertiesWithHttpInfo($param) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { } throw $e; @@ -2340,10 +2328,6 @@ public function testJsonFormDataWithHttpInfo($param, $param2) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { } throw $e; diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeClassnameTags123Api.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeClassnameTags123Api.php index e6578c57023..c4c31fc985b 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeClassnameTags123Api.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeClassnameTags123Api.php @@ -165,12 +165,12 @@ public function testClassnameWithHttpInfo($body) ]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\Client' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( $content, '\Swagger\Client\Model\Client', diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php index 530406bce23..4afa5616fb1 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php @@ -150,10 +150,6 @@ public function addPetWithHttpInfo($body) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { } throw $e; @@ -380,10 +376,6 @@ public function deletePetWithHttpInfo($pet_id, $api_key = null) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { } throw $e; @@ -635,12 +627,12 @@ public function findPetsByStatusWithHttpInfo($status) ]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\Pet[]' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( $content, '\Swagger\Client\Model\Pet[]', @@ -904,12 +896,12 @@ public function findPetsByTagsWithHttpInfo($tags) ]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\Pet[]' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( $content, '\Swagger\Client\Model\Pet[]', @@ -1173,12 +1165,12 @@ public function getPetByIdWithHttpInfo($pet_id) ]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\Pet' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( $content, '\Swagger\Client\Model\Pet', @@ -1429,10 +1421,6 @@ public function updatePetWithHttpInfo($body) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { } throw $e; @@ -1661,10 +1649,6 @@ public function updatePetWithFormWithHttpInfo($pet_id, $name = null, $status = n return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { } throw $e; @@ -1927,12 +1911,12 @@ public function uploadFileWithHttpInfo($pet_id, $additional_metadata = null, $fi ]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\ApiResponse' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( $content, '\Swagger\Client\Model\ApiResponse', diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php index 1ed685309b7..18b5fde8b47 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php @@ -150,10 +150,6 @@ public function deleteOrderWithHttpInfo($order_id) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { } throw $e; @@ -392,12 +388,12 @@ public function getInventoryWithHttpInfo() ]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('map[string,int]' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( $content, 'map[string,int]', @@ -646,12 +642,12 @@ public function getOrderByIdWithHttpInfo($order_id) ]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\Order' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( $content, '\Swagger\Client\Model\Order', @@ -919,12 +915,12 @@ public function placeOrderWithHttpInfo($body) ]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\Order' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( $content, '\Swagger\Client\Model\Order', diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/UserApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/UserApi.php index 55dd3946a12..45725dbc2ef 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/UserApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/UserApi.php @@ -150,10 +150,6 @@ public function createUserWithHttpInfo($body) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { } throw $e; @@ -374,10 +370,6 @@ public function createUsersWithArrayInputWithHttpInfo($body) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { } throw $e; @@ -598,10 +590,6 @@ public function createUsersWithListInputWithHttpInfo($body) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { } throw $e; @@ -822,10 +810,6 @@ public function deleteUserWithHttpInfo($username) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { } throw $e; @@ -1066,12 +1050,12 @@ public function getUserByNameWithHttpInfo($username) ]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\User' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( $content, '\Swagger\Client\Model\User', @@ -1334,12 +1318,12 @@ public function loginUserWithHttpInfo($username, $password) ]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('string' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( $content, 'string', @@ -1592,10 +1576,6 @@ public function logoutUserWithHttpInfo() return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { } throw $e; @@ -1806,10 +1786,6 @@ public function updateUserWithHttpInfo($username, $body) return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { - $content = $e->getResponseBody(); - if ($returnType !== 'string') { - $content = json_decode($content); - } switch ($e->getCode()) { } throw $e;