Skip to content

Commit

Permalink
[PHP] Non required enum property (#7723)
Browse files Browse the repository at this point in the history
* Add required enum property

* Update samples

* Add test case which reproduce the problem

refs swagger-api/swagger-codegen#7686 (comment)
> 2. Non-required enum property is listed as invalid when omitted

* If the property is not empty, perform validation

* Update samples

* Use is_null() according to setter implementation

refs https://github.com/ackintosh/swagger-codegen/blob/377247f125988b591685e9f7cedc95fe5e99bcc2/modules/swagger-codegen/src/main/resources/php/model_generic.mustache#L347

* Update samples
  • Loading branch information
ackintosh authored and wing328 committed Mar 8, 2018
1 parent 227e245 commit 32cf2f1
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1225,13 +1225,21 @@ definitions:
- (xyz)
Enum_Test:
type: object
required:
- enum_string_required
properties:
enum_string:
type: string
enum:
- UPPER
- lower
- ''
enum_string_required:
type: string
enum:
- UPPER
- lower
- ''
enum_integer:
type: integer
format: int32
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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;
Expand All @@ -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
*
Expand Down Expand Up @@ -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;
Expand All @@ -255,23 +278,34 @@ 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)
);
}

$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)
Expand All @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 32cf2f1

Please sign in to comment.