From 19c1acf06973002c293c952bd7c7b816e7848307 Mon Sep 17 00:00:00 2001 From: Rens Groothuijsen Date: Wed, 1 Oct 2025 22:13:51 +0200 Subject: [PATCH] [go] Correctly set default array value on query parameters --- .../codegen/languages/GoClientCodegen.java | 12 ++++++++ .../src/main/resources/go/api.mustache | 12 ++++++-- .../codegen/go/GoClientCodegenTest.java | 20 +++++++++++++ .../src/test/resources/3_1/issue_21077.yaml | 29 +++++++++++++++++++ .../go-external-refs/docs/DefaultValue.md | 8 ++--- .../echo_api/go-external-refs/docs/Query.md | 2 +- .../client/echo_api/go/docs/DefaultValue.md | 8 ++--- samples/client/echo_api/go/docs/Query.md | 2 +- .../petstore/go/go-petstore/api_fake.go | 5 ++-- .../petstore/go/go-petstore/api_fake.go | 5 ++-- 10 files changed, 87 insertions(+), 16 deletions(-) create mode 100644 modules/openapi-generator/src/test/resources/3_1/issue_21077.yaml 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 b7e788cc56b1..7b26d2e120c0 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 @@ -17,6 +17,7 @@ package org.openapitools.codegen.languages; +import com.fasterxml.jackson.databind.node.ArrayNode; import com.google.common.collect.Iterables; import com.samskivert.mustache.Mustache; import io.swagger.v3.oas.models.media.Schema; @@ -440,6 +441,17 @@ public String toDefaultValue(Schema p) { return null; } + if (ModelUtils.isArraySchema(p)) { + StringJoiner joinedDefaultValues = new StringJoiner(", "); + Object defaultValues = p.getDefault(); + if (defaultValues instanceof ArrayNode) { + for (var value : (ArrayNode) defaultValues) { + joinedDefaultValues.add(value.toString()); + } + return "{" + joinedDefaultValues + "}"; + } + } + return super.toDefaultValue(p); } diff --git a/modules/openapi-generator/src/main/resources/go/api.mustache b/modules/openapi-generator/src/main/resources/go/api.mustache index 84da8017fdf9..f2d06ae4a05b 100644 --- a/modules/openapi-generator/src/main/resources/go/api.mustache +++ b/modules/openapi-generator/src/main/resources/go/api.mustache @@ -217,8 +217,16 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", r.{{paramName}}, "{{style}}", "{{collectionFormat}}") {{/isCollectionFormatMulti}} {{#defaultValue}}} else { - var defaultValue {{{dataType}}} = {{{.}}} - r.{{paramName}} = &defaultValue + {{#isArray}} + var defaultValue {{{dataType}}} = {{{dataType}}}{{{.}}} + parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", defaultValue, "{{style}}", "{{collectionFormat}}") + r.{{paramName}} = &defaultValue + {{/isArray}} + {{^isArray}} + var defaultValue {{{dataType}}} = {{{.}}} + parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", defaultValue, "{{style}}", "{{collectionFormat}}") + r.{{paramName}} = &defaultValue + {{/isArray}} {{/defaultValue}}} {{/required}} {{/queryParams}} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java index 155af6dd84dc..c65975aeba54 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java @@ -418,4 +418,24 @@ public void testXmlOptionsBeingUsed() throws IOException { TestUtils.assertFileContains(Paths.get(output + "/model_pet.go"), "tags>tag"); } + + @Test + public void testArrayDefaultValue() throws IOException { + File output = Files.createTempDirectory("test").toFile(); + output.deleteOnExit(); + + final CodegenConfigurator configurator = new CodegenConfigurator() + .setGeneratorName("go") + .setInputSpec("src/test/resources/3_1/issue_21077.yaml") + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); + + DefaultGenerator generator = new DefaultGenerator(); + List files = generator.opts(configurator.toClientOptInput()).generate(); + files.forEach(File::deleteOnExit); + Path apiPath = Paths.get(output + "/api_default.go"); + String defaultArrayString = "var defaultValue []interface{} = []interface{}{\"test1\", \"test2\", 1}"; + String defaultValueString = "var defaultValue string = \"test3\""; + TestUtils.assertFileContains(apiPath, defaultArrayString); + TestUtils.assertFileContains(apiPath, defaultValueString); + } } diff --git a/modules/openapi-generator/src/test/resources/3_1/issue_21077.yaml b/modules/openapi-generator/src/test/resources/3_1/issue_21077.yaml new file mode 100644 index 000000000000..8e19b1e6a88a --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_1/issue_21077.yaml @@ -0,0 +1,29 @@ +openapi: 3.1.0 + +info: + title: Test + version: "1.0" + +paths: + /a: + post: + summary: Test + requestBody: + content: + application/json: + schema: + type: string + parameters: + - name: "arrayparam" + in: query + schema: + type: array + default: ["test1", "test2", 1] + - name: "stringparam" + in: query + schema: + type: string + default: "test3" + responses: + 200: + description: Ok diff --git a/samples/client/echo_api/go-external-refs/docs/DefaultValue.md b/samples/client/echo_api/go-external-refs/docs/DefaultValue.md index c6469d1db9a9..89c24172d2f1 100644 --- a/samples/client/echo_api/go-external-refs/docs/DefaultValue.md +++ b/samples/client/echo_api/go-external-refs/docs/DefaultValue.md @@ -4,10 +4,10 @@ Name | Type | Description | Notes ------------ | ------------- | ------------- | ------------- -**ArrayStringEnumRefDefault** | Pointer to [**[]StringEnumRef**](StringEnumRef.md) | | [optional] [default to ["success","failure"]] -**ArrayStringEnumDefault** | Pointer to **[]string** | | [optional] [default to ["success","failure"]] -**ArrayStringDefault** | Pointer to **[]string** | | [optional] [default to ["failure","skipped"]] -**ArrayIntegerDefault** | Pointer to **[]int32** | | [optional] [default to [1,3]] +**ArrayStringEnumRefDefault** | Pointer to [**[]StringEnumRef**](StringEnumRef.md) | | [optional] [default to {"success", "failure"}] +**ArrayStringEnumDefault** | Pointer to **[]string** | | [optional] [default to {"success", "failure"}] +**ArrayStringDefault** | Pointer to **[]string** | | [optional] [default to {"failure", "skipped"}] +**ArrayIntegerDefault** | Pointer to **[]int32** | | [optional] [default to {1, 3}] **ArrayString** | Pointer to **[]string** | | [optional] **ArrayStringNullable** | Pointer to **[]string** | | [optional] **ArrayStringExtensionNullable** | Pointer to **[]string** | | [optional] diff --git a/samples/client/echo_api/go-external-refs/docs/Query.md b/samples/client/echo_api/go-external-refs/docs/Query.md index 2216bdeabdae..5c44965725d9 100644 --- a/samples/client/echo_api/go-external-refs/docs/Query.md +++ b/samples/client/echo_api/go-external-refs/docs/Query.md @@ -5,7 +5,7 @@ Name | Type | Description | Notes ------------ | ------------- | ------------- | ------------- **Id** | Pointer to **int64** | Query | [optional] -**Outcomes** | Pointer to **[]string** | | [optional] [default to ["SUCCESS","FAILURE"]] +**Outcomes** | Pointer to **[]string** | | [optional] [default to {"SUCCESS", "FAILURE"}] ## Methods diff --git a/samples/client/echo_api/go/docs/DefaultValue.md b/samples/client/echo_api/go/docs/DefaultValue.md index c6469d1db9a9..89c24172d2f1 100644 --- a/samples/client/echo_api/go/docs/DefaultValue.md +++ b/samples/client/echo_api/go/docs/DefaultValue.md @@ -4,10 +4,10 @@ Name | Type | Description | Notes ------------ | ------------- | ------------- | ------------- -**ArrayStringEnumRefDefault** | Pointer to [**[]StringEnumRef**](StringEnumRef.md) | | [optional] [default to ["success","failure"]] -**ArrayStringEnumDefault** | Pointer to **[]string** | | [optional] [default to ["success","failure"]] -**ArrayStringDefault** | Pointer to **[]string** | | [optional] [default to ["failure","skipped"]] -**ArrayIntegerDefault** | Pointer to **[]int32** | | [optional] [default to [1,3]] +**ArrayStringEnumRefDefault** | Pointer to [**[]StringEnumRef**](StringEnumRef.md) | | [optional] [default to {"success", "failure"}] +**ArrayStringEnumDefault** | Pointer to **[]string** | | [optional] [default to {"success", "failure"}] +**ArrayStringDefault** | Pointer to **[]string** | | [optional] [default to {"failure", "skipped"}] +**ArrayIntegerDefault** | Pointer to **[]int32** | | [optional] [default to {1, 3}] **ArrayString** | Pointer to **[]string** | | [optional] **ArrayStringNullable** | Pointer to **[]string** | | [optional] **ArrayStringExtensionNullable** | Pointer to **[]string** | | [optional] diff --git a/samples/client/echo_api/go/docs/Query.md b/samples/client/echo_api/go/docs/Query.md index 2216bdeabdae..5c44965725d9 100644 --- a/samples/client/echo_api/go/docs/Query.md +++ b/samples/client/echo_api/go/docs/Query.md @@ -5,7 +5,7 @@ Name | Type | Description | Notes ------------ | ------------- | ------------- | ------------- **Id** | Pointer to **int64** | Query | [optional] -**Outcomes** | Pointer to **[]string** | | [optional] [default to ["SUCCESS","FAILURE"]] +**Outcomes** | Pointer to **[]string** | | [optional] [default to {"SUCCESS", "FAILURE"}] ## Methods diff --git a/samples/client/petstore/go/go-petstore/api_fake.go b/samples/client/petstore/go/go-petstore/api_fake.go index d37cf69d4192..48de6702cee6 100644 --- a/samples/client/petstore/go/go-petstore/api_fake.go +++ b/samples/client/petstore/go/go-petstore/api_fake.go @@ -1425,8 +1425,9 @@ func (a *FakeAPIService) TestEnumParametersExecute(r ApiTestEnumParametersReques if r.enumQueryString != nil { parameterAddToHeaderOrQuery(localVarQueryParams, "enum_query_string", r.enumQueryString, "", "") } else { - var defaultValue string = "-efg" - r.enumQueryString = &defaultValue + var defaultValue string = "-efg" + parameterAddToHeaderOrQuery(localVarQueryParams, "enum_query_string", defaultValue, "", "") + r.enumQueryString = &defaultValue } if r.enumQueryInteger != nil { parameterAddToHeaderOrQuery(localVarQueryParams, "enum_query_integer", r.enumQueryInteger, "", "") diff --git a/samples/openapi3/client/petstore/go/go-petstore/api_fake.go b/samples/openapi3/client/petstore/go/go-petstore/api_fake.go index 6bd258ae1f7a..063323ff0824 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/api_fake.go +++ b/samples/openapi3/client/petstore/go/go-petstore/api_fake.go @@ -1751,8 +1751,9 @@ func (a *FakeAPIService) TestEnumParametersExecute(r ApiTestEnumParametersReques if r.enumQueryString != nil { parameterAddToHeaderOrQuery(localVarQueryParams, "enum_query_string", r.enumQueryString, "form", "") } else { - var defaultValue string = "-efg" - r.enumQueryString = &defaultValue + var defaultValue string = "-efg" + parameterAddToHeaderOrQuery(localVarQueryParams, "enum_query_string", defaultValue, "form", "") + r.enumQueryString = &defaultValue } if r.enumQueryInteger != nil { parameterAddToHeaderOrQuery(localVarQueryParams, "enum_query_integer", r.enumQueryInteger, "form", "")