mirror of
				https://github.com/OpenAPITools/openapi-generator.git
				synced 2025-11-04 10:43:44 +00:00 
			
		
		
		
	[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:
		
							parent
							
								
									6d88d073ca
								
							
						
					
					
						commit
						d58835e571
					
				@ -194,7 +194,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
 | 
			
		||||
        {{#isEnum}}
 | 
			
		||||
        {{^isContainer}}
 | 
			
		||||
        $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(
 | 
			
		||||
                "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 (!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;
 | 
			
		||||
        }
 | 
			
		||||
        {{/isContainer}}
 | 
			
		||||
@ -344,7 +344,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
 | 
			
		||||
        {{#isEnum}}
 | 
			
		||||
        $allowedValues = $this->{{getter}}AllowableValues();
 | 
			
		||||
        {{^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(
 | 
			
		||||
                sprintf(
 | 
			
		||||
                    "Invalid value for '{{name}}', must be one of '%s'",
 | 
			
		||||
 | 
			
		||||
@ -226,7 +226,7 @@ class EnumArrays implements ModelInterface, ArrayAccess
 | 
			
		||||
        $invalidProperties = [];
 | 
			
		||||
 | 
			
		||||
        $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(
 | 
			
		||||
                "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 (!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 true;
 | 
			
		||||
@ -273,7 +273,7 @@ class EnumArrays implements ModelInterface, ArrayAccess
 | 
			
		||||
    public function setJustSymbol($just_symbol)
 | 
			
		||||
    {
 | 
			
		||||
        $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(
 | 
			
		||||
                sprintf(
 | 
			
		||||
                    "Invalid value for 'just_symbol', must be one of '%s'",
 | 
			
		||||
 | 
			
		||||
@ -278,7 +278,7 @@ class EnumTest implements ModelInterface, ArrayAccess
 | 
			
		||||
        $invalidProperties = [];
 | 
			
		||||
 | 
			
		||||
        $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(
 | 
			
		||||
                "invalid value for 'enum_string', must be one of '%s'",
 | 
			
		||||
                implode("', '", $allowedValues)
 | 
			
		||||
@ -289,7 +289,7 @@ class EnumTest implements ModelInterface, ArrayAccess
 | 
			
		||||
            $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)) {
 | 
			
		||||
        if (!is_null($this->container['enum_string_required']) && !in_array($this->container['enum_string_required'], $allowedValues, true)) {
 | 
			
		||||
            $invalidProperties[] = sprintf(
 | 
			
		||||
                "invalid value for 'enum_string_required', must be one of '%s'",
 | 
			
		||||
                implode("', '", $allowedValues)
 | 
			
		||||
@ -297,7 +297,7 @@ class EnumTest implements ModelInterface, ArrayAccess
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $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(
 | 
			
		||||
                "invalid value for 'enum_integer', must be one of '%s'",
 | 
			
		||||
                implode("', '", $allowedValues)
 | 
			
		||||
@ -305,7 +305,7 @@ class EnumTest implements ModelInterface, ArrayAccess
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $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(
 | 
			
		||||
                "invalid value for 'enum_number', must be one of '%s'",
 | 
			
		||||
                implode("', '", $allowedValues)
 | 
			
		||||
@ -325,22 +325,22 @@ class EnumTest implements ModelInterface, ArrayAccess
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        $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;
 | 
			
		||||
        }
 | 
			
		||||
        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)) {
 | 
			
		||||
        if (!is_null($this->container['enum_string_required']) && !in_array($this->container['enum_string_required'], $allowedValues, true)) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        $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;
 | 
			
		||||
        }
 | 
			
		||||
        $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 true;
 | 
			
		||||
@ -367,7 +367,7 @@ class EnumTest implements ModelInterface, ArrayAccess
 | 
			
		||||
    public function setEnumString($enum_string)
 | 
			
		||||
    {
 | 
			
		||||
        $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(
 | 
			
		||||
                sprintf(
 | 
			
		||||
                    "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)
 | 
			
		||||
    {
 | 
			
		||||
        $allowedValues = $this->getEnumStringRequiredAllowableValues();
 | 
			
		||||
        if (!in_array($enum_string_required, $allowedValues)) {
 | 
			
		||||
        if (!in_array($enum_string_required, $allowedValues, true)) {
 | 
			
		||||
            throw new \InvalidArgumentException(
 | 
			
		||||
                sprintf(
 | 
			
		||||
                    "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)
 | 
			
		||||
    {
 | 
			
		||||
        $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(
 | 
			
		||||
                sprintf(
 | 
			
		||||
                    "Invalid value for 'enum_integer', must be one of '%s'",
 | 
			
		||||
@ -466,7 +466,7 @@ class EnumTest implements ModelInterface, ArrayAccess
 | 
			
		||||
    public function setEnumNumber($enum_number)
 | 
			
		||||
    {
 | 
			
		||||
        $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(
 | 
			
		||||
                sprintf(
 | 
			
		||||
                    "Invalid value for 'enum_number', must be one of '%s'",
 | 
			
		||||
 | 
			
		||||
@ -237,7 +237,7 @@ class Order implements ModelInterface, ArrayAccess
 | 
			
		||||
        $invalidProperties = [];
 | 
			
		||||
 | 
			
		||||
        $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(
 | 
			
		||||
                "invalid value for 'status', must be one of '%s'",
 | 
			
		||||
                implode("', '", $allowedValues)
 | 
			
		||||
@ -257,7 +257,7 @@ class Order implements ModelInterface, ArrayAccess
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        $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 true;
 | 
			
		||||
@ -380,7 +380,7 @@ class Order implements ModelInterface, ArrayAccess
 | 
			
		||||
    public function setStatus($status)
 | 
			
		||||
    {
 | 
			
		||||
        $allowedValues = $this->getStatusAllowableValues();
 | 
			
		||||
        if (!is_null($status) && !in_array($status, $allowedValues)) {
 | 
			
		||||
        if (!is_null($status) && !in_array($status, $allowedValues, true)) {
 | 
			
		||||
            throw new \InvalidArgumentException(
 | 
			
		||||
                sprintf(
 | 
			
		||||
                    "Invalid value for 'status', must be one of '%s'",
 | 
			
		||||
 | 
			
		||||
@ -243,7 +243,7 @@ class Pet implements ModelInterface, ArrayAccess
 | 
			
		||||
            $invalidProperties[] = "'photo_urls' can't be null";
 | 
			
		||||
        }
 | 
			
		||||
        $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(
 | 
			
		||||
                "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 (!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 true;
 | 
			
		||||
@ -416,7 +416,7 @@ class Pet implements ModelInterface, ArrayAccess
 | 
			
		||||
    public function setStatus($status)
 | 
			
		||||
    {
 | 
			
		||||
        $allowedValues = $this->getStatusAllowableValues();
 | 
			
		||||
        if (!is_null($status) && !in_array($status, $allowedValues)) {
 | 
			
		||||
        if (!is_null($status) && !in_array($status, $allowedValues, true)) {
 | 
			
		||||
            throw new \InvalidArgumentException(
 | 
			
		||||
                sprintf(
 | 
			
		||||
                    "Invalid value for 'status', must be one of '%s'",
 | 
			
		||||
 | 
			
		||||
@ -14,8 +14,8 @@
 | 
			
		||||
 | 
			
		||||
    <filter>
 | 
			
		||||
        <whitelist processUncoveredFilesFromWhitelist="true">
 | 
			
		||||
            <directory suffix=".php">./lib\Api</directory>
 | 
			
		||||
            <directory suffix=".php">./lib\Model</directory>
 | 
			
		||||
            <directory suffix=".php">./lib/Api</directory>
 | 
			
		||||
            <directory suffix=".php">./lib/Model</directory>
 | 
			
		||||
        </whitelist>
 | 
			
		||||
    </filter>
 | 
			
		||||
</phpunit>
 | 
			
		||||
 | 
			
		||||
@ -83,6 +83,13 @@ class EnumTestTest extends \PHPUnit_Framework_TestCase
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Test attribute "enum_string_required"
 | 
			
		||||
     */
 | 
			
		||||
    public function testPropertyEnumStringRequired()
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Test attribute "enum_integer"
 | 
			
		||||
     */
 | 
			
		||||
 | 
			
		||||
@ -16,6 +16,30 @@ class EnumTestTest extends \PHPUnit_Framework_TestCase
 | 
			
		||||
        $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()
 | 
			
		||||
    {
 | 
			
		||||
        $enum = new EnumTest([
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user