From 8b15d4820ed1ee206e2aab99e8f55dc27bed3e82 Mon Sep 17 00:00:00 2001 From: Julian Vennen Date: Mon, 11 Sep 2023 14:06:50 +0200 Subject: [PATCH] Use backed enums in php-nextgen (#16556) --- .../languages/PhpNextgenClientCodegen.java | 32 +++++++++++++++++++ .../php-nextgen/ObjectSerializer.mustache | 17 +++++----- .../resources/php-nextgen/model_enum.mustache | 23 ++----------- .../OpenAPIClient-php/src/Model/EnumClass.php | 23 +++---------- .../OpenAPIClient-php/src/Model/OuterEnum.php | 23 +++---------- .../src/Model/OuterEnumDefaultValue.php | 23 +++---------- .../src/Model/OuterEnumInteger.php | 23 +++---------- .../Model/OuterEnumIntegerDefaultValue.php | 23 +++---------- .../src/Model/SingleRefType.php | 20 ++---------- .../src/ObjectSerializer.php | 17 +++++----- 10 files changed, 73 insertions(+), 151 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java index 2f1f9dcb1c7..8aab539f4a8 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java @@ -20,11 +20,14 @@ package org.openapitools.codegen.languages; import org.apache.commons.lang3.StringUtils; import org.openapitools.codegen.CliOption; import org.openapitools.codegen.CodegenConstants; +import org.openapitools.codegen.CodegenModel; import org.openapitools.codegen.CodegenType; import org.openapitools.codegen.SupportingFile; import org.openapitools.codegen.meta.GeneratorMetadata; import org.openapitools.codegen.meta.Stability; import org.openapitools.codegen.meta.features.*; +import org.openapitools.codegen.model.ModelMap; +import org.openapitools.codegen.model.ModelsMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -33,6 +36,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.EnumSet; import java.util.List; +import java.util.Map; public class PhpNextgenClientCodegen extends AbstractPhpCodegen { @SuppressWarnings("hiding") @@ -122,4 +126,32 @@ public class PhpNextgenClientCodegen extends AbstractPhpCodegen { supportingFiles.add(new SupportingFile(".phplint.mustache", "", ".phplint.yml")); supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh")); } + + @Override + public Map postProcessAllModels(Map objs) { + final Map processed = super.postProcessAllModels(objs); + + for (Map.Entry entry : processed.entrySet()) { + entry.setValue(postProcessModelsMap(entry.getValue())); + } + + return processed; + } + + private ModelsMap postProcessModelsMap(ModelsMap objs) { + for (ModelMap m : objs.getModels()) { + CodegenModel model = m.getModel(); + + if (model.isEnum) { + for (Map enumVars : (List>) model.getAllowableValues().get("enumVars")) { + if ((Boolean) enumVars.get("isString")) { + model.vendorExtensions.putIfAbsent("x-php-enum-type", "string"); + } else { + model.vendorExtensions.putIfAbsent("x-php-enum-type", "int"); + } + } + } + } + return objs; + } } diff --git a/modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache b/modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache index 9c46077110b..7f2be65eb67 100644 --- a/modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache +++ b/modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache @@ -79,12 +79,10 @@ 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 */ - $allowedEnumTypes = $callable(); - if (!in_array($value, $allowedEnumTypes, true)) { - $imploded = implode("', '", $allowedEnumTypes); + if ($openAPIType instanceof \BackedEnum) { + $data = $openAPIType::tryFrom($data); + if ($data === null) { + $imploded = implode("', '", array_map(fn($case) => $case->value, $openAPIType::cases())); throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'"); } } @@ -488,9 +486,10 @@ class ObjectSerializer } - if (method_exists($class, 'getAllowableEnumValues')) { - if (!in_array($data, $class::getAllowableEnumValues(), true)) { - $imploded = implode("', '", $class::getAllowableEnumValues()); + if ($class instanceof \BackedEnum) { + $data = $class::tryFrom($data); + if ($data === null) { + $imploded = implode("', '", array_map(fn($case) => $case->value, $class::cases())); throw new \InvalidArgumentException("Invalid value for enum '$class', must be one of: '$imploded'"); } return $data; diff --git a/modules/openapi-generator/src/main/resources/php-nextgen/model_enum.mustache b/modules/openapi-generator/src/main/resources/php-nextgen/model_enum.mustache index 77001f2e605..bb8e093f7aa 100644 --- a/modules/openapi-generator/src/main/resources/php-nextgen/model_enum.mustache +++ b/modules/openapi-generator/src/main/resources/php-nextgen/model_enum.mustache @@ -1,28 +1,9 @@ -class {{classname}} +enum {{classname}}: {{vendorExtensions.x-php-enum-type}} { - /** - * Possible values of this enum - */ {{#allowableValues}} {{#enumVars}} - public const {{^isString}}NUMBER_{{/isString}}{{{name}}} = {{{value}}}; + case {{^isString}}NUMBER_{{/isString}}{{{name}}} = {{{value}}}; {{/enumVars}} {{/allowableValues}} - /** - * Gets allowable values of the enum - * @return string[] - */ - public static function getAllowableEnumValues() - { - return [ - {{#allowableValues}} - {{#enumVars}} - self::{{^isString}}NUMBER_{{/isString}}{{{name}}}{{^-last}}, - {{/-last}} - {{/enumVars}} - {{/allowableValues}} - - ]; - } } diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/EnumClass.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/EnumClass.php index 3fa28382757..328f4f330d2 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/EnumClass.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/EnumClass.php @@ -37,29 +37,14 @@ use \OpenAPI\Client\ObjectSerializer; * @author OpenAPI Generator team * @link https://openapi-generator.tech */ -class EnumClass +enum EnumClass: string { - /** - * Possible values of this enum - */ - public const ABC = '_abc'; + case ABC = '_abc'; - public const EFG = '-efg'; + case EFG = '-efg'; - public const XYZ = '(xyz)'; + case XYZ = '(xyz)'; - /** - * Gets allowable values of the enum - * @return string[] - */ - public static function getAllowableEnumValues() - { - return [ - self::ABC, - self::EFG, - self::XYZ - ]; - } } diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/OuterEnum.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/OuterEnum.php index ff541c22882..ed5aa962f71 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/OuterEnum.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/OuterEnum.php @@ -37,29 +37,14 @@ use \OpenAPI\Client\ObjectSerializer; * @author OpenAPI Generator team * @link https://openapi-generator.tech */ -class OuterEnum +enum OuterEnum: string { - /** - * Possible values of this enum - */ - public const PLACED = 'placed'; + case PLACED = 'placed'; - public const APPROVED = 'approved'; + case APPROVED = 'approved'; - public const DELIVERED = 'delivered'; + case DELIVERED = 'delivered'; - /** - * Gets allowable values of the enum - * @return string[] - */ - public static function getAllowableEnumValues() - { - return [ - self::PLACED, - self::APPROVED, - self::DELIVERED - ]; - } } diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/OuterEnumDefaultValue.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/OuterEnumDefaultValue.php index 09ce6f0f634..334410e862e 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/OuterEnumDefaultValue.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/OuterEnumDefaultValue.php @@ -37,29 +37,14 @@ use \OpenAPI\Client\ObjectSerializer; * @author OpenAPI Generator team * @link https://openapi-generator.tech */ -class OuterEnumDefaultValue +enum OuterEnumDefaultValue: string { - /** - * Possible values of this enum - */ - public const PLACED = 'placed'; + case PLACED = 'placed'; - public const APPROVED = 'approved'; + case APPROVED = 'approved'; - public const DELIVERED = 'delivered'; + case DELIVERED = 'delivered'; - /** - * Gets allowable values of the enum - * @return string[] - */ - public static function getAllowableEnumValues() - { - return [ - self::PLACED, - self::APPROVED, - self::DELIVERED - ]; - } } diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/OuterEnumInteger.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/OuterEnumInteger.php index ba2f6c4df13..83013b84235 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/OuterEnumInteger.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/OuterEnumInteger.php @@ -37,29 +37,14 @@ use \OpenAPI\Client\ObjectSerializer; * @author OpenAPI Generator team * @link https://openapi-generator.tech */ -class OuterEnumInteger +enum OuterEnumInteger: int { - /** - * Possible values of this enum - */ - public const NUMBER_0 = 0; + case NUMBER_0 = 0; - public const NUMBER_1 = 1; + case NUMBER_1 = 1; - public const NUMBER_2 = 2; + case NUMBER_2 = 2; - /** - * Gets allowable values of the enum - * @return string[] - */ - public static function getAllowableEnumValues() - { - return [ - self::NUMBER_0, - self::NUMBER_1, - self::NUMBER_2 - ]; - } } diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/OuterEnumIntegerDefaultValue.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/OuterEnumIntegerDefaultValue.php index 8e870afe04b..d841179ebe7 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/OuterEnumIntegerDefaultValue.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/OuterEnumIntegerDefaultValue.php @@ -37,29 +37,14 @@ use \OpenAPI\Client\ObjectSerializer; * @author OpenAPI Generator team * @link https://openapi-generator.tech */ -class OuterEnumIntegerDefaultValue +enum OuterEnumIntegerDefaultValue: int { - /** - * Possible values of this enum - */ - public const NUMBER_0 = 0; + case NUMBER_0 = 0; - public const NUMBER_1 = 1; + case NUMBER_1 = 1; - public const NUMBER_2 = 2; + case NUMBER_2 = 2; - /** - * Gets allowable values of the enum - * @return string[] - */ - public static function getAllowableEnumValues() - { - return [ - self::NUMBER_0, - self::NUMBER_1, - self::NUMBER_2 - ]; - } } diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/SingleRefType.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/SingleRefType.php index 59eb6481f84..877fa704fae 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/SingleRefType.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/SingleRefType.php @@ -37,26 +37,12 @@ use \OpenAPI\Client\ObjectSerializer; * @author OpenAPI Generator team * @link https://openapi-generator.tech */ -class SingleRefType +enum SingleRefType: string { - /** - * Possible values of this enum - */ - public const ADMIN = 'admin'; + case ADMIN = 'admin'; - public const USER = 'user'; + case USER = 'user'; - /** - * Gets allowable values of the enum - * @return string[] - */ - public static function getAllowableEnumValues() - { - return [ - self::ADMIN, - self::USER - ]; - } } diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ObjectSerializer.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ObjectSerializer.php index 16097a025a9..336ef331d93 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ObjectSerializer.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ObjectSerializer.php @@ -88,12 +88,10 @@ class ObjectSerializer $getter = $data::getters()[$property]; $value = $data->$getter(); if ($value !== null && !in_array($openAPIType, ['\DateTime', '\SplFileObject', 'array', 'bool', 'boolean', 'byte', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) { - $callable = [$openAPIType, 'getAllowableEnumValues']; - if (is_callable($callable)) { - /** array $callable */ - $allowedEnumTypes = $callable(); - if (!in_array($value, $allowedEnumTypes, true)) { - $imploded = implode("', '", $allowedEnumTypes); + if ($openAPIType instanceof \BackedEnum) { + $data = $openAPIType::tryFrom($data); + if ($data === null) { + $imploded = implode("', '", array_map(fn($case) => $case->value, $openAPIType::cases())); throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'"); } } @@ -497,9 +495,10 @@ class ObjectSerializer } - if (method_exists($class, 'getAllowableEnumValues')) { - if (!in_array($data, $class::getAllowableEnumValues(), true)) { - $imploded = implode("', '", $class::getAllowableEnumValues()); + if ($class instanceof \BackedEnum) { + $data = $class::tryFrom($data); + if ($data === null) { + $imploded = implode("', '", array_map(fn($case) => $case->value, $class::cases())); throw new \InvalidArgumentException("Invalid value for enum '$class', must be one of: '$imploded'"); } return $data;