mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-21 05:17:08 +00:00
[go] Correctly set default array value on query parameters (#22060)
This commit is contained in:
committed by
GitHub
parent
75ae04ecfd
commit
a4d05b38f5
@@ -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 class GoClientCodegen extends AbstractGoCodegen {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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}}} = {{{.}}}
|
||||
{{#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}}
|
||||
|
||||
@@ -418,4 +418,24 @@ public class GoClientCodegenTest {
|
||||
|
||||
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<File> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -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]
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -1426,6 +1426,7 @@ func (a *FakeAPIService) TestEnumParametersExecute(r ApiTestEnumParametersReques
|
||||
parameterAddToHeaderOrQuery(localVarQueryParams, "enum_query_string", r.enumQueryString, "", "")
|
||||
} else {
|
||||
var defaultValue string = "-efg"
|
||||
parameterAddToHeaderOrQuery(localVarQueryParams, "enum_query_string", defaultValue, "", "")
|
||||
r.enumQueryString = &defaultValue
|
||||
}
|
||||
if r.enumQueryInteger != nil {
|
||||
|
||||
@@ -1752,6 +1752,7 @@ func (a *FakeAPIService) TestEnumParametersExecute(r ApiTestEnumParametersReques
|
||||
parameterAddToHeaderOrQuery(localVarQueryParams, "enum_query_string", r.enumQueryString, "form", "")
|
||||
} else {
|
||||
var defaultValue string = "-efg"
|
||||
parameterAddToHeaderOrQuery(localVarQueryParams, "enum_query_string", defaultValue, "form", "")
|
||||
r.enumQueryString = &defaultValue
|
||||
}
|
||||
if r.enumQueryInteger != nil {
|
||||
|
||||
Reference in New Issue
Block a user