Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add "x-enum-varnames" extension to control the naming of the enum par…
…ameter name #893
  • Loading branch information
osjupiter committed Aug 28, 2018
commit cfd1f4cf9148040c3bf460bc30086bbaaa10ea16
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {
enumVar.put("isString", isDataTypeString(cm.dataType));
enumVars.add(enumVar);
}
// if "x-enum-varnames" defined, update varnames
updateEnumVarsWithExtensions(enumVars, cm.getVendorExtensions());
cm.allowableValues.put("enumVars", enumVars);
}

Expand Down Expand Up @@ -4003,6 +4005,8 @@ public void updateCodegenPropertyEnum(CodegenProperty var) {
enumVar.put("isString", isDataTypeString(dataType));
enumVars.add(enumVar);
}
// if "x-enum-varnames" defined, update varnames
updateEnumVarsWithExtensions(enumVars, var.getVendorExtensions());
allowableValues.put("enumVars", enumVars);

// handle default value for enum, e.g. available => StatusEnum.AVAILABLE
Expand All @@ -4020,6 +4024,17 @@ public void updateCodegenPropertyEnum(CodegenProperty var) {
}
}

private void updateEnumVarsWithExtensions(List<Map<String, Object>> enumVars, Map<String, Object> vendorExtensions) {
if (vendorExtensions != null && vendorExtensions.containsKey("x-enum-varnames")) {
List<String> alias = (List<String>) vendorExtensions.get("x-enum-varnames");
if (alias.size() == enumVars.size()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maye you could just log a warning if the sizes do not match.

In my opinion it is OK to still change the first two defined names in a case like this:

      EnumProp:
        type: string
        enum:
          - a
          - b
          - c
        x-enum-varnames:
          - FOO
          - BAR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.
Then I'll make changes to allow to set different number of elements from target enum.

for (int i = 0; i < alias.size(); i++) {
enumVars.get(i).put("name", alias.get(i));
}
}
}
}

/**
* If the pattern misses the delimiter, add "/" to the beginning and end
* Otherwise, return the original pattern
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,41 @@ public void updateCodegenPropertyEnum() {
Assert.assertEquals(testedEnumVar.getOrDefault("isString", ""), false);
}

@Test
public void updateCodegenPropertyEnumWithExtention() {
final DefaultCodegen codegen = new DefaultCodegen();
CodegenProperty enumProperty = codegenPropertyWithXEnumVarName();

codegen.updateCodegenPropertyEnum(enumProperty);

List<Map<String, Object>> enumVars = (List<Map<String, Object>>) enumProperty.getAllowableValues().get("enumVars");
Assert.assertNotNull(enumVars);
Assert.assertNotNull(enumVars.get(0));
Assert.assertEquals(enumVars.get(0).getOrDefault("name", ""), "DOGVAR");
Assert.assertEquals(enumVars.get(0).getOrDefault("value", ""), "\"dog\"");
Assert.assertNotNull(enumVars.get(1));
Assert.assertEquals(enumVars.get(1).getOrDefault("name", ""), "CATVAR");
Assert.assertEquals(enumVars.get(1).getOrDefault("value", ""), "\"cat\"");
}

@Test
public void postProcessModelsEnumWithExtention() {
final DefaultCodegen codegen = new DefaultCodegen();
Map<String, Object> objs = codegenModelWithXEnumVarName();
CodegenModel cm = (CodegenModel) ((Map<String, Object>) ((List<Object>) objs.get("models")).get(0)).get("model");

codegen.postProcessModelsEnum(objs);

List<Map<String, Object>> enumVars = (List<Map<String, Object>>) cm.getAllowableValues().get("enumVars");
Assert.assertNotNull(enumVars);
Assert.assertNotNull(enumVars.get(0));
Assert.assertEquals(enumVars.get(0).getOrDefault("name", ""), "DOGVAR");
Assert.assertEquals(enumVars.get(0).getOrDefault("value", ""), "\"dog\"");
Assert.assertNotNull(enumVars.get(1));
Assert.assertEquals(enumVars.get(1).getOrDefault("name", ""), "CATVAR");
Assert.assertEquals(enumVars.get(1).getOrDefault("value", ""), "\"cat\"");
}

@Test
public void testExample1() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/examples.yaml", null, new ParseOptions()).getOpenAPI();
Expand Down Expand Up @@ -478,4 +513,31 @@ private CodegenProperty codegenPropertyWithArrayOfIntegerValues() {
array.dataType = "Array";
return array;
}

private CodegenProperty codegenPropertyWithXEnumVarName() {
final CodegenProperty var = new CodegenProperty();
final HashMap<String, Object> allowableValues = new HashMap<>();
allowableValues.put("values", Arrays.asList("dog", "cat"));
var.setAllowableValues(allowableValues);
var.dataType = "String";
final List<String> aliases = Arrays.asList("DOGVAR", "CATVAR");
Map<String, Object> extentions = Collections.singletonMap("x-enum-varnames", aliases);
var.setVendorExtensions(extentions);
return var;
}

private Map<String, Object> codegenModelWithXEnumVarName() {
final CodegenModel cm = new CodegenModel();
cm.isEnum = true;
final HashMap<String, Object> allowableValues = new HashMap<>();
allowableValues.put("values", Arrays.asList("dog", "cat"));
cm.setAllowableValues(allowableValues);
cm.dataType = "String";
final List<String> aliases = Arrays.asList("DOGVAR", "CATVAR");
Map<String, Object> extentions = Collections.singletonMap("x-enum-varnames", aliases);
cm.setVendorExtensions(extentions);
cm.setVars(Collections.emptyList());
Map<String, Object> objs = Collections.singletonMap("models", Collections.singletonList(Collections.singletonMap("model", cm)));
return objs;
}
}