Skip to content

Commit a0429a0

Browse files
committed
Fix php invalid enum const names
#1475
1 parent 37cdc8e commit a0429a0

6 files changed

Lines changed: 30 additions & 23 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
public abstract class AbstractPhpCodegen extends DefaultCodegen implements CodegenConfig {
3838

3939
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractPhpCodegen.class);
40+
private static final String NUMERIC_ENUM_PREFIX = "N";
4041

4142
public static final String VARIABLE_NAMING_CONVENTION = "variableNamingConvention";
4243
public static final String PACKAGE_NAME = "packageName";
@@ -629,6 +630,10 @@ public String toEnumVarName(String name, String datatype) {
629630
varName = varName.replaceAll("-", "MINUS_");
630631
varName = varName.replaceAll("\\+", "PLUS_");
631632
varName = varName.replaceAll("\\.", "_DOT_");
633+
634+
if (varName.matches("\\d.*")) { // if after replacements the value is still numeric
635+
return NUMERIC_ENUM_PREFIX + varName;
636+
}
632637
return varName;
633638
}
634639

@@ -637,8 +642,10 @@ public String toEnumVarName(String name, String datatype) {
637642
enumName = enumName.replaceFirst("^_", "");
638643
enumName = enumName.replaceFirst("_$", "");
639644

640-
if (isReservedWord(enumName) || enumName.matches("\\d.*")) { // reserved word or starts with number
645+
if (isReservedWord(enumName)) { // reserved word
641646
return escapeReservedWord(enumName);
647+
} else if (enumName.matches("\\d.*")) { // or starts with number
648+
return NUMERIC_ENUM_PREFIX + enumName;
642649
} else {
643650
return enumName;
644651
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/php/PhpModelTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public void enumArrayModelTest() {
336336
}
337337

338338
@Test(description = "test enum model for values (numeric, string, etc)")
339-
public void enumMdoelValueTest() {
339+
public void enumModelValueTest() {
340340
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml");
341341
final DefaultCodegen codegen = new PhpClientCodegen();
342342
codegen.setOpenAPI(openAPI);
@@ -353,7 +353,7 @@ public void enumMdoelValueTest() {
353353
Assert.assertEquals(prope.allowableValues.get("values"), Arrays.asList(1, -1));
354354

355355
HashMap<String, Object> one = new HashMap<String, Object>();
356-
one.put("name", "1");
356+
one.put("name", "N1");
357357
one.put("value", "1");
358358
one.put("isString", false);
359359
HashMap<String, Object> minusOne = new HashMap<String, Object>();

samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ public function getModelName()
184184
const ENUM_STRING_REQUIRED_UPPER = 'UPPER';
185185
const ENUM_STRING_REQUIRED_LOWER = 'lower';
186186
const ENUM_STRING_REQUIRED_EMPTY = '';
187-
const ENUM_INTEGER_1 = 1;
187+
const ENUM_INTEGER_N1 = 1;
188188
const ENUM_INTEGER_MINUS_1 = -1;
189-
const ENUM_NUMBER_1_DOT_1 = 1.1;
189+
const ENUM_NUMBER_N1_DOT_1 = 1.1;
190190
const ENUM_NUMBER_MINUS_1_DOT_2 = -1.2;
191191

192192

@@ -227,7 +227,7 @@ public function getEnumStringRequiredAllowableValues()
227227
public function getEnumIntegerAllowableValues()
228228
{
229229
return [
230-
self::ENUM_INTEGER_1,
230+
self::ENUM_INTEGER_N1,
231231
self::ENUM_INTEGER_MINUS_1,
232232
];
233233
}
@@ -240,7 +240,7 @@ public function getEnumIntegerAllowableValues()
240240
public function getEnumNumberAllowableValues()
241241
{
242242
return [
243-
self::ENUM_NUMBER_1_DOT_1,
243+
self::ENUM_NUMBER_N1_DOT_1,
244244
self::ENUM_NUMBER_MINUS_1_DOT_2,
245245
];
246246
}

samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Model/EnumTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ public function getModelName()
199199
const ENUM_STRING_REQUIRED_UPPER = 'UPPER';
200200
const ENUM_STRING_REQUIRED_LOWER = 'lower';
201201
const ENUM_STRING_REQUIRED_EMPTY = '';
202-
const ENUM_INTEGER_1 = 1;
202+
const ENUM_INTEGER_N1 = 1;
203203
const ENUM_INTEGER_MINUS_1 = -1;
204-
const ENUM_NUMBER_1_DOT_1 = 1.1;
204+
const ENUM_NUMBER_N1_DOT_1 = 1.1;
205205
const ENUM_NUMBER_MINUS_1_DOT_2 = -1.2;
206206

207207

@@ -242,7 +242,7 @@ public function getEnumStringRequiredAllowableValues()
242242
public function getEnumIntegerAllowableValues()
243243
{
244244
return [
245-
self::ENUM_INTEGER_1,
245+
self::ENUM_INTEGER_N1,
246246
self::ENUM_INTEGER_MINUS_1,
247247
];
248248
}
@@ -255,7 +255,7 @@ public function getEnumIntegerAllowableValues()
255255
public function getEnumNumberAllowableValues()
256256
{
257257
return [
258-
self::ENUM_NUMBER_1_DOT_1,
258+
self::ENUM_NUMBER_N1_DOT_1,
259259
self::ENUM_NUMBER_MINUS_1_DOT_2,
260260
];
261261
}

samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumInteger.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ class OuterEnumInteger
4343
/**
4444
* Possible values of this enum
4545
*/
46-
const 0 = 0;
47-
const 1 = 1;
48-
const 2 = 2;
46+
const N0 = 0;
47+
const N1 = 1;
48+
const N2 = 2;
4949

5050
/**
5151
* Gets allowable values of the enum
@@ -54,9 +54,9 @@ class OuterEnumInteger
5454
public static function getAllowableEnumValues()
5555
{
5656
return [
57-
self::0,
58-
self::1,
59-
self::2,
57+
self::N0,
58+
self::N1,
59+
self::N2,
6060
];
6161
}
6262
}

samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumIntegerDefaultValue.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ class OuterEnumIntegerDefaultValue
4343
/**
4444
* Possible values of this enum
4545
*/
46-
const 0 = 0;
47-
const 1 = 1;
48-
const 2 = 2;
46+
const N0 = 0;
47+
const N1 = 1;
48+
const N2 = 2;
4949

5050
/**
5151
* Gets allowable values of the enum
@@ -54,9 +54,9 @@ class OuterEnumIntegerDefaultValue
5454
public static function getAllowableEnumValues()
5555
{
5656
return [
57-
self::0,
58-
self::1,
59-
self::2,
57+
self::N0,
58+
self::N1,
59+
self::N2,
6060
];
6161
}
6262
}

0 commit comments

Comments
 (0)