Skip to content

Commit 4a5d16b

Browse files
ackintoshwing328
authored andcommitted
[PHP] Fix string length validation (#7953)
* Add a test case which reproduces the issue #7846 * Change `strlen` -> `mb_strlen` in order to count the length correctly * Regenerate the samples
1 parent 37faaf9 commit 4a5d16b

File tree

8 files changed

+519
-13
lines changed

8 files changed

+519
-13
lines changed

modules/swagger-codegen/src/main/resources/php/model_generic.mustache

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,13 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
205205
{{/isEnum}}
206206
{{#hasValidation}}
207207
{{#maxLength}}
208-
if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(strlen($this->container['{{name}}']) > {{maxLength}})) {
208+
if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(mb_strlen($this->container['{{name}}']) > {{maxLength}})) {
209209
$invalidProperties[] = "invalid value for '{{name}}', the character length must be smaller than or equal to {{{maxLength}}}.";
210210
}
211211

212212
{{/maxLength}}
213213
{{#minLength}}
214-
if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(strlen($this->container['{{name}}']) < {{minLength}})) {
214+
if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(mb_strlen($this->container['{{name}}']) < {{minLength}})) {
215215
$invalidProperties[] = "invalid value for '{{name}}', the character length must be bigger than or equal to {{{minLength}}}.";
216216
}
217217

@@ -281,12 +281,12 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
281281
{{/isEnum}}
282282
{{#hasValidation}}
283283
{{#maxLength}}
284-
if (strlen($this->container['{{name}}']) > {{maxLength}}) {
284+
if (mb_strlen($this->container['{{name}}']) > {{maxLength}}) {
285285
return false;
286286
}
287287
{{/maxLength}}
288288
{{#minLength}}
289-
if (strlen($this->container['{{name}}']) < {{minLength}}) {
289+
if (mb_strlen($this->container['{{name}}']) < {{minLength}}) {
290290
return false;
291291
}
292292
{{/minLength}}
@@ -366,11 +366,11 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
366366
{{/isEnum}}
367367
{{#hasValidation}}
368368
{{#maxLength}}
369-
if ({{^required}}!is_null(${{name}}) && {{/required}}(strlen(${{name}}) > {{maxLength}})) {
369+
if ({{^required}}!is_null(${{name}}) && {{/required}}(mb_strlen(${{name}}) > {{maxLength}})) {
370370
throw new \InvalidArgumentException('invalid length for ${{name}} when calling {{classname}}.{{operationId}}, must be smaller than or equal to {{maxLength}}.');
371371
}{{/maxLength}}
372372
{{#minLength}}
373-
if ({{^required}}!is_null(${{name}}) && {{/required}}(strlen(${{name}}) < {{minLength}})) {
373+
if ({{^required}}!is_null(${{name}}) && {{/required}}(mb_strlen(${{name}}) < {{minLength}})) {
374374
throw new \InvalidArgumentException('invalid length for ${{name}} when calling {{classname}}.{{operationId}}, must be bigger than or equal to {{minLength}}.');
375375
}
376376
{{/minLength}}

samples/client/petstore-security-test/php/SwaggerClient-php/lib/ObjectSerializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public static function deserialize($data, $class, $httpHeaders = null)
271271
// determine file name
272272
if (array_key_exists('Content-Disposition', $httpHeaders) &&
273273
preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeaders['Content-Disposition'], $match)) {
274-
$filename = Configuration::getDefaultConfiguration()->getTempFolderPath() . self::sanitizeFilename($match[1]);
274+
$filename = Configuration::getDefaultConfiguration()->getTempFolderPath() . DIRECTORY_SEPARATOR . self::sanitizeFilename($match[1]);
275275
} else {
276276
$filename = tempnam(Configuration::getDefaultConfiguration()->getTempFolderPath(), '');
277277
}

samples/client/petstore/php/SwaggerClient-php/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ All URIs are relative to *http://petstore.swagger.io:80/v2*
8080
Class | Method | HTTP request | Description
8181
------------ | ------------- | ------------- | -------------
8282
*AnotherFakeApi* | [**testSpecialTags**](docs/Api/AnotherFakeApi.md#testspecialtags) | **PATCH** /another-fake/dummy | To test special tags
83+
*DefaultApi* | [**testBodyWithQueryParams**](docs/Api/DefaultApi.md#testbodywithqueryparams) | **PUT** /fake/body-with-query-params |
8384
*FakeApi* | [**fakeOuterBooleanSerialize**](docs/Api/FakeApi.md#fakeouterbooleanserialize) | **POST** /fake/outer/boolean |
8485
*FakeApi* | [**fakeOuterCompositeSerialize**](docs/Api/FakeApi.md#fakeoutercompositeserialize) | **POST** /fake/outer/composite |
8586
*FakeApi* | [**fakeOuterNumberSerialize**](docs/Api/FakeApi.md#fakeouternumberserialize) | **POST** /fake/outer/number |
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Swagger\Client\DefaultApi
2+
3+
All URIs are relative to *http://petstore.swagger.io:80/v2*
4+
5+
Method | HTTP request | Description
6+
------------- | ------------- | -------------
7+
[**testBodyWithQueryParams**](DefaultApi.md#testBodyWithQueryParams) | **PUT** /fake/body-with-query-params |
8+
9+
10+
# **testBodyWithQueryParams**
11+
> testBodyWithQueryParams($body, $query)
12+
13+
14+
15+
### Example
16+
```php
17+
<?php
18+
require_once(__DIR__ . '/vendor/autoload.php');
19+
20+
$apiInstance = new Swagger\Client\Api\DefaultApi(
21+
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
22+
// This is optional, `GuzzleHttp\Client` will be used as default.
23+
new GuzzleHttp\Client()
24+
);
25+
$body = new \Swagger\Client\Model\User(); // \Swagger\Client\Model\User |
26+
$query = "query_example"; // string |
27+
28+
try {
29+
$apiInstance->testBodyWithQueryParams($body, $query);
30+
} catch (Exception $e) {
31+
echo 'Exception when calling DefaultApi->testBodyWithQueryParams: ', $e->getMessage(), PHP_EOL;
32+
}
33+
?>
34+
```
35+
36+
### Parameters
37+
38+
Name | Type | Description | Notes
39+
------------- | ------------- | ------------- | -------------
40+
**body** | [**\Swagger\Client\Model\User**](../Model/User.md)| |
41+
**query** | **string**| |
42+
43+
### Return type
44+
45+
void (empty response body)
46+
47+
### Authorization
48+
49+
No authorization required
50+
51+
### HTTP request headers
52+
53+
- **Content-Type**: application/json
54+
- **Accept**: Not defined
55+
56+
[[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)
57+

0 commit comments

Comments
 (0)