forked from loafle/openapi-generator-original
[python] fixes enum naming bug (#13985)
* Adds fix * Adds needed java imports
This commit is contained in:
@@ -1273,7 +1273,10 @@ public class PythonClientCodegen extends AbstractPythonCodegen {
|
||||
// Replace " " with _
|
||||
String usedValue = value.replaceAll("\\s+", "_");
|
||||
// strip first character if it is invalid
|
||||
int lengthBeforeFirstCharStrip = usedValue.length();
|
||||
Character firstChar = usedValue.charAt(0);
|
||||
usedValue = usedValue.replaceAll("^[^_a-zA-Z]", "");
|
||||
boolean firstCharStripped = usedValue.length() == lengthBeforeFirstCharStrip - 1;
|
||||
// Replace / with _ for path enums
|
||||
usedValue = usedValue.replaceAll("/", "_");
|
||||
// Replace . with _ for tag enums
|
||||
@@ -1296,6 +1299,14 @@ public class PythonClientCodegen extends AbstractPythonCodegen {
|
||||
// remove trailing _
|
||||
usedValue = usedValue.replaceAll("[_]$", "");
|
||||
}
|
||||
// check first character to see if it is valid
|
||||
// if not then add a valid prefix
|
||||
boolean validFirstChar = Pattern.matches("^[_a-zA-Z]", usedValue.substring(0,1));
|
||||
if (!validFirstChar && firstCharStripped) {
|
||||
String charName = Character.getName(firstChar.hashCode());
|
||||
usedValue = charNameToVarName(charName) + "_" + usedValue;
|
||||
}
|
||||
|
||||
return usedValue;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,8 @@ import io.swagger.v3.oas.models.media.*;
|
||||
import org.openapitools.codegen.*;
|
||||
import org.openapitools.codegen.config.CodegenConfigurator;
|
||||
import org.openapitools.codegen.languages.PythonClientCodegen;
|
||||
import org.openapitools.codegen.model.ModelMap;
|
||||
import org.openapitools.codegen.model.ModelsMap;
|
||||
import org.openapitools.codegen.utils.ModelUtils;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
@@ -32,6 +34,7 @@ import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -197,4 +200,28 @@ public class PythonClientTest {
|
||||
Assert.assertEquals(cm.vendorExtensions.get("x-regex"), expectedRegexPattern);
|
||||
Assert.assertEquals(cm.vendorExtensions.get("x-modifiers"), Arrays.asList("DOTALL", "IGNORECASE", "MULTILINE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumNames() {
|
||||
OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/13942_schema_enum_names.yaml");
|
||||
PythonClientCodegen codegen = new PythonClientCodegen();
|
||||
codegen.setOpenAPI(openAPI);
|
||||
|
||||
String modelName = "StringEnum";
|
||||
Schema schema = openAPI.getComponents().getSchemas().get(modelName);
|
||||
|
||||
CodegenModel cm = codegen.fromModel(modelName, schema);
|
||||
|
||||
ModelMap modelMap = new ModelMap();
|
||||
modelMap.setModel(cm);
|
||||
|
||||
ModelsMap modelsMap = new ModelsMap();
|
||||
modelsMap.setModels(Collections.singletonList(modelMap));
|
||||
codegen.postProcessModels(modelsMap);
|
||||
|
||||
ArrayList<Map<String, Object>> enumVars = (ArrayList<Map<String, Object>>) cm.getAllowableValues().get("enumVars");
|
||||
Assert.assertEquals(enumVars.size(), 2);
|
||||
Assert.assertEquals(enumVars.get(0).get("name"), "DIGIT_THREE_67B9C");
|
||||
Assert.assertEquals(enumVars.get(1).get("name"), "FFA5A4");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
openapi: 3.0.3
|
||||
info:
|
||||
title: Test
|
||||
version: 1.0.0-SNAPSHOT
|
||||
paths: {}
|
||||
components:
|
||||
schemas:
|
||||
StringEnum:
|
||||
type: string
|
||||
enum:
|
||||
- "#367B9C"
|
||||
- "#FFA5A4"
|
||||
Reference in New Issue
Block a user