[PHP] Improve: Make validation strict (#7724)

* Add test case which reproduce the problem

refs https://github.com/swagger-api/swagger-codegen/pull/7686#issuecomment-368200011
> 1. We should pass true as 3rd argument of in_array()

* Add test case for setter

* Strict validation

* Update samples

* Tweak expected value according to changes in #7723
This commit is contained in:
Akihito Nakano 2018-03-26 00:17:46 +09:00 committed by William Cheng
parent 6d88d073ca
commit d58835e571
8 changed files with 57 additions and 26 deletions

View File

@ -194,7 +194,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
{{#isEnum}} {{#isEnum}}
{{^isContainer}} {{^isContainer}}
$allowedValues = $this->{{getter}}AllowableValues(); $allowedValues = $this->{{getter}}AllowableValues();
if (!is_null($this->container['{{name}}']) && !in_array($this->container['{{name}}'], $allowedValues)) { if (!is_null($this->container['{{name}}']) && !in_array($this->container['{{name}}'], $allowedValues, true)) {
$invalidProperties[] = sprintf( $invalidProperties[] = sprintf(
"invalid value for '{{name}}', must be one of '%s'", "invalid value for '{{name}}', must be one of '%s'",
implode("', '", $allowedValues) implode("', '", $allowedValues)
@ -274,7 +274,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
{{#isEnum}} {{#isEnum}}
{{^isContainer}} {{^isContainer}}
$allowedValues = $this->{{getter}}AllowableValues(); $allowedValues = $this->{{getter}}AllowableValues();
if (!is_null($this->container['{{name}}']) && !in_array($this->container['{{name}}'], $allowedValues)) { if (!is_null($this->container['{{name}}']) && !in_array($this->container['{{name}}'], $allowedValues, true)) {
return false; return false;
} }
{{/isContainer}} {{/isContainer}}
@ -344,7 +344,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
{{#isEnum}} {{#isEnum}}
$allowedValues = $this->{{getter}}AllowableValues(); $allowedValues = $this->{{getter}}AllowableValues();
{{^isContainer}} {{^isContainer}}
if ({{^required}}!is_null(${{name}}) && {{/required}}!in_array(${{{name}}}, $allowedValues)) { if ({{^required}}!is_null(${{name}}) && {{/required}}!in_array(${{{name}}}, $allowedValues, true)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
sprintf( sprintf(
"Invalid value for '{{name}}', must be one of '%s'", "Invalid value for '{{name}}', must be one of '%s'",

View File

@ -226,7 +226,7 @@ class EnumArrays implements ModelInterface, ArrayAccess
$invalidProperties = []; $invalidProperties = [];
$allowedValues = $this->getJustSymbolAllowableValues(); $allowedValues = $this->getJustSymbolAllowableValues();
if (!is_null($this->container['just_symbol']) && !in_array($this->container['just_symbol'], $allowedValues)) { if (!is_null($this->container['just_symbol']) && !in_array($this->container['just_symbol'], $allowedValues, true)) {
$invalidProperties[] = sprintf( $invalidProperties[] = sprintf(
"invalid value for 'just_symbol', must be one of '%s'", "invalid value for 'just_symbol', must be one of '%s'",
implode("', '", $allowedValues) implode("', '", $allowedValues)
@ -246,7 +246,7 @@ class EnumArrays implements ModelInterface, ArrayAccess
{ {
$allowedValues = $this->getJustSymbolAllowableValues(); $allowedValues = $this->getJustSymbolAllowableValues();
if (!is_null($this->container['just_symbol']) && !in_array($this->container['just_symbol'], $allowedValues)) { if (!is_null($this->container['just_symbol']) && !in_array($this->container['just_symbol'], $allowedValues, true)) {
return false; return false;
} }
return true; return true;
@ -273,7 +273,7 @@ class EnumArrays implements ModelInterface, ArrayAccess
public function setJustSymbol($just_symbol) public function setJustSymbol($just_symbol)
{ {
$allowedValues = $this->getJustSymbolAllowableValues(); $allowedValues = $this->getJustSymbolAllowableValues();
if (!is_null($just_symbol) && !in_array($just_symbol, $allowedValues)) { if (!is_null($just_symbol) && !in_array($just_symbol, $allowedValues, true)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
sprintf( sprintf(
"Invalid value for 'just_symbol', must be one of '%s'", "Invalid value for 'just_symbol', must be one of '%s'",

View File

@ -278,7 +278,7 @@ class EnumTest implements ModelInterface, ArrayAccess
$invalidProperties = []; $invalidProperties = [];
$allowedValues = $this->getEnumStringAllowableValues(); $allowedValues = $this->getEnumStringAllowableValues();
if (!is_null($this->container['enum_string']) && !in_array($this->container['enum_string'], $allowedValues)) { if (!is_null($this->container['enum_string']) && !in_array($this->container['enum_string'], $allowedValues, true)) {
$invalidProperties[] = sprintf( $invalidProperties[] = sprintf(
"invalid value for 'enum_string', must be one of '%s'", "invalid value for 'enum_string', must be one of '%s'",
implode("', '", $allowedValues) implode("', '", $allowedValues)
@ -289,7 +289,7 @@ class EnumTest implements ModelInterface, ArrayAccess
$invalidProperties[] = "'enum_string_required' can't be null"; $invalidProperties[] = "'enum_string_required' can't be null";
} }
$allowedValues = $this->getEnumStringRequiredAllowableValues(); $allowedValues = $this->getEnumStringRequiredAllowableValues();
if (!is_null($this->container['enum_string_required']) && !in_array($this->container['enum_string_required'], $allowedValues)) { if (!is_null($this->container['enum_string_required']) && !in_array($this->container['enum_string_required'], $allowedValues, true)) {
$invalidProperties[] = sprintf( $invalidProperties[] = sprintf(
"invalid value for 'enum_string_required', must be one of '%s'", "invalid value for 'enum_string_required', must be one of '%s'",
implode("', '", $allowedValues) implode("', '", $allowedValues)
@ -297,7 +297,7 @@ class EnumTest implements ModelInterface, ArrayAccess
} }
$allowedValues = $this->getEnumIntegerAllowableValues(); $allowedValues = $this->getEnumIntegerAllowableValues();
if (!is_null($this->container['enum_integer']) && !in_array($this->container['enum_integer'], $allowedValues)) { if (!is_null($this->container['enum_integer']) && !in_array($this->container['enum_integer'], $allowedValues, true)) {
$invalidProperties[] = sprintf( $invalidProperties[] = sprintf(
"invalid value for 'enum_integer', must be one of '%s'", "invalid value for 'enum_integer', must be one of '%s'",
implode("', '", $allowedValues) implode("', '", $allowedValues)
@ -305,7 +305,7 @@ class EnumTest implements ModelInterface, ArrayAccess
} }
$allowedValues = $this->getEnumNumberAllowableValues(); $allowedValues = $this->getEnumNumberAllowableValues();
if (!is_null($this->container['enum_number']) && !in_array($this->container['enum_number'], $allowedValues)) { if (!is_null($this->container['enum_number']) && !in_array($this->container['enum_number'], $allowedValues, true)) {
$invalidProperties[] = sprintf( $invalidProperties[] = sprintf(
"invalid value for 'enum_number', must be one of '%s'", "invalid value for 'enum_number', must be one of '%s'",
implode("', '", $allowedValues) implode("', '", $allowedValues)
@ -325,22 +325,22 @@ class EnumTest implements ModelInterface, ArrayAccess
{ {
$allowedValues = $this->getEnumStringAllowableValues(); $allowedValues = $this->getEnumStringAllowableValues();
if (!is_null($this->container['enum_string']) && !in_array($this->container['enum_string'], $allowedValues)) { if (!is_null($this->container['enum_string']) && !in_array($this->container['enum_string'], $allowedValues, true)) {
return false; return false;
} }
if ($this->container['enum_string_required'] === null) { if ($this->container['enum_string_required'] === null) {
return false; return false;
} }
$allowedValues = $this->getEnumStringRequiredAllowableValues(); $allowedValues = $this->getEnumStringRequiredAllowableValues();
if (!is_null($this->container['enum_string_required']) && !in_array($this->container['enum_string_required'], $allowedValues)) { if (!is_null($this->container['enum_string_required']) && !in_array($this->container['enum_string_required'], $allowedValues, true)) {
return false; return false;
} }
$allowedValues = $this->getEnumIntegerAllowableValues(); $allowedValues = $this->getEnumIntegerAllowableValues();
if (!is_null($this->container['enum_integer']) && !in_array($this->container['enum_integer'], $allowedValues)) { if (!is_null($this->container['enum_integer']) && !in_array($this->container['enum_integer'], $allowedValues, true)) {
return false; return false;
} }
$allowedValues = $this->getEnumNumberAllowableValues(); $allowedValues = $this->getEnumNumberAllowableValues();
if (!is_null($this->container['enum_number']) && !in_array($this->container['enum_number'], $allowedValues)) { if (!is_null($this->container['enum_number']) && !in_array($this->container['enum_number'], $allowedValues, true)) {
return false; return false;
} }
return true; return true;
@ -367,7 +367,7 @@ class EnumTest implements ModelInterface, ArrayAccess
public function setEnumString($enum_string) public function setEnumString($enum_string)
{ {
$allowedValues = $this->getEnumStringAllowableValues(); $allowedValues = $this->getEnumStringAllowableValues();
if (!is_null($enum_string) && !in_array($enum_string, $allowedValues)) { if (!is_null($enum_string) && !in_array($enum_string, $allowedValues, true)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
sprintf( sprintf(
"Invalid value for 'enum_string', must be one of '%s'", "Invalid value for 'enum_string', must be one of '%s'",
@ -400,7 +400,7 @@ class EnumTest implements ModelInterface, ArrayAccess
public function setEnumStringRequired($enum_string_required) public function setEnumStringRequired($enum_string_required)
{ {
$allowedValues = $this->getEnumStringRequiredAllowableValues(); $allowedValues = $this->getEnumStringRequiredAllowableValues();
if (!in_array($enum_string_required, $allowedValues)) { if (!in_array($enum_string_required, $allowedValues, true)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
sprintf( sprintf(
"Invalid value for 'enum_string_required', must be one of '%s'", "Invalid value for 'enum_string_required', must be one of '%s'",
@ -433,7 +433,7 @@ class EnumTest implements ModelInterface, ArrayAccess
public function setEnumInteger($enum_integer) public function setEnumInteger($enum_integer)
{ {
$allowedValues = $this->getEnumIntegerAllowableValues(); $allowedValues = $this->getEnumIntegerAllowableValues();
if (!is_null($enum_integer) && !in_array($enum_integer, $allowedValues)) { if (!is_null($enum_integer) && !in_array($enum_integer, $allowedValues, true)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
sprintf( sprintf(
"Invalid value for 'enum_integer', must be one of '%s'", "Invalid value for 'enum_integer', must be one of '%s'",
@ -466,7 +466,7 @@ class EnumTest implements ModelInterface, ArrayAccess
public function setEnumNumber($enum_number) public function setEnumNumber($enum_number)
{ {
$allowedValues = $this->getEnumNumberAllowableValues(); $allowedValues = $this->getEnumNumberAllowableValues();
if (!is_null($enum_number) && !in_array($enum_number, $allowedValues)) { if (!is_null($enum_number) && !in_array($enum_number, $allowedValues, true)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
sprintf( sprintf(
"Invalid value for 'enum_number', must be one of '%s'", "Invalid value for 'enum_number', must be one of '%s'",

View File

@ -237,7 +237,7 @@ class Order implements ModelInterface, ArrayAccess
$invalidProperties = []; $invalidProperties = [];
$allowedValues = $this->getStatusAllowableValues(); $allowedValues = $this->getStatusAllowableValues();
if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues)) { if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues, true)) {
$invalidProperties[] = sprintf( $invalidProperties[] = sprintf(
"invalid value for 'status', must be one of '%s'", "invalid value for 'status', must be one of '%s'",
implode("', '", $allowedValues) implode("', '", $allowedValues)
@ -257,7 +257,7 @@ class Order implements ModelInterface, ArrayAccess
{ {
$allowedValues = $this->getStatusAllowableValues(); $allowedValues = $this->getStatusAllowableValues();
if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues)) { if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues, true)) {
return false; return false;
} }
return true; return true;
@ -380,7 +380,7 @@ class Order implements ModelInterface, ArrayAccess
public function setStatus($status) public function setStatus($status)
{ {
$allowedValues = $this->getStatusAllowableValues(); $allowedValues = $this->getStatusAllowableValues();
if (!is_null($status) && !in_array($status, $allowedValues)) { if (!is_null($status) && !in_array($status, $allowedValues, true)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
sprintf( sprintf(
"Invalid value for 'status', must be one of '%s'", "Invalid value for 'status', must be one of '%s'",

View File

@ -243,7 +243,7 @@ class Pet implements ModelInterface, ArrayAccess
$invalidProperties[] = "'photo_urls' can't be null"; $invalidProperties[] = "'photo_urls' can't be null";
} }
$allowedValues = $this->getStatusAllowableValues(); $allowedValues = $this->getStatusAllowableValues();
if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues)) { if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues, true)) {
$invalidProperties[] = sprintf( $invalidProperties[] = sprintf(
"invalid value for 'status', must be one of '%s'", "invalid value for 'status', must be one of '%s'",
implode("', '", $allowedValues) implode("', '", $allowedValues)
@ -269,7 +269,7 @@ class Pet implements ModelInterface, ArrayAccess
return false; return false;
} }
$allowedValues = $this->getStatusAllowableValues(); $allowedValues = $this->getStatusAllowableValues();
if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues)) { if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues, true)) {
return false; return false;
} }
return true; return true;
@ -416,7 +416,7 @@ class Pet implements ModelInterface, ArrayAccess
public function setStatus($status) public function setStatus($status)
{ {
$allowedValues = $this->getStatusAllowableValues(); $allowedValues = $this->getStatusAllowableValues();
if (!is_null($status) && !in_array($status, $allowedValues)) { if (!is_null($status) && !in_array($status, $allowedValues, true)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
sprintf( sprintf(
"Invalid value for 'status', must be one of '%s'", "Invalid value for 'status', must be one of '%s'",

View File

@ -14,8 +14,8 @@
<filter> <filter>
<whitelist processUncoveredFilesFromWhitelist="true"> <whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./lib\Api</directory> <directory suffix=".php">./lib/Api</directory>
<directory suffix=".php">./lib\Model</directory> <directory suffix=".php">./lib/Model</directory>
</whitelist> </whitelist>
</filter> </filter>
</phpunit> </phpunit>

View File

@ -83,6 +83,13 @@ class EnumTestTest extends \PHPUnit_Framework_TestCase
{ {
} }
/**
* Test attribute "enum_string_required"
*/
public function testPropertyEnumStringRequired()
{
}
/** /**
* Test attribute "enum_integer" * Test attribute "enum_integer"
*/ */

View File

@ -16,6 +16,30 @@ class EnumTestTest extends \PHPUnit_Framework_TestCase
$this->assertSame(EnumTest::ENUM_NUMBER_MINUS_1_DOT_2, -1.2); $this->assertSame(EnumTest::ENUM_NUMBER_MINUS_1_DOT_2, -1.2);
} }
public function testStrictValidation()
{
$enum = new EnumTest([
'enum_string' => 0,
]);
$this->assertFalse($enum->valid());
$expected = [
"invalid value for 'enum_string', must be one of 'UPPER', 'lower', ''",
"'enum_string_required' can't be null",
];
$this->assertSame($expected, $enum->listInvalidProperties());
}
/**
* @expectedException \InvalidArgumentException
*/
public function testThrowExceptionWhenInvalidAmbiguousValueHasPassed()
{
$enum = new EnumTest();
$enum->setEnumString(0);
}
public function testNonRequiredPropertyIsOptional() public function testNonRequiredPropertyIsOptional()
{ {
$enum = new EnumTest([ $enum = new EnumTest([