From 31a3436a8e556e3fba650ef1cef2a31b6ee6151a Mon Sep 17 00:00:00 2001 From: Xavi Aparicio Date: Wed, 14 Sep 2016 13:41:05 +0200 Subject: [PATCH] Added !is_null condition in setter conditions for non-required properties --- .../main/resources/php/model_generic.mustache | 18 ++++++------- .../lib/Model/EnumArrays.php | 4 +-- .../SwaggerClient-php/lib/Model/EnumTest.php | 6 ++--- .../lib/Model/FormatTest.php | 26 +++++++++---------- .../SwaggerClient-php/lib/Model/MapTest.php | 2 +- .../php/SwaggerClient-php/lib/Model/Order.php | 2 +- .../php/SwaggerClient-php/lib/Model/Pet.php | 2 +- 7 files changed, 30 insertions(+), 30 deletions(-) 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 47f79771a61..072e88ad55d 100644 --- a/modules/swagger-codegen/src/main/resources/php/model_generic.mustache +++ b/modules/swagger-codegen/src/main/resources/php/model_generic.mustache @@ -261,47 +261,47 @@ 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)) { + if ({{^required}}!is_null(${{name}}) && {{/required}}(!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)) { + if ({{^required}}!is_null(${{name}}) && {{/required}}(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}} - if (strlen(${{name}}) > {{maxLength}}) { + if ({{^required}}!is_null(${{name}}) && {{/required}}(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 (strlen(${{name}}) < {{minLength}}) { + if ({{^required}}!is_null(${{name}}) && {{/required}}(strlen(${{name}}) < {{minLength}})) { throw new \InvalidArgumentException('invalid length for ${{name}} when calling {{classname}}.{{operationId}}, must be bigger than or equal to {{minLength}}.'); } {{/minLength}} {{#maximum}} - if (${{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}) { + if ({{^required}}!is_null(${{name}}) && {{/required}}(${{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}})) { throw new \InvalidArgumentException('invalid value for ${{name}} when calling {{classname}}.{{operationId}}, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}.'); } {{/maximum}} {{#minimum}} - if (${{name}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}) { + if ({{^required}}!is_null(${{name}}) && {{/required}}(${{name}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}})) { throw new \InvalidArgumentException('invalid value for ${{name}} when calling {{classname}}.{{operationId}}, must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}.'); } {{/minimum}} {{#pattern}} - if (!preg_match("{{{pattern}}}", ${{name}})) { + if ({{^required}}!is_null(${{name}}) && {{/required}}(!preg_match("{{{pattern}}}", ${{name}}))) { throw new \InvalidArgumentException("invalid value for ${{name}} when calling {{classname}}.{{operationId}}, must conform to the pattern {{{pattern}}}."); } {{/pattern}} {{#maxItems}} - if (count(${{name}}) > {{maxItems}}) { + if ({{^required}}!is_null(${{name}}) && {{/required}}(count(${{name}}) > {{maxItems}})) { throw new \InvalidArgumentException('invalid value for ${{name}} when calling {{classname}}.{{operationId}}, number of items must be less than or equal to {{maxItems}}.'); }{{/maxItems}} {{#minItems}} - if (count(${{name}}) < {{minItems}}) { + if ({{^required}}!is_null(${{name}}) && {{/required}}(count(${{name}}) < {{minItems}})) { throw new \InvalidArgumentException('invalid length for ${{name}} when calling {{classname}}.{{operationId}}, number of items must be greater than or equal to {{minItems}}.'); } {{/minItems}} 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 9b9e6f999be..77334dfdee9 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumArrays.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumArrays.php @@ -214,7 +214,7 @@ class EnumArrays implements ArrayAccess public function setJustSymbol($just_symbol) { $allowed_values = array('>=', '$'); - if (!in_array($just_symbol, $allowed_values)) { + if (!is_null($just_symbol) && (!in_array($just_symbol, $allowed_values))) { throw new \InvalidArgumentException("Invalid value for 'just_symbol', must be one of '>=', '$'"); } $this->container['just_symbol'] = $just_symbol; @@ -239,7 +239,7 @@ class EnumArrays implements ArrayAccess public function setArrayEnum($array_enum) { $allowed_values = array('fish', 'crab'); - if (array_diff($array_enum, $allowed_values)) { + if (!is_null($array_enum) && (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/EnumTest.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumTest.php index 758c953f4c3..cf927c088cf 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumTest.php @@ -251,7 +251,7 @@ class EnumTest implements ArrayAccess public function setEnumString($enum_string) { $allowed_values = array('UPPER', 'lower'); - if (!in_array($enum_string, $allowed_values)) { + if (!is_null($enum_string) && (!in_array($enum_string, $allowed_values))) { throw new \InvalidArgumentException("Invalid value for 'enum_string', must be one of 'UPPER', 'lower'"); } $this->container['enum_string'] = $enum_string; @@ -276,7 +276,7 @@ class EnumTest implements ArrayAccess public function setEnumInteger($enum_integer) { $allowed_values = array('1', '-1'); - if (!in_array($enum_integer, $allowed_values)) { + if (!is_null($enum_integer) && (!in_array($enum_integer, $allowed_values))) { throw new \InvalidArgumentException("Invalid value for 'enum_integer', must be one of '1', '-1'"); } $this->container['enum_integer'] = $enum_integer; @@ -301,7 +301,7 @@ class EnumTest implements ArrayAccess public function setEnumNumber($enum_number) { $allowed_values = array('1.1', '-1.2'); - if (!in_array($enum_number, $allowed_values)) { + if (!is_null($enum_number) && (!in_array($enum_number, $allowed_values))) { throw new \InvalidArgumentException("Invalid value for 'enum_number', must be one of '1.1', '-1.2'"); } $this->container['enum_number'] = $enum_number; 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 9fd635feb00..d97ea3b8b8b 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/FormatTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/FormatTest.php @@ -347,10 +347,10 @@ class FormatTest implements ArrayAccess public function setInteger($integer) { - if ($integer > 100.0) { + if (!is_null($integer) && ($integer > 100.0)) { throw new \InvalidArgumentException('invalid value for $integer when calling FormatTest., must be smaller than or equal to 100.0.'); } - if ($integer < 10.0) { + if (!is_null($integer) && ($integer < 10.0)) { throw new \InvalidArgumentException('invalid value for $integer when calling FormatTest., must be bigger than or equal to 10.0.'); } @@ -376,10 +376,10 @@ class FormatTest implements ArrayAccess public function setInt32($int32) { - if ($int32 > 200.0) { + if (!is_null($int32) && ($int32 > 200.0)) { throw new \InvalidArgumentException('invalid value for $int32 when calling FormatTest., must be smaller than or equal to 200.0.'); } - if ($int32 < 20.0) { + if (!is_null($int32) && ($int32 < 20.0)) { throw new \InvalidArgumentException('invalid value for $int32 when calling FormatTest., must be bigger than or equal to 20.0.'); } @@ -426,10 +426,10 @@ class FormatTest implements ArrayAccess public function setNumber($number) { - if ($number > 543.2) { + if (($number > 543.2)) { throw new \InvalidArgumentException('invalid value for $number when calling FormatTest., must be smaller than or equal to 543.2.'); } - if ($number < 32.1) { + if (($number < 32.1)) { throw new \InvalidArgumentException('invalid value for $number when calling FormatTest., must be bigger than or equal to 32.1.'); } @@ -455,10 +455,10 @@ class FormatTest implements ArrayAccess public function setFloat($float) { - if ($float > 987.6) { + if (!is_null($float) && ($float > 987.6)) { throw new \InvalidArgumentException('invalid value for $float when calling FormatTest., must be smaller than or equal to 987.6.'); } - if ($float < 54.3) { + if (!is_null($float) && ($float < 54.3)) { throw new \InvalidArgumentException('invalid value for $float when calling FormatTest., must be bigger than or equal to 54.3.'); } @@ -484,10 +484,10 @@ class FormatTest implements ArrayAccess public function setDouble($double) { - if ($double > 123.4) { + if (!is_null($double) && ($double > 123.4)) { throw new \InvalidArgumentException('invalid value for $double when calling FormatTest., must be smaller than or equal to 123.4.'); } - if ($double < 67.8) { + if (!is_null($double) && ($double < 67.8)) { throw new \InvalidArgumentException('invalid value for $double when calling FormatTest., must be bigger than or equal to 67.8.'); } @@ -513,7 +513,7 @@ class FormatTest implements ArrayAccess public function setString($string) { - if (!preg_match("/[a-z]/i", $string)) { + if (!is_null($string) && (!preg_match("/[a-z]/i", $string))) { throw new \InvalidArgumentException("invalid value for $string when calling FormatTest., must conform to the pattern /[a-z]/i."); } @@ -643,10 +643,10 @@ class FormatTest implements ArrayAccess */ public function setPassword($password) { - if (strlen($password) > 64) { + if ((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 ((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/lib/Model/MapTest.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/MapTest.php index a66e65b72ae..2c464db1fc1 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 (array_diff($map_of_enum_string, $allowed_values)) { + if (!is_null($map_of_enum_string) && (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/Model/Order.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Order.php index a91971f18a6..f530d91f930 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Order.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Order.php @@ -306,7 +306,7 @@ class Order implements ArrayAccess public function setStatus($status) { $allowed_values = array('placed', 'approved', 'delivered'); - if (!in_array($status, $allowed_values)) { + if (!is_null($status) && (!in_array($status, $allowed_values))) { throw new \InvalidArgumentException("Invalid value for 'status', must be one of 'placed', 'approved', 'delivered'"); } $this->container['status'] = $status; diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php index 493ef0be3c5..e3248c87ee9 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php @@ -339,7 +339,7 @@ class Pet implements ArrayAccess public function setStatus($status) { $allowed_values = array('available', 'pending', 'sold'); - if (!in_array($status, $allowed_values)) { + if (!is_null($status) && (!in_array($status, $allowed_values))) { throw new \InvalidArgumentException("Invalid value for 'status', must be one of 'available', 'pending', 'sold'"); } $this->container['status'] = $status;