-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* fixed enum template; added enum handling to ObjectSerializer * regenerated templates * improved InvalidArgumentException message * regenerated sample client * added value check of OuterEnum during sanitization * regenerated samples
- Loading branch information
Showing
7 changed files
with
164 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions
12
modules/swagger-codegen/src/main/resources/php/model_enum.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
class {{classname}} { | ||
/** | ||
* Possible values of this enum | ||
*/ | ||
{{#allowableValues}}{{#enumVars}}const {{{name}}} = {{{value}}}; | ||
{{/enumVars}}{{/allowableValues}} | ||
|
||
{{#vars}}{{#isEnum}} | ||
/** | ||
* Gets allowable values of the enum | ||
* @return string[] | ||
*/ | ||
public function {{getter}}AllowableValues() | ||
public static function getAllowableEnumValues() | ||
{ | ||
return [ | ||
{{#allowableValues}}{{#enumVars}}self::{{datatypeWithEnum}}_{{{name}}},{{^-last}} | ||
{{#allowableValues}}{{#enumVars}}self::{{{name}}},{{^-last}} | ||
{{/-last}}{{/enumVars}}{{/allowableValues}} | ||
]; | ||
} | ||
{{/isEnum}}{{/vars}} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
samples/client/petstore/php/SwaggerClient-php/tests/OuterEnumTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace Swagger\Client; | ||
|
||
use Swagger\Client\Model\EnumTest; | ||
use Swagger\Client\Model\OuterEnum; | ||
|
||
class OuterEnumTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testDeserialize() | ||
{ | ||
$result = ObjectSerializer::deserialize( | ||
"placed", | ||
OuterEnum::class | ||
); | ||
|
||
$this->assertInternalType('string', $result); | ||
$this->assertEquals('placed', $result); | ||
} | ||
|
||
public function testDeserializeInvalidValue() | ||
{ | ||
$this->setExpectedException(\InvalidArgumentException::class, 'Invalid value for enum'); | ||
|
||
ObjectSerializer::deserialize( | ||
"lkjfalgkdfjg", | ||
OuterEnum::class | ||
); | ||
} | ||
|
||
public function testDeserializeNested() | ||
{ | ||
$json = '{ | ||
"enum_string": "UPPER", | ||
"enum_integer": -1, | ||
"enum_number": -1.2, | ||
"outerEnum": "approved" | ||
}'; | ||
|
||
/** * @var EnumTest $result */ | ||
$result = ObjectSerializer::deserialize( | ||
json_decode($json), | ||
EnumTest::class | ||
); | ||
|
||
$this->assertInstanceOf(EnumTest::class, $result); | ||
$this->assertEquals('approved', $result->getOuterEnum()); | ||
} | ||
|
||
public function testSanitize() | ||
{ | ||
$json = "placed"; | ||
|
||
$result = ObjectSerializer::sanitizeForSerialization( | ||
$json | ||
); | ||
|
||
$this->assertInternalType('string', $result); | ||
} | ||
|
||
public function testSanitizeNested() | ||
{ | ||
$input = new EnumTest([ | ||
'enum_string' => 'UPPER', | ||
'enum_integer' => -1, | ||
'enum_number' => -1.2, | ||
'outer_enum' => 'approved' | ||
]); | ||
|
||
$result = ObjectSerializer::sanitizeForSerialization( | ||
$input | ||
); | ||
|
||
$this->assertInternalType('object', $result); | ||
$this->assertInstanceOf(\stdClass::class, $result); | ||
|
||
$this->assertInternalType('string', $result->outerEnum); | ||
$this->assertEquals('approved', $result->outerEnum); | ||
} | ||
|
||
public function testSanitizeNestedInvalidValue() | ||
{ | ||
$this->setExpectedException(\InvalidArgumentException::class, 'Invalid value for enum'); | ||
|
||
$input = new EnumTest([ | ||
'enum_string' => 'UPPER', | ||
'enum_integer' => -1, | ||
'enum_number' => -1.2, | ||
'outer_enum' => 'invalid_value' | ||
]); | ||
|
||
ObjectSerializer::sanitizeForSerialization($input); | ||
} | ||
} |