Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Fixes enum naming bug #72

Merged
merged 5 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,10 @@ public String toEnumVarName(String value, String datatype) {
// 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
Expand All @@ -1296,6 +1299,14 @@ public String toEnumVarName(String value, String datatype) {
// 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
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;
Expand All @@ -32,6 +34,7 @@
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;
Expand Down Expand Up @@ -197,4 +200,28 @@ public void testRegexWithMultipleFlagsWorks() {
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/70_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");
}
}
Original file line number Diff line number Diff line change
@@ -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"