Skip to content

Commit

Permalink
[GO] Fix exception in constructExampleCode with oneOf primitive type (O…
Browse files Browse the repository at this point in the history
  • Loading branch information
okhowang committed May 13, 2022
1 parent 02f1626 commit d39a7c3
Showing 1 changed file with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,7 @@ public void processOpts() {

// add lambda for mustache templates to handle oneOf/anyOf naming
// e.g. []string => ArrayOfString
additionalProperties.put("lambda.type-to-name", new Mustache.Lambda() {
@Override
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
String content = fragment.execute();
content = content.trim().replace("[]", "array_of_");
content = content.trim().replace("[", "map_of_");
content = content.trim().replace("]", "");
writer.write(camelize(content));
}
});
additionalProperties.put("lambda.type-to-name", (Mustache.Lambda) (fragment, writer) -> writer.write(typeToName(fragment.execute())));

supportingFiles.add(new SupportingFile("openapi.mustache", "api", "openapi.yaml"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
Expand Down Expand Up @@ -658,8 +649,13 @@ private String constructExampleCode(CodegenModel codegenModel, HashMap<String, C
return goImportAlias + "." + model + "(" + example + ")";
} else if (codegenModel.oneOf != null && !codegenModel.oneOf.isEmpty()) {
String subModel = (String) codegenModel.oneOf.toArray()[0];
String oneOf = constructExampleCode(modelMaps.get(subModel), modelMaps, processedModelMap, depth+1).substring(1);
return goImportAlias + "." + model + "{" + subModel + ": " + oneOf + "}";
String oneOf;
if (modelMaps.get(subModel) == null) {
oneOf = "new(" + subModel + ")";// a primitive type
} else {
oneOf = constructExampleCode(modelMaps.get(subModel), modelMaps, processedModelMap, depth + 1).substring(1);
}
return goImportAlias + "." + model + "{" + typeToName(subModel) + ": " + oneOf + "}";
} else {
ArrayList<Integer> v = new ArrayList<>();
v.add(depth);
Expand All @@ -672,4 +668,11 @@ private String constructExampleCode(CodegenModel codegenModel, HashMap<String, C
}
return "*" + goImportAlias + ".New" + toModelName(model) + "(" + StringUtils.join(propertyExamples, ", ") + ")";
}

private String typeToName(String content) {
content = content.trim().replace("[]", "array_of_");
content = content.trim().replace("[", "map_of_");
content = content.trim().replace("]", "");
return camelize(content);
}
}

0 comments on commit d39a7c3

Please sign in to comment.