diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java index af33ba1f36a7..40d986a66504 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java @@ -684,6 +684,28 @@ public void setParameterExampleValue(CodegenParameter p) { p.example = example; } + @Override + protected void updateEnumVarsWithExtensions(List> enumVars, Map vendorExtensions, String dataType) { + if (vendorExtensions != null) { + if (vendorExtensions.containsKey("x-enum-varnames")) { + List values = (List) vendorExtensions.get("x-enum-varnames"); + int size = Math.min(enumVars.size(), values.size()); + + for (int i = 0; i < size; i++) { + enumVars.get(i).put("name", toEnumVarName(values.get(i), dataType)); + } + } + + if (vendorExtensions.containsKey("x-enum-descriptions")) { + List values = (List) vendorExtensions.get("x-enum-descriptions"); + int size = Math.min(enumVars.size(), values.size()); + for (int i = 0; i < size; i++) { + enumVars.get(i).put("enumDescription", values.get(i)); + } + } + } + } + @Override public String toEnumValue(String value, String datatype) { if ("int".equals(datatype) || "float".equals(datatype)) { @@ -704,11 +726,11 @@ public String toEnumVarName(String name, String datatype) { return enumNameMapping.get(name); } - if (name.length() == 0) { + if (name.isEmpty()) { return "EMPTY"; } - if (name.trim().length() == 0) { + if (name.trim().isEmpty()) { return "SPACE_" + name.length(); } @@ -719,11 +741,12 @@ public String toEnumVarName(String name, String datatype) { // number if ("int".equals(datatype) || "float".equals(datatype)) { - String varName = name; - varName = varName.replaceAll("-", "MINUS_"); - varName = varName.replaceAll("\\+", "PLUS_"); - varName = varName.replaceAll("\\.", "_DOT_"); - return varName; + if (name.matches("\\d.*")) { // starts with number + name = "NUMBER_" + name; + } + name = name.replaceAll("-", "MINUS_"); + name = name.replaceAll("\\+", "PLUS_"); + name = name.replaceAll("\\.", "_DOT_"); } // string diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpLaravelServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpLaravelServerCodegen.java index 59e191e75d13..9befedd4a485 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpLaravelServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpLaravelServerCodegen.java @@ -374,22 +374,4 @@ public String toDefaultValue(Schema p) { public String toEnumDefaultValue(String value, String datatype) { return datatype + "::" + value; } - - @Override - public String toEnumVarName(String value, String datatype) { - if (value.length() == 0) { - return super.toEnumVarName(value, datatype); - } - - // number - if ("int".equals(datatype) || "float".equals(datatype)) { - String varName = "NUMBER_" + value; - varName = varName.replaceAll("-", "MINUS_"); - varName = varName.replaceAll("\\+", "PLUS_"); - varName = varName.replaceAll("\\.", "_DOT_"); - return varName; - } - - return super.toEnumVarName(value, datatype); - } } 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 bb8e093f7aad..d0a24379e609 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 @@ -2,8 +2,15 @@ enum {{classname}}: {{vendorExtensions.x-php-enum-type}} { {{#allowableValues}} {{#enumVars}} - case {{^isString}}NUMBER_{{/isString}}{{{name}}} = {{{value}}}; + {{#enumDescription}} + /** + * {{enumDescription}} + */ + {{/enumDescription}} + case {{{name}}} = {{{value}}}; +{{^-last}} +{{/-last}} {{/enumVars}} {{/allowableValues}} } 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 77001f2e6058..86d022506db8 100644 --- a/modules/openapi-generator/src/main/resources/php/model_enum.mustache +++ b/modules/openapi-generator/src/main/resources/php/model_enum.mustache @@ -5,7 +5,12 @@ class {{classname}} */ {{#allowableValues}} {{#enumVars}} - public const {{^isString}}NUMBER_{{/isString}}{{{name}}} = {{{value}}}; + {{#enumDescription}} + /** + * {{enumDescription}} + */ + {{/enumDescription}} + public const {{{name}}} = {{{value}}}; {{/enumVars}} {{/allowableValues}} @@ -18,7 +23,7 @@ class {{classname}} return [ {{#allowableValues}} {{#enumVars}} - self::{{^isString}}NUMBER_{{/isString}}{{{name}}}{{^-last}}, + self::{{{name}}}{{^-last}}, {{/-last}} {{/enumVars}} {{/allowableValues}} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/php/PhpModelTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/php/PhpModelTest.java index 39b3bb7870e8..d2bc34a33b57 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/php/PhpModelTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/php/PhpModelTest.java @@ -348,7 +348,7 @@ public void enumModelValueTest() { Assert.assertEquals(prope.allowableValues.get("values"), Arrays.asList(1, -1)); HashMap one = new HashMap(); - one.put("name", "1"); + one.put("name", "NUMBER_1"); one.put("value", "1"); one.put("isString", false); HashMap minusOne = new HashMap(); diff --git a/modules/openapi-generator/src/test/resources/3_0/php-nextgen/petstore-with-fake-endpoints-models-for-testing.yaml b/modules/openapi-generator/src/test/resources/3_0/php-nextgen/petstore-with-fake-endpoints-models-for-testing.yaml index 433bd56071e8..cebc271e1d5b 100644 --- a/modules/openapi-generator/src/test/resources/3_0/php-nextgen/petstore-with-fake-endpoints-models-for-testing.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/php-nextgen/petstore-with-fake-endpoints-models-for-testing.yaml @@ -2066,4 +2066,19 @@ components: ArrayRef: type: array items: - type: string \ No newline at end of file + type: string + EnumWithNameAndDescription: + type: integer + enum: + - 1 + - 2 + - 3 + - 4 + x-enum-varnames: + - ONE + - "2" + - " 3" + x-enum-descriptions: + - The word one + - The digit two + - The digit three prefixed by a space diff --git a/modules/openapi-generator/src/test/resources/3_0/php/petstore-with-fake-endpoints-models-for-testing.yaml b/modules/openapi-generator/src/test/resources/3_0/php/petstore-with-fake-endpoints-models-for-testing.yaml index 71e9f1525ed7..138e51a2f208 100644 --- a/modules/openapi-generator/src/test/resources/3_0/php/petstore-with-fake-endpoints-models-for-testing.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/php/petstore-with-fake-endpoints-models-for-testing.yaml @@ -2076,3 +2076,18 @@ components: type: string type_: type: string + EnumWithNameAndDescription: + type: integer + enum: + - 1 + - 2 + - 3 + - 4 + x-enum-varnames: + - ONE + - "2" + - " 3" + x-enum-descriptions: + - The word one + - The digit two + - The digit three prefixed by a space diff --git a/samples/client/echo_api/php-nextgen-streaming/src/Model/StringEnumRef.php b/samples/client/echo_api/php-nextgen-streaming/src/Model/StringEnumRef.php index 337fc1a3f8d0..8ab408d1baf3 100644 --- a/samples/client/echo_api/php-nextgen-streaming/src/Model/StringEnumRef.php +++ b/samples/client/echo_api/php-nextgen-streaming/src/Model/StringEnumRef.php @@ -42,7 +42,6 @@ enum StringEnumRef: string case FAILURE = 'failure'; case UNCLASSIFIED = 'unclassified'; - } diff --git a/samples/client/echo_api/php-nextgen/src/Model/StringEnumRef.php b/samples/client/echo_api/php-nextgen/src/Model/StringEnumRef.php index 337fc1a3f8d0..8ab408d1baf3 100644 --- a/samples/client/echo_api/php-nextgen/src/Model/StringEnumRef.php +++ b/samples/client/echo_api/php-nextgen/src/Model/StringEnumRef.php @@ -42,7 +42,6 @@ enum StringEnumRef: string case FAILURE = 'failure'; case UNCLASSIFIED = 'unclassified'; - } diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/.openapi-generator/FILES b/samples/client/petstore/php-nextgen/OpenAPIClient-php/.openapi-generator/FILES index ec822fdd8838..ebd39c9d9de9 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/.openapi-generator/FILES +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/.openapi-generator/FILES @@ -29,6 +29,7 @@ docs/Model/Dog.md docs/Model/EnumArrays.md docs/Model/EnumClass.md docs/Model/EnumTest.md +docs/Model/EnumWithNameAndDescription.md docs/Model/FakeBigDecimalMap200Response.md docs/Model/File.md docs/Model/FileSchemaTestClass.md @@ -91,6 +92,7 @@ src/Model/Dog.php src/Model/EnumArrays.php src/Model/EnumClass.php src/Model/EnumTest.php +src/Model/EnumWithNameAndDescription.php src/Model/FakeBigDecimalMap200Response.php src/Model/File.php src/Model/FileSchemaTestClass.php diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/README.md b/samples/client/petstore/php-nextgen/OpenAPIClient-php/README.md index a9cdc4b822c7..6b60abeee5f9 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/README.md +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/README.md @@ -138,6 +138,7 @@ Class | Method | HTTP request | Description - [EnumArrays](docs/Model/EnumArrays.md) - [EnumClass](docs/Model/EnumClass.md) - [EnumTest](docs/Model/EnumTest.md) +- [EnumWithNameAndDescription](docs/Model/EnumWithNameAndDescription.md) - [FakeBigDecimalMap200Response](docs/Model/FakeBigDecimalMap200Response.md) - [File](docs/Model/File.md) - [FileSchemaTestClass](docs/Model/FileSchemaTestClass.md) diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/docs/Model/EnumWithNameAndDescription.md b/samples/client/petstore/php-nextgen/OpenAPIClient-php/docs/Model/EnumWithNameAndDescription.md new file mode 100644 index 000000000000..ab03446144aa --- /dev/null +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/docs/Model/EnumWithNameAndDescription.md @@ -0,0 +1,8 @@ +# # EnumWithNameAndDescription + +## Properties + +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- + +[[Back to Model list]](../../README.md#models) [[Back to API list]](../../README.md#endpoints) [[Back to README]](../../README.md) 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 309739e44c5a..554e0f359425 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 @@ -41,7 +41,6 @@ enum EnumClass: string case EFG = '-efg'; case XYZ = '(xyz)'; - } diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/EnumTest.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/EnumTest.php index e27329183f4b..56d1a53b62b0 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/EnumTest.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/EnumTest.php @@ -275,9 +275,9 @@ public function getModelName(): string public const ENUM_STRING_REQUIRED_UPPER = 'UPPER'; public const ENUM_STRING_REQUIRED_LOWER = 'lower'; public const ENUM_STRING_REQUIRED_EMPTY = ''; - public const ENUM_INTEGER_1 = 1; + public const ENUM_INTEGER_NUMBER_1 = 1; public const ENUM_INTEGER_MINUS_1 = -1; - public const ENUM_NUMBER_1_DOT_1 = 1.1; + public const ENUM_NUMBER_NUMBER_1_DOT_1 = 1.1; public const ENUM_NUMBER_MINUS_1_DOT_2 = -1.2; /** @@ -316,7 +316,7 @@ public function getEnumStringRequiredAllowableValues() public function getEnumIntegerAllowableValues() { return [ - self::ENUM_INTEGER_1, + self::ENUM_INTEGER_NUMBER_1, self::ENUM_INTEGER_MINUS_1, ]; } @@ -329,7 +329,7 @@ public function getEnumIntegerAllowableValues() public function getEnumNumberAllowableValues() { return [ - self::ENUM_NUMBER_1_DOT_1, + self::ENUM_NUMBER_NUMBER_1_DOT_1, self::ENUM_NUMBER_MINUS_1_DOT_2, ]; } diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/EnumWithNameAndDescription.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/EnumWithNameAndDescription.php new file mode 100644 index 000000000000..1bfaa9daf841 --- /dev/null +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/EnumWithNameAndDescription.php @@ -0,0 +1,57 @@ +