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 eab63f63a9c8..ec90e6e3ab1e 100644 --- a/modules/swagger-codegen/src/main/resources/php/model_generic.mustache +++ b/modules/swagger-codegen/src/main/resources/php/model_generic.mustache @@ -194,7 +194,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa {{#isEnum}} {{^isContainer}} $allowedValues = $this->{{getter}}AllowableValues(); - if (!in_array($this->container['{{name}}'], $allowedValues)) { + if (!is_null($this->container['{{name}}']) && !in_array($this->container['{{name}}'], $allowedValues)) { $invalidProperties[] = sprintf( "invalid value for '{{name}}', must be one of '%s'", implode("', '", $allowedValues) @@ -274,7 +274,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa {{#isEnum}} {{^isContainer}} $allowedValues = $this->{{getter}}AllowableValues(); - if (!in_array($this->container['{{name}}'], $allowedValues)) { + if (!is_null($this->container['{{name}}']) && !in_array($this->container['{{name}}'], $allowedValues)) { return false; } {{/isContainer}} diff --git a/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml b/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml index 4dddaaa6c14e..bf15aa2900f5 100644 --- a/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml +++ b/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml @@ -1225,6 +1225,8 @@ definitions: - (xyz) Enum_Test: type: object + required: + - enum_string_required properties: enum_string: type: string @@ -1232,6 +1234,12 @@ definitions: - UPPER - lower - '' + enum_string_required: + type: string + enum: + - UPPER + - lower + - '' enum_integer: type: integer format: int32 diff --git a/samples/client/petstore/php/SwaggerClient-php/docs/Model/EnumTest.md b/samples/client/petstore/php/SwaggerClient-php/docs/Model/EnumTest.md index 4d0f96c720f5..9d0911aa9385 100644 --- a/samples/client/petstore/php/SwaggerClient-php/docs/Model/EnumTest.md +++ b/samples/client/petstore/php/SwaggerClient-php/docs/Model/EnumTest.md @@ -4,6 +4,7 @@ Name | Type | Description | Notes ------------ | ------------- | ------------- | ------------- **enum_string** | **string** | | [optional] +**enum_string_required** | **string** | | **enum_integer** | **int** | | [optional] **enum_number** | **double** | | [optional] **outer_enum** | [**\Swagger\Client\Model\OuterEnum**](OuterEnum.md) | | [optional] 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 0d548f2ad393..6720f6bb8c52 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumArrays.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumArrays.php @@ -226,7 +226,7 @@ public function listInvalidProperties() $invalidProperties = []; $allowedValues = $this->getJustSymbolAllowableValues(); - if (!in_array($this->container['just_symbol'], $allowedValues)) { + if (!is_null($this->container['just_symbol']) && !in_array($this->container['just_symbol'], $allowedValues)) { $invalidProperties[] = sprintf( "invalid value for 'just_symbol', must be one of '%s'", implode("', '", $allowedValues) @@ -246,7 +246,7 @@ public function valid() { $allowedValues = $this->getJustSymbolAllowableValues(); - if (!in_array($this->container['just_symbol'], $allowedValues)) { + if (!is_null($this->container['just_symbol']) && !in_array($this->container['just_symbol'], $allowedValues)) { return false; } return true; 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 87dcc3da6bd0..5c097931fd69 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumTest.php @@ -58,6 +58,7 @@ class EnumTest implements ModelInterface, ArrayAccess */ protected static $swaggerTypes = [ 'enum_string' => 'string', + 'enum_string_required' => 'string', 'enum_integer' => 'int', 'enum_number' => 'double', 'outer_enum' => '\Swagger\Client\Model\OuterEnum' @@ -70,6 +71,7 @@ class EnumTest implements ModelInterface, ArrayAccess */ protected static $swaggerFormats = [ 'enum_string' => null, + 'enum_string_required' => null, 'enum_integer' => 'int32', 'enum_number' => 'double', 'outer_enum' => null @@ -103,6 +105,7 @@ public static function swaggerFormats() */ protected static $attributeMap = [ 'enum_string' => 'enum_string', + 'enum_string_required' => 'enum_string_required', 'enum_integer' => 'enum_integer', 'enum_number' => 'enum_number', 'outer_enum' => 'outerEnum' @@ -115,6 +118,7 @@ public static function swaggerFormats() */ protected static $setters = [ 'enum_string' => 'setEnumString', + 'enum_string_required' => 'setEnumStringRequired', 'enum_integer' => 'setEnumInteger', 'enum_number' => 'setEnumNumber', 'outer_enum' => 'setOuterEnum' @@ -127,6 +131,7 @@ public static function swaggerFormats() */ protected static $getters = [ 'enum_string' => 'getEnumString', + 'enum_string_required' => 'getEnumStringRequired', 'enum_integer' => 'getEnumInteger', 'enum_number' => 'getEnumNumber', 'outer_enum' => 'getOuterEnum' @@ -176,6 +181,9 @@ public function getModelName() const ENUM_STRING_UPPER = 'UPPER'; const ENUM_STRING_LOWER = 'lower'; const ENUM_STRING_EMPTY = ''; + const ENUM_STRING_REQUIRED_UPPER = 'UPPER'; + const ENUM_STRING_REQUIRED_LOWER = 'lower'; + const ENUM_STRING_REQUIRED_EMPTY = ''; const ENUM_INTEGER_1 = 1; const ENUM_INTEGER_MINUS_1 = -1; const ENUM_NUMBER_1_DOT_1 = 1.1; @@ -197,6 +205,20 @@ public function getEnumStringAllowableValues() ]; } + /** + * Gets allowable values of the enum + * + * @return string[] + */ + public function getEnumStringRequiredAllowableValues() + { + return [ + self::ENUM_STRING_REQUIRED_UPPER, + self::ENUM_STRING_REQUIRED_LOWER, + self::ENUM_STRING_REQUIRED_EMPTY, + ]; + } + /** * Gets allowable values of the enum * @@ -240,6 +262,7 @@ public function getEnumNumberAllowableValues() public function __construct(array $data = null) { $this->container['enum_string'] = isset($data['enum_string']) ? $data['enum_string'] : null; + $this->container['enum_string_required'] = isset($data['enum_string_required']) ? $data['enum_string_required'] : null; $this->container['enum_integer'] = isset($data['enum_integer']) ? $data['enum_integer'] : null; $this->container['enum_number'] = isset($data['enum_number']) ? $data['enum_number'] : null; $this->container['outer_enum'] = isset($data['outer_enum']) ? $data['outer_enum'] : null; @@ -255,15 +278,26 @@ public function listInvalidProperties() $invalidProperties = []; $allowedValues = $this->getEnumStringAllowableValues(); - if (!in_array($this->container['enum_string'], $allowedValues)) { + if (!is_null($this->container['enum_string']) && !in_array($this->container['enum_string'], $allowedValues)) { $invalidProperties[] = sprintf( "invalid value for 'enum_string', must be one of '%s'", implode("', '", $allowedValues) ); } + if ($this->container['enum_string_required'] === null) { + $invalidProperties[] = "'enum_string_required' can't be null"; + } + $allowedValues = $this->getEnumStringRequiredAllowableValues(); + if (!is_null($this->container['enum_string_required']) && !in_array($this->container['enum_string_required'], $allowedValues)) { + $invalidProperties[] = sprintf( + "invalid value for 'enum_string_required', must be one of '%s'", + implode("', '", $allowedValues) + ); + } + $allowedValues = $this->getEnumIntegerAllowableValues(); - if (!in_array($this->container['enum_integer'], $allowedValues)) { + if (!is_null($this->container['enum_integer']) && !in_array($this->container['enum_integer'], $allowedValues)) { $invalidProperties[] = sprintf( "invalid value for 'enum_integer', must be one of '%s'", implode("', '", $allowedValues) @@ -271,7 +305,7 @@ public function listInvalidProperties() } $allowedValues = $this->getEnumNumberAllowableValues(); - if (!in_array($this->container['enum_number'], $allowedValues)) { + if (!is_null($this->container['enum_number']) && !in_array($this->container['enum_number'], $allowedValues)) { $invalidProperties[] = sprintf( "invalid value for 'enum_number', must be one of '%s'", implode("', '", $allowedValues) @@ -291,15 +325,22 @@ public function valid() { $allowedValues = $this->getEnumStringAllowableValues(); - if (!in_array($this->container['enum_string'], $allowedValues)) { + if (!is_null($this->container['enum_string']) && !in_array($this->container['enum_string'], $allowedValues)) { + return false; + } + if ($this->container['enum_string_required'] === null) { + return false; + } + $allowedValues = $this->getEnumStringRequiredAllowableValues(); + if (!is_null($this->container['enum_string_required']) && !in_array($this->container['enum_string_required'], $allowedValues)) { return false; } $allowedValues = $this->getEnumIntegerAllowableValues(); - if (!in_array($this->container['enum_integer'], $allowedValues)) { + if (!is_null($this->container['enum_integer']) && !in_array($this->container['enum_integer'], $allowedValues)) { return false; } $allowedValues = $this->getEnumNumberAllowableValues(); - if (!in_array($this->container['enum_number'], $allowedValues)) { + if (!is_null($this->container['enum_number']) && !in_array($this->container['enum_number'], $allowedValues)) { return false; } return true; @@ -339,6 +380,39 @@ public function setEnumString($enum_string) return $this; } + /** + * Gets enum_string_required + * + * @return string + */ + public function getEnumStringRequired() + { + return $this->container['enum_string_required']; + } + + /** + * Sets enum_string_required + * + * @param string $enum_string_required enum_string_required + * + * @return $this + */ + public function setEnumStringRequired($enum_string_required) + { + $allowedValues = $this->getEnumStringRequiredAllowableValues(); + if (!in_array($enum_string_required, $allowedValues)) { + throw new \InvalidArgumentException( + sprintf( + "Invalid value for 'enum_string_required', must be one of '%s'", + implode("', '", $allowedValues) + ) + ); + } + $this->container['enum_string_required'] = $enum_string_required; + + return $this; + } + /** * Gets enum_integer * 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 60831bb91d6b..0089f45bcf74 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Order.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Order.php @@ -237,7 +237,7 @@ public function listInvalidProperties() $invalidProperties = []; $allowedValues = $this->getStatusAllowableValues(); - if (!in_array($this->container['status'], $allowedValues)) { + if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues)) { $invalidProperties[] = sprintf( "invalid value for 'status', must be one of '%s'", implode("', '", $allowedValues) @@ -257,7 +257,7 @@ public function valid() { $allowedValues = $this->getStatusAllowableValues(); - if (!in_array($this->container['status'], $allowedValues)) { + if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues)) { return false; } return true; 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 5369903fbfa1..73e73c892fcb 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php @@ -243,7 +243,7 @@ public function listInvalidProperties() $invalidProperties[] = "'photo_urls' can't be null"; } $allowedValues = $this->getStatusAllowableValues(); - if (!in_array($this->container['status'], $allowedValues)) { + if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues)) { $invalidProperties[] = sprintf( "invalid value for 'status', must be one of '%s'", implode("', '", $allowedValues) @@ -269,7 +269,7 @@ public function valid() return false; } $allowedValues = $this->getStatusAllowableValues(); - if (!in_array($this->container['status'], $allowedValues)) { + if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues)) { return false; } return true; diff --git a/samples/client/petstore/php/SwaggerClient-php/tests/EnumTestTest.php b/samples/client/petstore/php/SwaggerClient-php/tests/EnumTestTest.php index 6db05c5b61d5..9e4ea5892515 100644 --- a/samples/client/petstore/php/SwaggerClient-php/tests/EnumTestTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/tests/EnumTestTest.php @@ -15,4 +15,20 @@ public function testPossibleValues() $this->assertSame(EnumTest::ENUM_NUMBER_1_DOT_1, 1.1); $this->assertSame(EnumTest::ENUM_NUMBER_MINUS_1_DOT_2, -1.2); } + + public function testNonRequiredPropertyIsOptional() + { + $enum = new EnumTest([ + 'enum_string_required' => 'UPPER', + ]); + $this->assertSame([], $enum->listInvalidProperties()); + $this->assertTrue($enum->valid()); + } + + public function testRequiredProperty() + { + $enum = new EnumTest(); + $this->assertSame(["'enum_string_required' can't be null"], $enum->listInvalidProperties()); + $this->assertFalse($enum->valid()); + } }