diff --git a/modules/swagger-codegen/src/main/resources/php/api.mustache b/modules/swagger-codegen/src/main/resources/php/api.mustache index cf5064c94c2..ecaca5467f9 100644 --- a/modules/swagger-codegen/src/main/resources/php/api.mustache +++ b/modules/swagger-codegen/src/main/resources/php/api.mustache @@ -181,8 +181,12 @@ use {{invokerPackage}}\ObjectSerializer; {{#responses}} {{#dataType}} {{^isWildcard}}case {{code}}:{{/isWildcard}}{{#isWildcard}}default:{{/isWildcard}} + $content = $e->getResponseBody(); + if ('{{dataType}}' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '{{dataType}}', $e->getResponseHeaders() ); 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..4193042ed91 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/AnotherFakeApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/AnotherFakeApi.php @@ -167,8 +167,12 @@ public function testSpecialTagsWithHttpInfo($body) } catch (ApiException $e) { switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\Client' !== 'string') { + $content = json_decode($content); + } $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..a24fd708e39 --- /dev/null +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/DefaultApi.php @@ -0,0 +1,339 @@ +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) { + 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..11397058c86 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeApi.php @@ -163,8 +163,12 @@ public function fakeOuterBooleanSerializeWithHttpInfo($body = null) } catch (ApiException $e) { switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\OuterBoolean' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\OuterBoolean', $e->getResponseHeaders() ); @@ -410,8 +414,12 @@ public function fakeOuterCompositeSerializeWithHttpInfo($body = null) } catch (ApiException $e) { switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\OuterComposite' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\OuterComposite', $e->getResponseHeaders() ); @@ -657,8 +665,12 @@ public function fakeOuterNumberSerializeWithHttpInfo($body = null) } catch (ApiException $e) { switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\OuterNumber' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\OuterNumber', $e->getResponseHeaders() ); @@ -904,8 +916,12 @@ public function fakeOuterStringSerializeWithHttpInfo($body = null) } catch (ApiException $e) { switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\OuterString' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\OuterString', $e->getResponseHeaders() ); @@ -1155,8 +1171,12 @@ public function testClientModelWithHttpInfo($body) } catch (ApiException $e) { switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\Client' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\Client', $e->getResponseHeaders() ); 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..c4c31fc985b 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeClassnameTags123Api.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeClassnameTags123Api.php @@ -167,8 +167,12 @@ public function testClassnameWithHttpInfo($body) } catch (ApiException $e) { switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\Client' !== 'string') { + $content = json_decode($content); + } $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..4afa5616fb1 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php @@ -629,8 +629,12 @@ public function findPetsByStatusWithHttpInfo($status) } catch (ApiException $e) { switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\Pet[]' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\Pet[]', $e->getResponseHeaders() ); @@ -894,8 +898,12 @@ public function findPetsByTagsWithHttpInfo($tags) } catch (ApiException $e) { switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\Pet[]' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\Pet[]', $e->getResponseHeaders() ); @@ -1159,8 +1167,12 @@ public function getPetByIdWithHttpInfo($pet_id) } catch (ApiException $e) { switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\Pet' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\Pet', $e->getResponseHeaders() ); @@ -1901,8 +1913,12 @@ public function uploadFileWithHttpInfo($pet_id, $additional_metadata = null, $fi } catch (ApiException $e) { switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\ApiResponse' !== 'string') { + $content = json_decode($content); + } $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..18b5fde8b47 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php @@ -390,8 +390,12 @@ public function getInventoryWithHttpInfo() } catch (ApiException $e) { switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('map[string,int]' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, 'map[string,int]', $e->getResponseHeaders() ); @@ -640,8 +644,12 @@ public function getOrderByIdWithHttpInfo($order_id) } catch (ApiException $e) { switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\Order' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\Order', $e->getResponseHeaders() ); @@ -909,8 +917,12 @@ public function placeOrderWithHttpInfo($body) } catch (ApiException $e) { switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\Order' !== 'string') { + $content = json_decode($content); + } $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..45725dbc2ef 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/UserApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/UserApi.php @@ -1052,8 +1052,12 @@ public function getUserByNameWithHttpInfo($username) } catch (ApiException $e) { switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('\Swagger\Client\Model\User' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, '\Swagger\Client\Model\User', $e->getResponseHeaders() ); @@ -1316,8 +1320,12 @@ public function loginUserWithHttpInfo($username, $password) } catch (ApiException $e) { switch ($e->getCode()) { case 200: + $content = $e->getResponseBody(); + if ('string' !== 'string') { + $content = json_decode($content); + } $data = ObjectSerializer::deserialize( - $e->getResponseBody(), + $content, 'string', $e->getResponseHeaders() ); 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 @@ +