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

This commit is contained in:
okhowang
2022-05-15 17:25:23 +08:00
committed by GitHub
parent 29323f38a3
commit 245f23339b

View File

@@ -255,16 +255,7 @@ public class GoClientCodegen extends AbstractGoCodegen {
// 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"));
@@ -658,8 +649,13 @@ public class GoClientCodegen extends AbstractGoCodegen {
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);
@@ -672,4 +668,11 @@ public class GoClientCodegen extends AbstractGoCodegen {
}
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);
}
}