From 03c7e3ea0bc1b83d456c4f15f6c26f5a74e04d5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20J=C3=B8rgensen?= Date: Fri, 5 Aug 2016 10:35:11 +0200 Subject: [PATCH 1/2] [PHP] Fix enum validation in container setters When the setter is taking an array as argument we cannot use `in_array()` to validate if the values are part of the allowed enum values. Instead we use `array_diff()` for containers now. --- .../src/main/resources/php/model_generic.mustache | 7 +++++++ 1 file changed, 7 insertions(+) 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 27f66e8683b..c0ab1cd47e0 100644 --- a/modules/swagger-codegen/src/main/resources/php/model_generic.mustache +++ b/modules/swagger-codegen/src/main/resources/php/model_generic.mustache @@ -238,9 +238,16 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple { {{#isEnum}} $allowed_values = array({{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}); + {{^isContainer}} if (!in_array(${{{name}}}, $allowed_values)) { throw new \InvalidArgumentException("Invalid value for '{{name}}', must be one of {{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}"); } + {{/isContainer}} + {{#isContainer}} + if (array_diff(${{{name}}}, $allowed_values)) { + throw new \InvalidArgumentException("Invalid value for '{{name}}', must be one of {{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}"); + } + {{/isContainer}} {{/isEnum}} {{#hasValidation}} {{#maxLength}} From 40080542091369b02b66d87d838f37d80291fc6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20J=C3=B8rgensen?= Date: Fri, 5 Aug 2016 11:25:49 +0200 Subject: [PATCH 2/2] [PHP] Regenerate petstore samples --- .../php/SwaggerClient-php/README.md | 2 +- .../petstore/php/SwaggerClient-php/README.md | 3 +- .../docs/Model/EnumArrays.md | 2 +- .../lib/Model/EnumArrays.php | 52 +++++++++---------- .../lib/Model/FormatTest.php | 13 +++++ .../SwaggerClient-php/lib/Model/MapTest.php | 2 +- .../lib/ObjectSerializer.php | 2 +- 7 files changed, 45 insertions(+), 31 deletions(-) diff --git a/samples/client/petstore-security-test/php/SwaggerClient-php/README.md b/samples/client/petstore-security-test/php/SwaggerClient-php/README.md index f574d0cd8e1..baa8eb42188 100644 --- a/samples/client/petstore-security-test/php/SwaggerClient-php/README.md +++ b/samples/client/petstore-security-test/php/SwaggerClient-php/README.md @@ -4,7 +4,7 @@ This spec is mainly for testing Petstore server and contains fake endpoints, mod This PHP package is automatically generated by the [Swagger Codegen](https://github.com/swagger-api/swagger-codegen) project: - API version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r -- Build date: 2016-08-01T11:48:23.775+02:00 +- Build date: 2016-08-05T11:24:40.650+02:00 - Build package: class io.swagger.codegen.languages.PhpClientCodegen ## Requirements diff --git a/samples/client/petstore/php/SwaggerClient-php/README.md b/samples/client/petstore/php/SwaggerClient-php/README.md index 5371cc2160a..992cd394e4a 100644 --- a/samples/client/petstore/php/SwaggerClient-php/README.md +++ b/samples/client/petstore/php/SwaggerClient-php/README.md @@ -4,7 +4,7 @@ This spec is mainly for testing Petstore server and contains fake endpoints, mod This PHP package is automatically generated by the [Swagger Codegen](https://github.com/swagger-api/swagger-codegen) project: - API version: 1.0.0 -- Build date: 2016-08-01T11:48:17.791+02:00 +- Build date: 2016-08-05T11:24:33.550+02:00 - Build package: class io.swagger.codegen.languages.PhpClientCodegen ## Requirements @@ -114,6 +114,7 @@ Class | Method | HTTP request | Description - [Category](docs/Model/Category.md) - [Client](docs/Model/Client.md) - [Dog](docs/Model/Dog.md) + - [EnumArrays](docs/Model/EnumArrays.md) - [EnumClass](docs/Model/EnumClass.md) - [EnumTest](docs/Model/EnumTest.md) - [FormatTest](docs/Model/FormatTest.md) diff --git a/samples/client/petstore/php/SwaggerClient-php/docs/Model/EnumArrays.md b/samples/client/petstore/php/SwaggerClient-php/docs/Model/EnumArrays.md index b3b0c1574ff..ebaedecdae0 100644 --- a/samples/client/petstore/php/SwaggerClient-php/docs/Model/EnumArrays.md +++ b/samples/client/petstore/php/SwaggerClient-php/docs/Model/EnumArrays.md @@ -3,7 +3,7 @@ ## Properties Name | Type | Description | Notes ------------ | ------------- | ------------- | ------------- -**just_enum** | **string** | | [optional] +**just_symbol** | **string** | | [optional] **array_enum** | **string[]** | | [optional] [[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumArrays.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumArrays.php index f245fc28df4..dc24e411cb8 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumArrays.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumArrays.php @@ -66,7 +66,7 @@ class EnumArrays implements ArrayAccess * @var string[] */ protected static $swaggerTypes = array( - 'just_enum' => 'string', + 'just_symbol' => 'string', 'array_enum' => 'string[]' ); @@ -80,7 +80,7 @@ class EnumArrays implements ArrayAccess * @var string[] */ protected static $attributeMap = array( - 'just_enum' => 'just_enum', + 'just_symbol' => 'just_symbol', 'array_enum' => 'array_enum' ); @@ -94,7 +94,7 @@ class EnumArrays implements ArrayAccess * @var string[] */ protected static $setters = array( - 'just_enum' => 'setJustEnum', + 'just_symbol' => 'setJustSymbol', 'array_enum' => 'setArrayEnum' ); @@ -108,7 +108,7 @@ class EnumArrays implements ArrayAccess * @var string[] */ protected static $getters = array( - 'just_enum' => 'getJustEnum', + 'just_symbol' => 'getJustSymbol', 'array_enum' => 'getArrayEnum' ); @@ -117,8 +117,8 @@ class EnumArrays implements ArrayAccess return self::$getters; } - const JUST_ENUM_BIRD = 'bird'; - const JUST_ENUM_EAGLE = 'eagle'; + const JUST_SYMBOL_ = '>='; + const JUST_SYMBOL_ = '$'; const ARRAY_ENUM_FISH = 'fish'; const ARRAY_ENUM_CRAB = 'crab'; @@ -128,11 +128,11 @@ class EnumArrays implements ArrayAccess * Gets allowable values of the enum * @return string[] */ - public function getJustEnumAllowableValues() + public function getJustSymbolAllowableValues() { return [ - self::JUST_ENUM_BIRD, - self::JUST_ENUM_EAGLE, + self::JUST_SYMBOL_, + self::JUST_SYMBOL_, ]; } @@ -161,7 +161,7 @@ class EnumArrays implements ArrayAccess */ public function __construct(array $data = null) { - $this->container['just_enum'] = isset($data['just_enum']) ? $data['just_enum'] : null; + $this->container['just_symbol'] = isset($data['just_symbol']) ? $data['just_symbol'] : null; $this->container['array_enum'] = isset($data['array_enum']) ? $data['array_enum'] : null; } @@ -173,9 +173,9 @@ class EnumArrays implements ArrayAccess public function listInvalidProperties() { $invalid_properties = array(); - $allowed_values = array("bird", "eagle"); - if (!in_array($this->container['just_enum'], $allowed_values)) { - $invalid_properties[] = "invalid value for 'just_enum', must be one of #{allowed_values}."; + $allowed_values = array(">=", "$"); + if (!in_array($this->container['just_symbol'], $allowed_values)) { + $invalid_properties[] = "invalid value for 'just_symbol', must be one of #{allowed_values}."; } return $invalid_properties; @@ -189,8 +189,8 @@ class EnumArrays implements ArrayAccess */ public function valid() { - $allowed_values = array("bird", "eagle"); - if (!in_array($this->container['just_enum'], $allowed_values)) { + $allowed_values = array(">=", "$"); + if (!in_array($this->container['just_symbol'], $allowed_values)) { return false; } return true; @@ -198,26 +198,26 @@ class EnumArrays implements ArrayAccess /** - * Gets just_enum + * Gets just_symbol * @return string */ - public function getJustEnum() + public function getJustSymbol() { - return $this->container['just_enum']; + return $this->container['just_symbol']; } /** - * Sets just_enum - * @param string $just_enum + * Sets just_symbol + * @param string $just_symbol * @return $this */ - public function setJustEnum($just_enum) + public function setJustSymbol($just_symbol) { - $allowed_values = array('bird', 'eagle'); - if (!in_array($just_enum, $allowed_values)) { - throw new \InvalidArgumentException("Invalid value for 'just_enum', must be one of 'bird', 'eagle'"); + $allowed_values = array('>=', '$'); + if (!in_array($just_symbol, $allowed_values)) { + throw new \InvalidArgumentException("Invalid value for 'just_symbol', must be one of '>=', '$'"); } - $this->container['just_enum'] = $just_enum; + $this->container['just_symbol'] = $just_symbol; return $this; } @@ -239,7 +239,7 @@ class EnumArrays implements ArrayAccess public function setArrayEnum($array_enum) { $allowed_values = array('fish', 'crab'); - if (!in_array($array_enum, $allowed_values)) { + if (array_diff($array_enum, $allowed_values)) { throw new \InvalidArgumentException("Invalid value for 'array_enum', must be one of 'fish', 'crab'"); } $this->container['array_enum'] = $array_enum; 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 bbc34b97907..ca6187e6a21 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/FormatTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/FormatTest.php @@ -203,39 +203,50 @@ class FormatTest implements ArrayAccess if (!is_null($this->container['integer']) && ($this->container['integer'] > 100.0)) { $invalid_properties[] = "invalid value for 'integer', must be smaller than or equal to 100.0."; } + if (!is_null($this->container['integer']) && ($this->container['integer'] < 10.0)) { $invalid_properties[] = "invalid value for 'integer', must be bigger than or equal to 10.0."; } + if (!is_null($this->container['int32']) && ($this->container['int32'] > 200.0)) { $invalid_properties[] = "invalid value for 'int32', must be smaller than or equal to 200.0."; } + if (!is_null($this->container['int32']) && ($this->container['int32'] < 20.0)) { $invalid_properties[] = "invalid value for 'int32', must be bigger than or equal to 20.0."; } + if ($this->container['number'] === null) { $invalid_properties[] = "'number' can't be null"; } if (($this->container['number'] > 543.2)) { $invalid_properties[] = "invalid value for 'number', must be smaller than or equal to 543.2."; } + if (($this->container['number'] < 32.1)) { $invalid_properties[] = "invalid value for 'number', must be bigger than or equal to 32.1."; } + if (!is_null($this->container['float']) && ($this->container['float'] > 987.6)) { $invalid_properties[] = "invalid value for 'float', must be smaller than or equal to 987.6."; } + if (!is_null($this->container['float']) && ($this->container['float'] < 54.3)) { $invalid_properties[] = "invalid value for 'float', must be bigger than or equal to 54.3."; } + if (!is_null($this->container['double']) && ($this->container['double'] > 123.4)) { $invalid_properties[] = "invalid value for 'double', must be smaller than or equal to 123.4."; } + if (!is_null($this->container['double']) && ($this->container['double'] < 67.8)) { $invalid_properties[] = "invalid value for 'double', must be bigger than or equal to 67.8."; } + if (!is_null($this->container['string']) && !preg_match("/[a-z]/i", $this->container['string'])) { $invalid_properties[] = "invalid value for 'string', must be conform to the pattern /[a-z]/i."; } + if ($this->container['byte'] === null) { $invalid_properties[] = "'byte' can't be null"; } @@ -248,9 +259,11 @@ class FormatTest implements ArrayAccess if ((strlen($this->container['password']) > 64)) { $invalid_properties[] = "invalid value for 'password', the character length must be smaller than or equal to 64."; } + if ((strlen($this->container['password']) < 10)) { $invalid_properties[] = "invalid value for 'password', the character length must be bigger than or equal to 10."; } + return $invalid_properties; } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/MapTest.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/MapTest.php index 0ff51e87971..f706034736a 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/MapTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/MapTest.php @@ -212,7 +212,7 @@ class MapTest implements ArrayAccess public function setMapOfEnumString($map_of_enum_string) { $allowed_values = array('UPPER', 'lower'); - if (!in_array($map_of_enum_string, $allowed_values)) { + if (array_diff($map_of_enum_string, $allowed_values)) { throw new \InvalidArgumentException("Invalid value for 'map_of_enum_string', must be one of 'UPPER', 'lower'"); } $this->container['map_of_enum_string'] = $map_of_enum_string; diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php b/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php index fe68a3877d6..ce77aa6c3b3 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php @@ -264,7 +264,7 @@ class ObjectSerializer } else { return null; } - } elseif (in_array($class, array('integer', 'int', 'void', 'number', 'object', 'double', 'float', 'byte', 'DateTime', 'string', 'mixed', 'boolean', 'bool'))) { + } elseif (in_array($class, array('void', 'bool', 'string', 'double', 'byte', 'mixed', 'integer', 'float', 'int', 'DateTime', 'number', 'boolean', 'object'))) { settype($data, $class); return $data; } elseif ($class === '\SplFileObject') {