[PHP] Improve validation on empty arrays (#7686)

* Api should throw exception if an empty array is passed

* Update samples

- bin/php-petstore.sh
- bin/security/php-petstore.sh
This commit is contained in:
Akihito Nakano 2018-02-22 18:15:00 +09:00 committed by William Cheng
parent 6a81829bb3
commit 14e1e19803
9 changed files with 34 additions and 31 deletions

View File

@ -299,7 +299,7 @@ use {{invokerPackage}}\ObjectSerializer;
{{#allParams}} {{#allParams}}
{{#required}} {{#required}}
// verify the required parameter '{{paramName}}' is set // verify the required parameter '{{paramName}}' is set
if (${{paramName}} === null) { if (${{paramName}} === null || (is_array(${{paramName}}) && count(${{paramName}}) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter ${{paramName}} when calling {{operationId}}' 'Missing the required parameter ${{paramName}} when calling {{operationId}}'
); );

View File

@ -12,6 +12,8 @@ Method | HTTP request | Description
To test class name in snake case To test class name in snake case
To test class name in snake case
### Example ### Example
```php ```php
<?php <?php

View File

@ -262,7 +262,7 @@ class AnotherFakeApi
protected function testSpecialTagsRequest($body) protected function testSpecialTagsRequest($body)
{ {
// verify the required parameter 'body' is set // verify the required parameter 'body' is set
if ($body === null) { if ($body === null || (is_array($body) && count($body) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $body when calling testSpecialTags' 'Missing the required parameter $body when calling testSpecialTags'
); );

View File

@ -1250,7 +1250,7 @@ class FakeApi
protected function testClientModelRequest($body) protected function testClientModelRequest($body)
{ {
// verify the required parameter 'body' is set // verify the required parameter 'body' is set
if ($body === null) { if ($body === null || (is_array($body) && count($body) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $body when calling testClientModel' 'Missing the required parameter $body when calling testClientModel'
); );
@ -1535,7 +1535,7 @@ class FakeApi
protected function testEndpointParametersRequest($number, $double, $pattern_without_delimiter, $byte, $integer = null, $int32 = null, $int64 = null, $float = null, $string = null, $binary = null, $date = null, $date_time = null, $password = null, $callback = null) protected function testEndpointParametersRequest($number, $double, $pattern_without_delimiter, $byte, $integer = null, $int32 = null, $int64 = null, $float = null, $string = null, $binary = null, $date = null, $date_time = null, $password = null, $callback = null)
{ {
// verify the required parameter 'number' is set // verify the required parameter 'number' is set
if ($number === null) { if ($number === null || (is_array($number) && count($number) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $number when calling testEndpointParameters' 'Missing the required parameter $number when calling testEndpointParameters'
); );
@ -1548,7 +1548,7 @@ class FakeApi
} }
// verify the required parameter 'double' is set // verify the required parameter 'double' is set
if ($double === null) { if ($double === null || (is_array($double) && count($double) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $double when calling testEndpointParameters' 'Missing the required parameter $double when calling testEndpointParameters'
); );
@ -1561,7 +1561,7 @@ class FakeApi
} }
// verify the required parameter 'pattern_without_delimiter' is set // verify the required parameter 'pattern_without_delimiter' is set
if ($pattern_without_delimiter === null) { if ($pattern_without_delimiter === null || (is_array($pattern_without_delimiter) && count($pattern_without_delimiter) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $pattern_without_delimiter when calling testEndpointParameters' 'Missing the required parameter $pattern_without_delimiter when calling testEndpointParameters'
); );
@ -1571,7 +1571,7 @@ class FakeApi
} }
// verify the required parameter 'byte' is set // verify the required parameter 'byte' is set
if ($byte === null) { if ($byte === null || (is_array($byte) && count($byte) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $byte when calling testEndpointParameters' 'Missing the required parameter $byte when calling testEndpointParameters'
); );
@ -2161,7 +2161,7 @@ class FakeApi
protected function testInlineAdditionalPropertiesRequest($param) protected function testInlineAdditionalPropertiesRequest($param)
{ {
// verify the required parameter 'param' is set // verify the required parameter 'param' is set
if ($param === null) { if ($param === null || (is_array($param) && count($param) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $param when calling testInlineAdditionalProperties' 'Missing the required parameter $param when calling testInlineAdditionalProperties'
); );
@ -2386,13 +2386,13 @@ class FakeApi
protected function testJsonFormDataRequest($param, $param2) protected function testJsonFormDataRequest($param, $param2)
{ {
// verify the required parameter 'param' is set // verify the required parameter 'param' is set
if ($param === null) { if ($param === null || (is_array($param) && count($param) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $param when calling testJsonFormData' 'Missing the required parameter $param when calling testJsonFormData'
); );
} }
// verify the required parameter 'param2' is set // verify the required parameter 'param2' is set
if ($param2 === null) { if ($param2 === null || (is_array($param2) && count($param2) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $param2 when calling testJsonFormData' 'Missing the required parameter $param2 when calling testJsonFormData'
); );

View File

@ -262,7 +262,7 @@ class FakeClassnameTags123Api
protected function testClassnameRequest($body) protected function testClassnameRequest($body)
{ {
// verify the required parameter 'body' is set // verify the required parameter 'body' is set
if ($body === null) { if ($body === null || (is_array($body) && count($body) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $body when calling testClassname' 'Missing the required parameter $body when calling testClassname'
); );

View File

@ -225,7 +225,7 @@ class PetApi
protected function addPetRequest($body) protected function addPetRequest($body)
{ {
// verify the required parameter 'body' is set // verify the required parameter 'body' is set
if ($body === null) { if ($body === null || (is_array($body) && count($body) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $body when calling addPet' 'Missing the required parameter $body when calling addPet'
); );
@ -454,7 +454,7 @@ class PetApi
protected function deletePetRequest($pet_id, $api_key = null) protected function deletePetRequest($pet_id, $api_key = null)
{ {
// verify the required parameter 'pet_id' is set // verify the required parameter 'pet_id' is set
if ($pet_id === null) { if ($pet_id === null || (is_array($pet_id) && count($pet_id) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $pet_id when calling deletePet' 'Missing the required parameter $pet_id when calling deletePet'
); );
@ -724,7 +724,7 @@ class PetApi
protected function findPetsByStatusRequest($status) protected function findPetsByStatusRequest($status)
{ {
// verify the required parameter 'status' is set // verify the required parameter 'status' is set
if ($status === null) { if ($status === null || (is_array($status) && count($status) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $status when calling findPetsByStatus' 'Missing the required parameter $status when calling findPetsByStatus'
); );
@ -989,7 +989,7 @@ class PetApi
protected function findPetsByTagsRequest($tags) protected function findPetsByTagsRequest($tags)
{ {
// verify the required parameter 'tags' is set // verify the required parameter 'tags' is set
if ($tags === null) { if ($tags === null || (is_array($tags) && count($tags) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $tags when calling findPetsByTags' 'Missing the required parameter $tags when calling findPetsByTags'
); );
@ -1254,7 +1254,7 @@ class PetApi
protected function getPetByIdRequest($pet_id) protected function getPetByIdRequest($pet_id)
{ {
// verify the required parameter 'pet_id' is set // verify the required parameter 'pet_id' is set
if ($pet_id === null) { if ($pet_id === null || (is_array($pet_id) && count($pet_id) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $pet_id when calling getPetById' 'Missing the required parameter $pet_id when calling getPetById'
); );
@ -1484,7 +1484,7 @@ class PetApi
protected function updatePetRequest($body) protected function updatePetRequest($body)
{ {
// verify the required parameter 'body' is set // verify the required parameter 'body' is set
if ($body === null) { if ($body === null || (is_array($body) && count($body) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $body when calling updatePet' 'Missing the required parameter $body when calling updatePet'
); );
@ -1718,7 +1718,7 @@ class PetApi
protected function updatePetWithFormRequest($pet_id, $name = null, $status = null) protected function updatePetWithFormRequest($pet_id, $name = null, $status = null)
{ {
// verify the required parameter 'pet_id' is set // verify the required parameter 'pet_id' is set
if ($pet_id === null) { if ($pet_id === null || (is_array($pet_id) && count($pet_id) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $pet_id when calling updatePetWithForm' 'Missing the required parameter $pet_id when calling updatePetWithForm'
); );
@ -2002,7 +2002,7 @@ class PetApi
protected function uploadFileRequest($pet_id, $additional_metadata = null, $file = null) protected function uploadFileRequest($pet_id, $additional_metadata = null, $file = null)
{ {
// verify the required parameter 'pet_id' is set // verify the required parameter 'pet_id' is set
if ($pet_id === null) { if ($pet_id === null || (is_array($pet_id) && count($pet_id) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $pet_id when calling uploadFile' 'Missing the required parameter $pet_id when calling uploadFile'
); );

View File

@ -225,7 +225,7 @@ class StoreApi
protected function deleteOrderRequest($order_id) protected function deleteOrderRequest($order_id)
{ {
// verify the required parameter 'order_id' is set // verify the required parameter 'order_id' is set
if ($order_id === null) { if ($order_id === null || (is_array($order_id) && count($order_id) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $order_id when calling deleteOrder' 'Missing the required parameter $order_id when calling deleteOrder'
); );
@ -735,7 +735,7 @@ class StoreApi
protected function getOrderByIdRequest($order_id) protected function getOrderByIdRequest($order_id)
{ {
// verify the required parameter 'order_id' is set // verify the required parameter 'order_id' is set
if ($order_id === null) { if ($order_id === null || (is_array($order_id) && count($order_id) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $order_id when calling getOrderById' 'Missing the required parameter $order_id when calling getOrderById'
); );
@ -1004,7 +1004,7 @@ class StoreApi
protected function placeOrderRequest($body) protected function placeOrderRequest($body)
{ {
// verify the required parameter 'body' is set // verify the required parameter 'body' is set
if ($body === null) { if ($body === null || (is_array($body) && count($body) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $body when calling placeOrder' 'Missing the required parameter $body when calling placeOrder'
); );

View File

@ -225,7 +225,7 @@ class UserApi
protected function createUserRequest($body) protected function createUserRequest($body)
{ {
// verify the required parameter 'body' is set // verify the required parameter 'body' is set
if ($body === null) { if ($body === null || (is_array($body) && count($body) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $body when calling createUser' 'Missing the required parameter $body when calling createUser'
); );
@ -445,7 +445,7 @@ class UserApi
protected function createUsersWithArrayInputRequest($body) protected function createUsersWithArrayInputRequest($body)
{ {
// verify the required parameter 'body' is set // verify the required parameter 'body' is set
if ($body === null) { if ($body === null || (is_array($body) && count($body) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $body when calling createUsersWithArrayInput' 'Missing the required parameter $body when calling createUsersWithArrayInput'
); );
@ -665,7 +665,7 @@ class UserApi
protected function createUsersWithListInputRequest($body) protected function createUsersWithListInputRequest($body)
{ {
// verify the required parameter 'body' is set // verify the required parameter 'body' is set
if ($body === null) { if ($body === null || (is_array($body) && count($body) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $body when calling createUsersWithListInput' 'Missing the required parameter $body when calling createUsersWithListInput'
); );
@ -885,7 +885,7 @@ class UserApi
protected function deleteUserRequest($username) protected function deleteUserRequest($username)
{ {
// verify the required parameter 'username' is set // verify the required parameter 'username' is set
if ($username === null) { if ($username === null || (is_array($username) && count($username) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $username when calling deleteUser' 'Missing the required parameter $username when calling deleteUser'
); );
@ -1147,7 +1147,7 @@ class UserApi
protected function getUserByNameRequest($username) protected function getUserByNameRequest($username)
{ {
// verify the required parameter 'username' is set // verify the required parameter 'username' is set
if ($username === null) { if ($username === null || (is_array($username) && count($username) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $username when calling getUserByName' 'Missing the required parameter $username when calling getUserByName'
); );
@ -1414,13 +1414,13 @@ class UserApi
protected function loginUserRequest($username, $password) protected function loginUserRequest($username, $password)
{ {
// verify the required parameter 'username' is set // verify the required parameter 'username' is set
if ($username === null) { if ($username === null || (is_array($username) && count($username) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $username when calling loginUser' 'Missing the required parameter $username when calling loginUser'
); );
} }
// verify the required parameter 'password' is set // verify the required parameter 'password' is set
if ($password === null) { if ($password === null || (is_array($password) && count($password) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $password when calling loginUser' 'Missing the required parameter $password when calling loginUser'
); );
@ -1856,13 +1856,13 @@ class UserApi
protected function updateUserRequest($username, $body) protected function updateUserRequest($username, $body)
{ {
// verify the required parameter 'username' is set // verify the required parameter 'username' is set
if ($username === null) { if ($username === null || (is_array($username) && count($username) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $username when calling updateUser' 'Missing the required parameter $username when calling updateUser'
); );
} }
// verify the required parameter 'body' is set // verify the required parameter 'body' is set
if ($body === null) { if ($body === null || (is_array($body) && count($body) === 0)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Missing the required parameter $body when calling updateUser' 'Missing the required parameter $body when calling updateUser'
); );

View File

@ -377,6 +377,7 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
* test invalid argument * test invalid argument
* *
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
* @expectedExceptionMessage Missing the required parameter $status when calling findPetsByStatus
*/ */
public function testInvalidArgument() public function testInvalidArgument()
{ {