[core] fix referenced enum case (#2175)

This commit is contained in:
Jérémie Bresson 2019-02-19 15:42:32 +01:00 committed by William Cheng
parent 1dadd45ffe
commit fda867ebfe
2 changed files with 21 additions and 5 deletions

View File

@ -3980,8 +3980,8 @@ public class DefaultCodegen implements CodegenConfig {
Map<String, Object> allowableValues = var.allowableValues;
// handle array
if (var.items != null) {
allowableValues = var.items.allowableValues;
if (var.mostInnerItems != null) {
allowableValues = var.mostInnerItems.allowableValues;
}
if (allowableValues == null) {
@ -3993,6 +3993,13 @@ public class DefaultCodegen implements CodegenConfig {
return;
}
String varDataType = var.mostInnerItems != null ? var.mostInnerItems.dataType : var.dataType;
Optional<Schema> referencedSchema = ModelUtils.getSchemas(openAPI).entrySet().stream()
.filter(entry -> Objects.equals(varDataType, toModelName(entry.getKey())))
.map(Map.Entry::getValue)
.findFirst();
String dataType = (referencedSchema.isPresent()) ? getTypeDeclaration(referencedSchema.get()) : varDataType;
// put "enumVars" map into `allowableValues", including `name` and `value`
List<Map<String, Object>> enumVars = new ArrayList<>();
String commonPrefix = findCommonPrefixOfVars(values);
@ -4009,7 +4016,6 @@ public class DefaultCodegen implements CodegenConfig {
}
}
final String dataType = var.mostInnerItems != null ? var.mostInnerItems.dataType : var.dataType;
enumVar.put("name", toEnumVarName(enumName, dataType));
enumVar.put("value", toEnumValue(value.toString(), dataType));
enumVar.put("isString", isDataTypeString(dataType));
@ -4017,14 +4023,23 @@ public class DefaultCodegen implements CodegenConfig {
}
// if "x-enum-varnames" or "x-enum-descriptions" defined, update varnames
Map<String, Object> extensions = var.mostInnerItems != null ? var.mostInnerItems.getVendorExtensions() : var.getVendorExtensions();
if(referencedSchema.isPresent()) {
extensions = referencedSchema.get().getExtensions();
}
updateEnumVarsWithExtensions(enumVars, extensions);
allowableValues.put("enumVars", enumVars);
// handle default value for enum, e.g. available => StatusEnum.AVAILABLE
if (var.defaultValue != null) {
String enumName = null;
final String enumDefaultValue;
if("string".equalsIgnoreCase(dataType)) {
enumDefaultValue = toEnumValue(var.defaultValue, dataType);
} else {
enumDefaultValue = var.defaultValue;
}
for (Map<String, Object> enumVar : enumVars) {
if (toEnumValue(var.defaultValue, var.dataType).equals(enumVar.get("value"))) {
if (enumDefaultValue.equals(enumVar.get("value"))) {
enumName = (String) enumVar.get("name");
break;
}

View File

@ -569,7 +569,8 @@ public class DefaultCodegenTest {
allowableValues.put("values", Collections.singletonList(1));
items.setAllowableValues(allowableValues);
items.dataType = "Integer";
array.setItems(items);
array.items = items;
array.mostInnerItems = items;
array.dataType = "Array";
return array;
}