Skip to content

Commit

Permalink
[PHP] Remove NUMBER_ prefix from enum vars if a name is provided, sho…
Browse files Browse the repository at this point in the history
…w enum descriptions (#19555)

* Remove NUMBER_ prefix from enum vars if a name is provided, show enum descriptions

* Update php model tests
  • Loading branch information
JulianVennen authored Sep 16, 2024
1 parent bbafeae commit a5384d4
Show file tree
Hide file tree
Showing 33 changed files with 574 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,28 @@ public void setParameterExampleValue(CodegenParameter p) {
p.example = example;
}

@Override
protected void updateEnumVarsWithExtensions(List<Map<String, Object>> enumVars, Map<String, Object> vendorExtensions, String dataType) {
if (vendorExtensions != null) {
if (vendorExtensions.containsKey("x-enum-varnames")) {
List<String> values = (List<String>) 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<String> values = (List<String>) 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)) {
Expand All @@ -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();
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ class {{classname}}
*/
{{#allowableValues}}
{{#enumVars}}
public const {{^isString}}NUMBER_{{/isString}}{{{name}}} = {{{value}}};
{{#enumDescription}}
/**
* {{enumDescription}}
*/
{{/enumDescription}}
public const {{{name}}} = {{{value}}};

{{/enumVars}}
{{/allowableValues}}
Expand All @@ -18,7 +23,7 @@ class {{classname}}
return [
{{#allowableValues}}
{{#enumVars}}
self::{{^isString}}NUMBER_{{/isString}}{{{name}}}{{^-last}},
self::{{{name}}}{{^-last}},
{{/-last}}
{{/enumVars}}
{{/allowableValues}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public void enumModelValueTest() {
Assert.assertEquals(prope.allowableValues.get("values"), Arrays.asList(1, -1));

HashMap<String, Object> one = new HashMap<String, Object>();
one.put("name", "1");
one.put("name", "NUMBER_1");
one.put("value", "1");
one.put("isString", false);
HashMap<String, Object> minusOne = new HashMap<String, Object>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2066,4 +2066,19 @@ components:
ArrayRef:
type: array
items:
type: string
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ enum StringEnumRef: string
case FAILURE = 'failure';

case UNCLASSIFIED = 'unclassified';

}


Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ enum StringEnumRef: string
case FAILURE = 'failure';

case UNCLASSIFIED = 'unclassified';

}


Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ enum EnumClass: string
case EFG = '-efg';

case XYZ = '(xyz)';

}


Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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,
];
}
Expand All @@ -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,
];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* EnumWithNameAndDescription
*
* PHP version 8.1
*
* @package OpenAPI\Client
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/

/**
* OpenAPI Petstore
*
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* The version of the OpenAPI document: 1.0.0
* @generated Generated by: https://openapi-generator.tech
* Generator version: 7.9.0-SNAPSHOT
*/

/**
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

namespace OpenAPI\Client\Model;

/**
* EnumWithNameAndDescription Class Doc Comment
*
* @package OpenAPI\Client
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/
enum EnumWithNameAndDescription: int
{
/**
* The word one
*/
case ONE = 1;

/**
* The digit two
*/
case NUMBER_2 = 2;

/**
* The digit three prefixed by a space
*/
case _3 = 3;

case NUMBER_4 = 4;
}


Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ enum OuterEnum: string
case APPROVED = 'approved';

case SHIPPED = 'delivered';

}


Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ enum OuterEnumDefaultValue: string
case APPROVED = 'approved';

case SHIPPED = 'delivered';

}


Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ enum OuterEnumInteger: int
case NUMBER_1 = 1;

case NUMBER_2 = 2;

}


Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ enum OuterEnumIntegerDefaultValue: int
case NUMBER_1 = 1;

case NUMBER_2 = 2;

}


Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ enum SingleRefType: string
case ADMIN = 'admin';

case USER = 'user';

}


Loading

0 comments on commit a5384d4

Please sign in to comment.