[GO] Default time fails generation
Created by: eak24
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
Have you tested with the latest master to confirm the issue still exists? -
Have you searched for related issues/PRs? -
What's the actual output vs expected output?
Description
@knazarenko-ptc and I found that a spec generated with the following bit causes a failure in the generator. The error pops up because this cast to string fails: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java#L353 . Should this work?
OpenAPI declaration file content or url
{
"name": "timeQuery",
"in": "query",
"schema": {
"type": "string",
"format": "date-time",
"default": "2000-01-01T00:00:00Z"
}
}
Suggest a fix
The fix we have working locally is to call the toString() method on the default object:
diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java
index ed5418ab4a3..f22c18fb13b 100644
--- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java
+++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java
@@ -349,8 +349,12 @@ public class GoClientCodegen extends AbstractGoCodegen {
public String toDefaultValue(Schema p) {
p = ModelUtils.getReferencedSchema(this.openAPI, p);
if (ModelUtils.isStringSchema(p)) {
- if (p.getDefault() != null) {
- return "\"" + escapeText((String) p.getDefault()) + "\"";
+ Object defaultObj = p.getDefault();
+ if (defaultObj != null) {
+ if (defaultObj instanceof java.lang.String) {
+ return "\"" + escapeText((String) defaultObj) + "\"";
+ }
+ return "\"" + escapeText(defaultObj.toString()) + "\"";
}
return null;
}