diff --git a/modules/swagger-codegen/src/main/resources/php/model_generic.mustache b/modules/swagger-codegen/src/main/resources/php/model_generic.mustache index c9300084b0f..ee90c746a68 100644 --- a/modules/swagger-codegen/src/main/resources/php/model_generic.mustache +++ b/modules/swagger-codegen/src/main/resources/php/model_generic.mustache @@ -205,13 +205,13 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa {{/isEnum}} {{#hasValidation}} {{#maxLength}} - if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(strlen($this->container['{{name}}']) > {{maxLength}})) { + if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(mb_strlen($this->container['{{name}}']) > {{maxLength}})) { $invalidProperties[] = "invalid value for '{{name}}', the character length must be smaller than or equal to {{{maxLength}}}."; } {{/maxLength}} {{#minLength}} - if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(strlen($this->container['{{name}}']) < {{minLength}})) { + if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(mb_strlen($this->container['{{name}}']) < {{minLength}})) { $invalidProperties[] = "invalid value for '{{name}}', the character length must be bigger than or equal to {{{minLength}}}."; } @@ -281,12 +281,12 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa {{/isEnum}} {{#hasValidation}} {{#maxLength}} - if (strlen($this->container['{{name}}']) > {{maxLength}}) { + if (mb_strlen($this->container['{{name}}']) > {{maxLength}}) { return false; } {{/maxLength}} {{#minLength}} - if (strlen($this->container['{{name}}']) < {{minLength}}) { + if (mb_strlen($this->container['{{name}}']) < {{minLength}}) { return false; } {{/minLength}} @@ -366,11 +366,11 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa {{/isEnum}} {{#hasValidation}} {{#maxLength}} - if ({{^required}}!is_null(${{name}}) && {{/required}}(strlen(${{name}}) > {{maxLength}})) { + if ({{^required}}!is_null(${{name}}) && {{/required}}(mb_strlen(${{name}}) > {{maxLength}})) { throw new \InvalidArgumentException('invalid length for ${{name}} when calling {{classname}}.{{operationId}}, must be smaller than or equal to {{maxLength}}.'); }{{/maxLength}} {{#minLength}} - if ({{^required}}!is_null(${{name}}) && {{/required}}(strlen(${{name}}) < {{minLength}})) { + if ({{^required}}!is_null(${{name}}) && {{/required}}(mb_strlen(${{name}}) < {{minLength}})) { throw new \InvalidArgumentException('invalid length for ${{name}} when calling {{classname}}.{{operationId}}, must be bigger than or equal to {{minLength}}.'); } {{/minLength}} diff --git a/samples/client/petstore-security-test/php/SwaggerClient-php/lib/ObjectSerializer.php b/samples/client/petstore-security-test/php/SwaggerClient-php/lib/ObjectSerializer.php index c3f4590bc51..720eb97a8e5 100644 --- a/samples/client/petstore-security-test/php/SwaggerClient-php/lib/ObjectSerializer.php +++ b/samples/client/petstore-security-test/php/SwaggerClient-php/lib/ObjectSerializer.php @@ -271,7 +271,7 @@ class ObjectSerializer // determine file name if (array_key_exists('Content-Disposition', $httpHeaders) && preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeaders['Content-Disposition'], $match)) { - $filename = Configuration::getDefaultConfiguration()->getTempFolderPath() . self::sanitizeFilename($match[1]); + $filename = Configuration::getDefaultConfiguration()->getTempFolderPath() . DIRECTORY_SEPARATOR . self::sanitizeFilename($match[1]); } else { $filename = tempnam(Configuration::getDefaultConfiguration()->getTempFolderPath(), ''); } 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/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/Model/FormatTest.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/FormatTest.php index a8c74e7b761..8e9d1e45237 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/FormatTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/FormatTest.php @@ -321,11 +321,11 @@ class FormatTest implements ModelInterface, ArrayAccess if ($this->container['password'] === null) { $invalidProperties[] = "'password' can't be null"; } - if ((strlen($this->container['password']) > 64)) { + if ((mb_strlen($this->container['password']) > 64)) { $invalidProperties[] = "invalid value for 'password', the character length must be smaller than or equal to 64."; } - if ((strlen($this->container['password']) < 10)) { + if ((mb_strlen($this->container['password']) < 10)) { $invalidProperties[] = "invalid value for 'password', the character length must be bigger than or equal to 10."; } @@ -389,10 +389,10 @@ class FormatTest implements ModelInterface, ArrayAccess if ($this->container['password'] === null) { return false; } - if (strlen($this->container['password']) > 64) { + if (mb_strlen($this->container['password']) > 64) { return false; } - if (strlen($this->container['password']) < 10) { + if (mb_strlen($this->container['password']) < 10) { return false; } return true; @@ -756,10 +756,10 @@ class FormatTest implements ModelInterface, ArrayAccess */ public function setPassword($password) { - if ((strlen($password) > 64)) { + if ((mb_strlen($password) > 64)) { throw new \InvalidArgumentException('invalid length for $password when calling FormatTest., must be smaller than or equal to 64.'); } - if ((strlen($password) < 10)) { + if ((mb_strlen($password) < 10)) { throw new \InvalidArgumentException('invalid length for $password when calling FormatTest., must be bigger than or equal to 10.'); } 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 @@ + $the64MultiByteStrings, + // mandatory parameters + 'number' => 500, + 'byte' => base64_encode('test'), + 'date' => new DateTime(), + ]); + + $this->assertEmpty($formatTest->listInvalidProperties()); + + // Pass the strings via setter. + // Throws InvalidArgumentException if it doesn't count the length correctly. + $formatTest->setPassword($the64MultiByteStrings); + } +}