Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GO] Fix exception in constructExampleCode with oneOf primitive type #12362

Merged
merged 1 commit into from
May 15, 2022
Merged
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 @@ -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);
}
}