Skip to content

Commit

Permalink
Generate enums prefixed with model names to prevent collisions (#1)
Browse files Browse the repository at this point in the history
- Cherrypicked from OpenAPITools#3162
  • Loading branch information
arvindth authored and DABH committed Aug 30, 2019
1 parent bbc2ba4 commit 6aebdfe
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,37 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
return postProcessModelsEnum(objs);
}

@Override
public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {
objs = super.postProcessModelsEnum(objs);

List<Object> models = (List<Object>) objs.get("models");
for (Object _mo : models) {
Map<String, Object> mo = (Map<String, Object>) _mo;
CodegenModel cm = (CodegenModel) mo.get("model");

// For enum var names prepend with this model's name to help prevent namespace collision
if (Boolean.TRUE.equals(cm.isEnum) && cm.allowableValues != null) {
String prefix = toEnumVarName(cm.name, "string") + "_";
cm.allowableValues = prefixAllowableValues(cm.allowableValues, prefix);
}
}
return objs;
}

public Map<String, Object> prefixAllowableValues(Map<String, Object> allowableValues, String prefix) {
if (allowableValues.get("enumVars") != null) {
List<Map<String, Object>> enumVars = (List<Map<String, Object>>) allowableValues.get("enumVars");
for (Map<String, Object> enumVar : enumVars) {
String enumName = (String) enumVar.get("name");
if (enumName != null) {
enumVar.put("name", prefix + enumName);
}
}
}
return allowableValues;
}

@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
generateYAMLSpecFile(objs);
Expand Down

0 comments on commit 6aebdfe

Please sign in to comment.