Skip to content

Commit f0906bd

Browse files
committed
Merge pull request #2733 from abcsun/php_parameter_validation
[PHP] add parameter validation in methord call
2 parents 6b3735e + 7212009 commit f0906bd

File tree

8 files changed

+569
-13
lines changed

8 files changed

+569
-13
lines changed

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,37 @@ use \{{invokerPackage}}\ObjectSerializer;
122122
// verify the required parameter '{{paramName}}' is set
123123
if (${{paramName}} === null) {
124124
throw new \InvalidArgumentException('Missing the required parameter ${{paramName}} when calling {{operationId}}');
125-
}{{/required}}{{/allParams}}
125+
}
126+
{{/required}}
127+
{{#hasValidation}}
128+
{{#maxLength}}
129+
if (strlen(${{paramName}}) > {{maxLength}}) {
130+
throw new \InvalidArgumentException('invalid length for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be smaller than or equal to {{maxLength}}.');
131+
}
132+
{{/maxLength}}
133+
{{#minLength}}
134+
if (strlen(${{paramName}}) > {{minLength}}) {
135+
throw new \InvalidArgumentException('invalid length for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be bigger than or equal to {{minLength}}.');
136+
}
137+
{{/minLength}}
138+
{{#maximum}}
139+
if (${{paramName}} > {{maximum}}) {
140+
throw new \InvalidArgumentException('invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be smaller than or equal to {{maximum}}.');
141+
}
142+
{{/maximum}}
143+
{{#minimum}}
144+
if (${{paramName}} < {{minimum}}) {
145+
throw new \InvalidArgumentException('invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be bigger than or equal to {{minimum}}.');
146+
}
147+
{{/minimum}}
148+
{{#pattern}}
149+
if (!preg_match("{{pattern}}", ${{paramName}})) {
150+
throw new \InvalidArgumentException('invalid value for "{{paramName}}" when calling {{classname}}.{{operationId}}, must conform to the pattern {{pattern}}.');
151+
}
152+
{{/pattern}}
153+
154+
{{/hasValidation}}
155+
{{/allParams}}
126156

127157
// parse inputs
128158
$resourcePath = "{{path}}";

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This PHP package is automatically generated by the [Swagger Codegen](https://git
55

66
- API version: 1.0.0
77
- Package version: 1.0.0
8-
- Build date: 2016-04-23T22:48:00.795+08:00
8+
- Build date: 2016-04-29T03:01:58.276Z
99
- Build package: class io.swagger.codegen.languages.PhpClientCodegen
1010

1111
## Requirements
@@ -58,16 +58,24 @@ Please follow the [installation procedure](#installation--usage) and then run th
5858
<?php
5959
require_once(__DIR__ . '/vendor/autoload.php');
6060

61-
// Configure OAuth2 access token for authorization: petstore_auth
62-
Swagger\Client\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');
63-
64-
$api_instance = new Swagger\Client\Api\PetApi();
65-
$body = new \Swagger\Client\Model\Pet(); // \Swagger\Client\Model\Pet | Pet object that needs to be added to the store
61+
$api_instance = new Swagger\Client\Api\FakeApi();
62+
$number = "number_example"; // string | None
63+
$double = 1.2; // double | None
64+
$string = "string_example"; // string | None
65+
$byte = "B"; // string | None
66+
$integer = 56; // int | None
67+
$int32 = 56; // int | None
68+
$int64 = 789; // int | None
69+
$float = 3.4; // float | None
70+
$binary = "B"; // string | None
71+
$date = new \DateTime(); // \DateTime | None
72+
$date_time = new \DateTime(); // \DateTime | None
73+
$password = "password_example"; // string | None
6674

6775
try {
68-
$api_instance->addPet($body);
76+
$api_instance->testEndpointParameters($number, $double, $string, $byte, $integer, $int32, $int64, $float, $binary, $date, $date_time, $password);
6977
} catch (Exception $e) {
70-
echo 'Exception when calling PetApi->addPet: ', $e->getMessage(), "\n";
78+
echo 'Exception when calling FakeApi->testEndpointParameters: ', $e->getMessage(), "\n";
7179
}
7280

7381
?>
@@ -79,6 +87,7 @@ All URIs are relative to *http://petstore.swagger.io/v2*
7987

8088
Class | Method | HTTP request | Description
8189
------------ | ------------- | ------------- | -------------
90+
*FakeApi* | [**testEndpointParameters**](docs/FakeApi.md#testendpointparameters) | **POST** /fake | Fake endpoint for testing various parameters
8291
*PetApi* | [**addPet**](docs/PetApi.md#addpet) | **POST** /pet | Add a new pet to the store
8392
*PetApi* | [**deletePet**](docs/PetApi.md#deletepet) | **DELETE** /pet/{petId} | Deletes a pet
8493
*PetApi* | [**findPetsByStatus**](docs/PetApi.md#findpetsbystatus) | **GET** /pet/findByStatus | Finds Pets by status
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Swagger\Client\FakeApi
2+
3+
All URIs are relative to *http://petstore.swagger.io/v2*
4+
5+
Method | HTTP request | Description
6+
------------- | ------------- | -------------
7+
[**testEndpointParameters**](FakeApi.md#testEndpointParameters) | **POST** /fake | Fake endpoint for testing various parameters
8+
9+
10+
# **testEndpointParameters**
11+
> testEndpointParameters($number, $double, $string, $byte, $integer, $int32, $int64, $float, $binary, $date, $date_time, $password)
12+
13+
Fake endpoint for testing various parameters
14+
15+
Fake endpoint for testing various parameters
16+
17+
### Example
18+
```php
19+
<?php
20+
require_once(__DIR__ . '/vendor/autoload.php');
21+
22+
$api_instance = new Swagger\Client\Api\FakeApi();
23+
$number = "number_example"; // string | None
24+
$double = 1.2; // double | None
25+
$string = "string_example"; // string | None
26+
$byte = "B"; // string | None
27+
$integer = 56; // int | None
28+
$int32 = 56; // int | None
29+
$int64 = 789; // int | None
30+
$float = 3.4; // float | None
31+
$binary = "B"; // string | None
32+
$date = new \DateTime(); // \DateTime | None
33+
$date_time = new \DateTime(); // \DateTime | None
34+
$password = "password_example"; // string | None
35+
36+
try {
37+
$api_instance->testEndpointParameters($number, $double, $string, $byte, $integer, $int32, $int64, $float, $binary, $date, $date_time, $password);
38+
} catch (Exception $e) {
39+
echo 'Exception when calling FakeApi->testEndpointParameters: ', $e->getMessage(), "\n";
40+
}
41+
?>
42+
```
43+
44+
### Parameters
45+
46+
Name | Type | Description | Notes
47+
------------- | ------------- | ------------- | -------------
48+
**number** | **string**| None |
49+
**double** | **double**| None |
50+
**string** | **string**| None |
51+
**byte** | **string**| None |
52+
**integer** | **int**| None | [optional]
53+
**int32** | **int**| None | [optional]
54+
**int64** | **int**| None | [optional]
55+
**float** | **float**| None | [optional]
56+
**binary** | **string**| None | [optional]
57+
**date** | **\DateTime**| None | [optional]
58+
**date_time** | **\DateTime**| None | [optional]
59+
**password** | **string**| None | [optional]
60+
61+
### Return type
62+
63+
void (empty response body)
64+
65+
### Authorization
66+
67+
No authorization required
68+
69+
### HTTP request headers
70+
71+
- **Content-Type**: Not defined
72+
- **Accept**: application/xml, application/json
73+
74+
[[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)
75+

0 commit comments

Comments
 (0)