Skip to content

Commit a4d05b3

Browse files
[go] Correctly set default array value on query parameters (#22060)
1 parent 75ae04e commit a4d05b3

File tree

10 files changed

+87
-16
lines changed

10 files changed

+87
-16
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.openapitools.codegen.languages;
1919

20+
import com.fasterxml.jackson.databind.node.ArrayNode;
2021
import com.google.common.collect.Iterables;
2122
import com.samskivert.mustache.Mustache;
2223
import io.swagger.v3.oas.models.media.Schema;
@@ -440,6 +441,17 @@ public String toDefaultValue(Schema p) {
440441
return null;
441442
}
442443

444+
if (ModelUtils.isArraySchema(p)) {
445+
StringJoiner joinedDefaultValues = new StringJoiner(", ");
446+
Object defaultValues = p.getDefault();
447+
if (defaultValues instanceof ArrayNode) {
448+
for (var value : (ArrayNode) defaultValues) {
449+
joinedDefaultValues.add(value.toString());
450+
}
451+
return "{" + joinedDefaultValues + "}";
452+
}
453+
}
454+
443455
return super.toDefaultValue(p);
444456
}
445457

modules/openapi-generator/src/main/resources/go/api.mustache

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,16 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
217217
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", r.{{paramName}}, "{{style}}", "{{collectionFormat}}")
218218
{{/isCollectionFormatMulti}}
219219
{{#defaultValue}}} else {
220-
var defaultValue {{{dataType}}} = {{{.}}}
221-
r.{{paramName}} = &defaultValue
220+
{{#isArray}}
221+
var defaultValue {{{dataType}}} = {{{dataType}}}{{{.}}}
222+
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", defaultValue, "{{style}}", "{{collectionFormat}}")
223+
r.{{paramName}} = &defaultValue
224+
{{/isArray}}
225+
{{^isArray}}
226+
var defaultValue {{{dataType}}} = {{{.}}}
227+
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", defaultValue, "{{style}}", "{{collectionFormat}}")
228+
r.{{paramName}} = &defaultValue
229+
{{/isArray}}
222230
{{/defaultValue}}}
223231
{{/required}}
224232
{{/queryParams}}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,24 @@ public void testXmlOptionsBeingUsed() throws IOException {
418418

419419
TestUtils.assertFileContains(Paths.get(output + "/model_pet.go"), "tags>tag");
420420
}
421+
422+
@Test
423+
public void testArrayDefaultValue() throws IOException {
424+
File output = Files.createTempDirectory("test").toFile();
425+
output.deleteOnExit();
426+
427+
final CodegenConfigurator configurator = new CodegenConfigurator()
428+
.setGeneratorName("go")
429+
.setInputSpec("src/test/resources/3_1/issue_21077.yaml")
430+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
431+
432+
DefaultGenerator generator = new DefaultGenerator();
433+
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
434+
files.forEach(File::deleteOnExit);
435+
Path apiPath = Paths.get(output + "/api_default.go");
436+
String defaultArrayString = "var defaultValue []interface{} = []interface{}{\"test1\", \"test2\", 1}";
437+
String defaultValueString = "var defaultValue string = \"test3\"";
438+
TestUtils.assertFileContains(apiPath, defaultArrayString);
439+
TestUtils.assertFileContains(apiPath, defaultValueString);
440+
}
421441
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
openapi: 3.1.0
2+
3+
info:
4+
title: Test
5+
version: "1.0"
6+
7+
paths:
8+
/a:
9+
post:
10+
summary: Test
11+
requestBody:
12+
content:
13+
application/json:
14+
schema:
15+
type: string
16+
parameters:
17+
- name: "arrayparam"
18+
in: query
19+
schema:
20+
type: array
21+
default: ["test1", "test2", 1]
22+
- name: "stringparam"
23+
in: query
24+
schema:
25+
type: string
26+
default: "test3"
27+
responses:
28+
200:
29+
description: Ok

samples/client/echo_api/go-external-refs/docs/DefaultValue.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
Name | Type | Description | Notes
66
------------ | ------------- | ------------- | -------------
7-
**ArrayStringEnumRefDefault** | Pointer to [**[]StringEnumRef**](StringEnumRef.md) | | [optional] [default to ["success","failure"]]
8-
**ArrayStringEnumDefault** | Pointer to **[]string** | | [optional] [default to ["success","failure"]]
9-
**ArrayStringDefault** | Pointer to **[]string** | | [optional] [default to ["failure","skipped"]]
10-
**ArrayIntegerDefault** | Pointer to **[]int32** | | [optional] [default to [1,3]]
7+
**ArrayStringEnumRefDefault** | Pointer to [**[]StringEnumRef**](StringEnumRef.md) | | [optional] [default to {"success", "failure"}]
8+
**ArrayStringEnumDefault** | Pointer to **[]string** | | [optional] [default to {"success", "failure"}]
9+
**ArrayStringDefault** | Pointer to **[]string** | | [optional] [default to {"failure", "skipped"}]
10+
**ArrayIntegerDefault** | Pointer to **[]int32** | | [optional] [default to {1, 3}]
1111
**ArrayString** | Pointer to **[]string** | | [optional]
1212
**ArrayStringNullable** | Pointer to **[]string** | | [optional]
1313
**ArrayStringExtensionNullable** | Pointer to **[]string** | | [optional]

samples/client/echo_api/go-external-refs/docs/Query.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Name | Type | Description | Notes
66
------------ | ------------- | ------------- | -------------
77
**Id** | Pointer to **int64** | Query | [optional]
8-
**Outcomes** | Pointer to **[]string** | | [optional] [default to ["SUCCESS","FAILURE"]]
8+
**Outcomes** | Pointer to **[]string** | | [optional] [default to {"SUCCESS", "FAILURE"}]
99

1010
## Methods
1111

samples/client/echo_api/go/docs/DefaultValue.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
Name | Type | Description | Notes
66
------------ | ------------- | ------------- | -------------
7-
**ArrayStringEnumRefDefault** | Pointer to [**[]StringEnumRef**](StringEnumRef.md) | | [optional] [default to ["success","failure"]]
8-
**ArrayStringEnumDefault** | Pointer to **[]string** | | [optional] [default to ["success","failure"]]
9-
**ArrayStringDefault** | Pointer to **[]string** | | [optional] [default to ["failure","skipped"]]
10-
**ArrayIntegerDefault** | Pointer to **[]int32** | | [optional] [default to [1,3]]
7+
**ArrayStringEnumRefDefault** | Pointer to [**[]StringEnumRef**](StringEnumRef.md) | | [optional] [default to {"success", "failure"}]
8+
**ArrayStringEnumDefault** | Pointer to **[]string** | | [optional] [default to {"success", "failure"}]
9+
**ArrayStringDefault** | Pointer to **[]string** | | [optional] [default to {"failure", "skipped"}]
10+
**ArrayIntegerDefault** | Pointer to **[]int32** | | [optional] [default to {1, 3}]
1111
**ArrayString** | Pointer to **[]string** | | [optional]
1212
**ArrayStringNullable** | Pointer to **[]string** | | [optional]
1313
**ArrayStringExtensionNullable** | Pointer to **[]string** | | [optional]

samples/client/echo_api/go/docs/Query.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Name | Type | Description | Notes
66
------------ | ------------- | ------------- | -------------
77
**Id** | Pointer to **int64** | Query | [optional]
8-
**Outcomes** | Pointer to **[]string** | | [optional] [default to ["SUCCESS","FAILURE"]]
8+
**Outcomes** | Pointer to **[]string** | | [optional] [default to {"SUCCESS", "FAILURE"}]
99

1010
## Methods
1111

samples/client/petstore/go/go-petstore/api_fake.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/client/petstore/go/go-petstore/api_fake.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)