From 3b076d9a1e7652b980a64144a4c918bd4215ae75 Mon Sep 17 00:00:00 2001 From: Hiromi Hishida Date: Mon, 14 Feb 2022 09:32:57 +0900 Subject: [PATCH 1/6] fixed model_enum.mustache for real usage --- .../main/resources/php/model_enum.mustache | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/php/model_enum.mustache b/modules/openapi-generator/src/main/resources/php/model_enum.mustache index 88036f76ffc9..71dc0df4edb6 100644 --- a/modules/openapi-generator/src/main/resources/php/model_enum.mustache +++ b/modules/openapi-generator/src/main/resources/php/model_enum.mustache @@ -6,6 +6,19 @@ class {{classname}} {{#allowableValues}} {{#enumVars}} const {{^isString}}NUMBER_{{/isString}}{{{name}}} = {{{value}}}; + + /** + * @var mixed + */ + private $value; + + public function __construct($value) + { + if (!in_array($value, self::getAllowableEnumValues(), true) { + throw new \LogicException(sprintf('%s is not a allowed value for enum', $value)); + } + $this->value = $value; + } {{/enumVars}} {{/allowableValues}} @@ -25,4 +38,13 @@ class {{classname}} ]; } + + /** + * @return mixed + */ + #[ReturnTypeWillChange] + public function getValue() + { + return $this->value; + } } From e4409e33ff47ab193b0e33e495c5a00a0c668e73 Mon Sep 17 00:00:00 2001 From: Hiromi Hishida Date: Mon, 14 Feb 2022 09:33:49 +0900 Subject: [PATCH 2/6] fix --- .../src/main/resources/php/model_enum.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/php/model_enum.mustache b/modules/openapi-generator/src/main/resources/php/model_enum.mustache index 71dc0df4edb6..6fef243bc649 100644 --- a/modules/openapi-generator/src/main/resources/php/model_enum.mustache +++ b/modules/openapi-generator/src/main/resources/php/model_enum.mustache @@ -14,7 +14,7 @@ class {{classname}} public function __construct($value) { - if (!in_array($value, self::getAllowableEnumValues(), true) { + if (!in_array($value, self::getAllowableEnumValues(), true)) { throw new \LogicException(sprintf('%s is not a allowed value for enum', $value)); } $this->value = $value; From a8906eff185ab0e3837e4c4c4ff2bd9c7bb22ca9 Mon Sep 17 00:00:00 2001 From: 77web Date: Wed, 16 Feb 2022 11:43:26 +0900 Subject: [PATCH 3/6] added EnumInterface --- .../main/resources/php/EnumInterface.mustache | 36 +++++++++++++++++++ .../main/resources/php/model_enum.mustache | 4 +-- 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/php/EnumInterface.mustache diff --git a/modules/openapi-generator/src/main/resources/php/EnumInterface.mustache b/modules/openapi-generator/src/main/resources/php/EnumInterface.mustache new file mode 100644 index 000000000000..b731ee93fc89 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/php/EnumInterface.mustache @@ -0,0 +1,36 @@ +partial_header}} +/** + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + +namespace {{modelPackage}}; + +/** + * Interface abstracting enum access. + * + * @package {{modelPackage}} + * @author OpenAPI Generator team + */ +interface EnumInterface +{ + public static function getAllowableEnumValues(): array; + + /** + * @return mixed + */ + public function getValue(); +} diff --git a/modules/openapi-generator/src/main/resources/php/model_enum.mustache b/modules/openapi-generator/src/main/resources/php/model_enum.mustache index 6fef243bc649..2c3651978c6a 100644 --- a/modules/openapi-generator/src/main/resources/php/model_enum.mustache +++ b/modules/openapi-generator/src/main/resources/php/model_enum.mustache @@ -1,4 +1,4 @@ -class {{classname}} +class {{classname}} implements EnumInterface { /** * Possible values of this enum @@ -42,7 +42,7 @@ class {{classname}} /** * @return mixed */ - #[ReturnTypeWillChange] + #[\ReturnTypeWillChange] public function getValue() { return $this->value; From 5b14d13c5b5e97462e2e9e2e1512698059979eaf Mon Sep 17 00:00:00 2001 From: 77web Date: Wed, 16 Feb 2022 11:44:53 +0900 Subject: [PATCH 4/6] use EnumInterface in ObjectSerializer --- .../src/main/resources/php/ObjectSerializer.mustache | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache index 7d48d3557c34..51040326df4a 100644 --- a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache +++ b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache @@ -19,6 +19,7 @@ namespace {{invokerPackage}}; +use {{modelPackage}}\EnumInterface; use {{modelPackage}}\ModelInterface; /** @@ -78,9 +79,9 @@ class ObjectSerializer $getter = $data::getters()[$property]; $value = $data->$getter(); if ($value !== null && !in_array($openAPIType, [{{&primitives}}], true)) { - $callable = [$openAPIType, 'getAllowableEnumValues']; - if (is_callable($callable)) { - /** array $callable */ + if ($value instanceof EnumInterface) { + $value = $value->getValue(); + $callable = [$openAPIType, 'getAllowableEnumValues']; $allowedEnumTypes = $callable(); if (!in_array($value, $allowedEnumTypes, true)) { $imploded = implode("', '", $allowedEnumTypes); From 2020c8e82258f85cedf1ecf9ba2b3e77ac809f4e Mon Sep 17 00:00:00 2001 From: 77web Date: Wed, 16 Feb 2022 11:58:21 +0900 Subject: [PATCH 5/6] fixed property and constructor position in model_enum.mustache --- .../src/main/resources/php/model_enum.mustache | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/php/model_enum.mustache b/modules/openapi-generator/src/main/resources/php/model_enum.mustache index 2c3651978c6a..82d148d68577 100644 --- a/modules/openapi-generator/src/main/resources/php/model_enum.mustache +++ b/modules/openapi-generator/src/main/resources/php/model_enum.mustache @@ -6,12 +6,15 @@ class {{classname}} implements EnumInterface {{#allowableValues}} {{#enumVars}} const {{^isString}}NUMBER_{{/isString}}{{{name}}} = {{{value}}}; - + {{/enumVars}} + {{/allowableValues}} + + /** * @var mixed */ private $value; - + public function __construct($value) { if (!in_array($value, self::getAllowableEnumValues(), true)) { @@ -20,8 +23,6 @@ class {{classname}} implements EnumInterface $this->value = $value; } - {{/enumVars}} - {{/allowableValues}} /** * Gets allowable values of the enum * @return string[] From 1119d57474456ea34a6f8d123cadae3c95075f39 Mon Sep 17 00:00:00 2001 From: 77web Date: Wed, 16 Feb 2022 12:01:58 +0900 Subject: [PATCH 6/6] updated samples --- .../OpenAPIClient-php/lib/Model/EnumClass.php | 27 ++++++++++++++++--- .../OpenAPIClient-php/lib/Model/OuterEnum.php | 27 ++++++++++++++++--- .../lib/Model/OuterEnumDefaultValue.php | 27 ++++++++++++++++--- .../lib/Model/OuterEnumInteger.php | 27 ++++++++++++++++--- .../Model/OuterEnumIntegerDefaultValue.php | 27 ++++++++++++++++--- .../lib/ObjectSerializer.php | 7 ++--- 6 files changed, 124 insertions(+), 18 deletions(-) diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumClass.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumClass.php index 8fe1b2880723..a33e48c4c010 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumClass.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumClass.php @@ -37,17 +37,29 @@ * @author OpenAPI Generator team * @link https://openapi-generator.tech */ -class EnumClass +class EnumClass implements EnumInterface { /** * Possible values of this enum */ const ABC = '_abc'; - const EFG = '-efg'; - const XYZ = '(xyz)'; + + /** + * @var mixed + */ + private $value; + + public function __construct($value) + { + if (!in_array($value, self::getAllowableEnumValues(), true)) { + throw new \LogicException(sprintf('%s is not a allowed value for enum', $value)); + } + $this->value = $value; + } + /** * Gets allowable values of the enum * @return string[] @@ -60,6 +72,15 @@ public static function getAllowableEnumValues() self::XYZ ]; } + + /** + * @return mixed + */ + #[\ReturnTypeWillChange] + public function getValue() + { + return $this->value; + } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnum.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnum.php index b1b834319a8d..0c0b2a5994ab 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnum.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnum.php @@ -37,17 +37,29 @@ * @author OpenAPI Generator team * @link https://openapi-generator.tech */ -class OuterEnum +class OuterEnum implements EnumInterface { /** * Possible values of this enum */ const PLACED = 'placed'; - const APPROVED = 'approved'; - const DELIVERED = 'delivered'; + + /** + * @var mixed + */ + private $value; + + public function __construct($value) + { + if (!in_array($value, self::getAllowableEnumValues(), true)) { + throw new \LogicException(sprintf('%s is not a allowed value for enum', $value)); + } + $this->value = $value; + } + /** * Gets allowable values of the enum * @return string[] @@ -60,6 +72,15 @@ public static function getAllowableEnumValues() self::DELIVERED ]; } + + /** + * @return mixed + */ + #[\ReturnTypeWillChange] + public function getValue() + { + return $this->value; + } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumDefaultValue.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumDefaultValue.php index 5d88f63550c6..97e6af3e1bce 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumDefaultValue.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumDefaultValue.php @@ -37,17 +37,29 @@ * @author OpenAPI Generator team * @link https://openapi-generator.tech */ -class OuterEnumDefaultValue +class OuterEnumDefaultValue implements EnumInterface { /** * Possible values of this enum */ const PLACED = 'placed'; - const APPROVED = 'approved'; - const DELIVERED = 'delivered'; + + /** + * @var mixed + */ + private $value; + + public function __construct($value) + { + if (!in_array($value, self::getAllowableEnumValues(), true)) { + throw new \LogicException(sprintf('%s is not a allowed value for enum', $value)); + } + $this->value = $value; + } + /** * Gets allowable values of the enum * @return string[] @@ -60,6 +72,15 @@ public static function getAllowableEnumValues() self::DELIVERED ]; } + + /** + * @return mixed + */ + #[\ReturnTypeWillChange] + public function getValue() + { + return $this->value; + } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumInteger.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumInteger.php index 5bb6ee6f26e0..8903613e680a 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumInteger.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumInteger.php @@ -37,17 +37,29 @@ * @author OpenAPI Generator team * @link https://openapi-generator.tech */ -class OuterEnumInteger +class OuterEnumInteger implements EnumInterface { /** * Possible values of this enum */ const NUMBER_0 = 0; - const NUMBER_1 = 1; - const NUMBER_2 = 2; + + /** + * @var mixed + */ + private $value; + + public function __construct($value) + { + if (!in_array($value, self::getAllowableEnumValues(), true)) { + throw new \LogicException(sprintf('%s is not a allowed value for enum', $value)); + } + $this->value = $value; + } + /** * Gets allowable values of the enum * @return string[] @@ -60,6 +72,15 @@ public static function getAllowableEnumValues() self::NUMBER_2 ]; } + + /** + * @return mixed + */ + #[\ReturnTypeWillChange] + public function getValue() + { + return $this->value; + } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumIntegerDefaultValue.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumIntegerDefaultValue.php index f7ae2d6b29db..ad82dd385ca4 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumIntegerDefaultValue.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumIntegerDefaultValue.php @@ -37,17 +37,29 @@ * @author OpenAPI Generator team * @link https://openapi-generator.tech */ -class OuterEnumIntegerDefaultValue +class OuterEnumIntegerDefaultValue implements EnumInterface { /** * Possible values of this enum */ const NUMBER_0 = 0; - const NUMBER_1 = 1; - const NUMBER_2 = 2; + + /** + * @var mixed + */ + private $value; + + public function __construct($value) + { + if (!in_array($value, self::getAllowableEnumValues(), true)) { + throw new \LogicException(sprintf('%s is not a allowed value for enum', $value)); + } + $this->value = $value; + } + /** * Gets allowable values of the enum * @return string[] @@ -60,6 +72,15 @@ public static function getAllowableEnumValues() self::NUMBER_2 ]; } + + /** + * @return mixed + */ + #[\ReturnTypeWillChange] + public function getValue() + { + return $this->value; + } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php b/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php index 840e4d934149..d21ded32c011 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php @@ -28,6 +28,7 @@ namespace OpenAPI\Client; +use OpenAPI\Client\Model\EnumInterface; use OpenAPI\Client\Model\ModelInterface; /** @@ -87,9 +88,9 @@ public static function sanitizeForSerialization($data, $type = null, $format = n $getter = $data::getters()[$property]; $value = $data->$getter(); if ($value !== null && !in_array($openAPIType, ['\DateTime', '\SplFileObject', 'array', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) { - $callable = [$openAPIType, 'getAllowableEnumValues']; - if (is_callable($callable)) { - /** array $callable */ + if ($value instanceof EnumInterface) { + $value = $value->getValue(); + $callable = [$openAPIType, 'getAllowableEnumValues']; $allowedEnumTypes = $callable(); if (!in_array($value, $allowedEnumTypes, true)) { $imploded = implode("', '", $allowedEnumTypes);