forked from loafle/openapi-generator-original
[PHP] Non required enum property (#7723)
* Add required enum property
* Update samples
* Add test case which reproduce the problem
refs https://github.com/swagger-api/swagger-codegen/pull/7686#issuecomment-368200011
> 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 377247f125/modules/swagger-codegen/src/main/resources/php/model_generic.mustache (L347)
* Update samples
This commit is contained in:
parent
227e2458d4
commit
32cf2f16f5
@ -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}}
|
||||
|
@ -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
|
||||
|
@ -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]
|
||||
|
@ -226,7 +226,7 @@ class EnumArrays implements ModelInterface, ArrayAccess
|
||||
$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 @@ class EnumArrays implements ModelInterface, ArrayAccess
|
||||
{
|
||||
|
||||
$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;
|
||||
|
@ -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 @@ class EnumTest implements ModelInterface, ArrayAccess
|
||||
*/
|
||||
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 @@ class EnumTest implements ModelInterface, ArrayAccess
|
||||
*/
|
||||
protected static $setters = [
|
||||
'enum_string' => 'setEnumString',
|
||||
'enum_string_required' => 'setEnumStringRequired',
|
||||
'enum_integer' => 'setEnumInteger',
|
||||
'enum_number' => 'setEnumNumber',
|
||||
'outer_enum' => 'setOuterEnum'
|
||||
@ -127,6 +131,7 @@ class EnumTest implements ModelInterface, ArrayAccess
|
||||
*/
|
||||
protected static $getters = [
|
||||
'enum_string' => 'getEnumString',
|
||||
'enum_string_required' => 'getEnumStringRequired',
|
||||
'enum_integer' => 'getEnumInteger',
|
||||
'enum_number' => 'getEnumNumber',
|
||||
'outer_enum' => 'getOuterEnum'
|
||||
@ -176,6 +181,9 @@ class EnumTest implements ModelInterface, ArrayAccess
|
||||
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 @@ class EnumTest implements ModelInterface, ArrayAccess
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 @@ class EnumTest implements ModelInterface, ArrayAccess
|
||||
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 @@ class EnumTest implements ModelInterface, ArrayAccess
|
||||
$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 @@ class EnumTest implements ModelInterface, ArrayAccess
|
||||
}
|
||||
|
||||
$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 @@ class EnumTest implements ModelInterface, ArrayAccess
|
||||
{
|
||||
|
||||
$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 @@ class EnumTest implements ModelInterface, ArrayAccess
|
||||
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
|
||||
*
|
||||
|
@ -237,7 +237,7 @@ class Order implements ModelInterface, ArrayAccess
|
||||
$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 @@ class Order implements ModelInterface, ArrayAccess
|
||||
{
|
||||
|
||||
$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;
|
||||
|
@ -243,7 +243,7 @@ class Pet implements ModelInterface, ArrayAccess
|
||||
$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 @@ class Pet implements ModelInterface, ArrayAccess
|
||||
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;
|
||||
|
@ -15,4 +15,20 @@ class EnumTestTest extends \PHPUnit_Framework_TestCase
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user