Skip to content

Commit 97af5d1

Browse files
authored
[go] fix default value for array type (#22584)
* Better handling array schema for `toDefaultValue` Signed-off-by: titaneric <[email protected]> * add string array and enum array to existing test case Signed-off-by: titaneric <[email protected]> --------- Signed-off-by: titaneric <[email protected]>
1 parent 873e272 commit 97af5d1

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,28 @@ public String toDefaultValue(Schema p) {
446446
Object defaultValues = p.getDefault();
447447
if (defaultValues instanceof ArrayNode) {
448448
for (var value : (ArrayNode) defaultValues) {
449-
joinedDefaultValues.add(value.toString());
449+
if (value.isNull()) {
450+
joinedDefaultValues.add("nil");
451+
} else if (value.isTextual()) {
452+
joinedDefaultValues.add("\"" + escapeText(value.asText()) + "\"");
453+
} else {
454+
joinedDefaultValues.add(value.toString());
455+
}
456+
}
457+
return "{" + joinedDefaultValues + "}";
458+
} else if (defaultValues instanceof List<?>) {
459+
for (var value : (List<?>) defaultValues) {
460+
if (value == null) {
461+
joinedDefaultValues.add("nil");
462+
} else if (value instanceof String) {
463+
joinedDefaultValues.add("\"" + escapeText((String) value) + "\"");
464+
} else {
465+
joinedDefaultValues.add(value.toString());
466+
}
450467
}
451468
return "{" + joinedDefaultValues + "}";
452469
}
470+
return null;
453471
}
454472

455473
return super.toDefaultValue(p);

modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,11 @@ public void testArrayDefaultValue() throws IOException {
454454
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
455455
files.forEach(File::deleteOnExit);
456456
Path apiPath = Paths.get(output + "/api_default.go");
457-
String defaultArrayString = "var defaultValue []interface{} = []interface{}{\"test1\", \"test2\", 1}";
457+
String defaultStringArrayString = "var defaultValue []string = []string{\"test1\", \"test2\"}";
458+
String defaultEnumArrayString = "var defaultValue []ExampleEnum = []ExampleEnum{\"example1\"}";
458459
String defaultValueString = "var defaultValue string = \"test3\"";
459-
TestUtils.assertFileContains(apiPath, defaultArrayString);
460+
TestUtils.assertFileContains(apiPath, defaultStringArrayString);
461+
TestUtils.assertFileContains(apiPath, defaultEnumArrayString);
460462
TestUtils.assertFileContains(apiPath, defaultValueString);
461463
}
462464
}

modules/openapi-generator/src/test/resources/3_1/issue_21077.yaml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@ paths:
1818
in: query
1919
schema:
2020
type: array
21-
default: ["test1", "test2", 1]
21+
items:
22+
type: string
23+
default: ["test1", "test2"]
24+
- name: "enumarrayparam"
25+
in: query
26+
schema:
27+
type: array
28+
items:
29+
$ref: "#/components/schemas/ExampleEnum"
30+
default: ["example1"]
2231
- name: "stringparam"
2332
in: query
2433
schema:
@@ -27,3 +36,11 @@ paths:
2736
responses:
2837
200:
2938
description: Ok
39+
40+
components:
41+
schemas:
42+
ExampleEnum:
43+
type: string
44+
enum:
45+
- example1
46+
- example2

0 commit comments

Comments
 (0)