mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-21 09:57:10 +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;
|
package org.openapitools.codegen.languages;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
import com.samskivert.mustache.Mustache;
|
import com.samskivert.mustache.Mustache;
|
||||||
import io.swagger.v3.oas.models.media.Schema;
|
import io.swagger.v3.oas.models.media.Schema;
|
||||||
@@ -440,6 +441,17 @@ public class GoClientCodegen extends AbstractGoCodegen {
|
|||||||
return null;
|
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);
|
return super.toDefaultValue(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -217,8 +217,16 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
|
|||||||
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", r.{{paramName}}, "{{style}}", "{{collectionFormat}}")
|
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", r.{{paramName}}, "{{style}}", "{{collectionFormat}}")
|
||||||
{{/isCollectionFormatMulti}}
|
{{/isCollectionFormatMulti}}
|
||||||
{{#defaultValue}}} else {
|
{{#defaultValue}}} else {
|
||||||
var defaultValue {{{dataType}}} = {{{.}}}
|
{{#isArray}}
|
||||||
|
var defaultValue {{{dataType}}} = {{{dataType}}}{{{.}}}
|
||||||
|
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", defaultValue, "{{style}}", "{{collectionFormat}}")
|
||||||
r.{{paramName}} = &defaultValue
|
r.{{paramName}} = &defaultValue
|
||||||
|
{{/isArray}}
|
||||||
|
{{^isArray}}
|
||||||
|
var defaultValue {{{dataType}}} = {{{.}}}
|
||||||
|
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", defaultValue, "{{style}}", "{{collectionFormat}}")
|
||||||
|
r.{{paramName}} = &defaultValue
|
||||||
|
{{/isArray}}
|
||||||
{{/defaultValue}}}
|
{{/defaultValue}}}
|
||||||
{{/required}}
|
{{/required}}
|
||||||
{{/queryParams}}
|
{{/queryParams}}
|
||||||
|
|||||||
@@ -418,4 +418,24 @@ public class GoClientCodegenTest {
|
|||||||
|
|
||||||
TestUtils.assertFileContains(Paths.get(output + "/model_pet.go"), "tags>tag");
|
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
|
Name | Type | Description | Notes
|
||||||
------------ | ------------- | ------------- | -------------
|
------------ | ------------- | ------------- | -------------
|
||||||
**ArrayStringEnumRefDefault** | Pointer to [**[]StringEnumRef**](StringEnumRef.md) | | [optional] [default to ["success","failure"]]
|
**ArrayStringEnumRefDefault** | Pointer to [**[]StringEnumRef**](StringEnumRef.md) | | [optional] [default to {"success", "failure"}]
|
||||||
**ArrayStringEnumDefault** | Pointer to **[]string** | | [optional] [default to ["success","failure"]]
|
**ArrayStringEnumDefault** | Pointer to **[]string** | | [optional] [default to {"success", "failure"}]
|
||||||
**ArrayStringDefault** | Pointer to **[]string** | | [optional] [default to ["failure","skipped"]]
|
**ArrayStringDefault** | Pointer to **[]string** | | [optional] [default to {"failure", "skipped"}]
|
||||||
**ArrayIntegerDefault** | Pointer to **[]int32** | | [optional] [default to [1,3]]
|
**ArrayIntegerDefault** | Pointer to **[]int32** | | [optional] [default to {1, 3}]
|
||||||
**ArrayString** | Pointer to **[]string** | | [optional]
|
**ArrayString** | Pointer to **[]string** | | [optional]
|
||||||
**ArrayStringNullable** | Pointer to **[]string** | | [optional]
|
**ArrayStringNullable** | Pointer to **[]string** | | [optional]
|
||||||
**ArrayStringExtensionNullable** | Pointer to **[]string** | | [optional]
|
**ArrayStringExtensionNullable** | Pointer to **[]string** | | [optional]
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
Name | Type | Description | Notes
|
Name | Type | Description | Notes
|
||||||
------------ | ------------- | ------------- | -------------
|
------------ | ------------- | ------------- | -------------
|
||||||
**Id** | Pointer to **int64** | Query | [optional]
|
**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
|
## Methods
|
||||||
|
|
||||||
|
|||||||
@@ -4,10 +4,10 @@
|
|||||||
|
|
||||||
Name | Type | Description | Notes
|
Name | Type | Description | Notes
|
||||||
------------ | ------------- | ------------- | -------------
|
------------ | ------------- | ------------- | -------------
|
||||||
**ArrayStringEnumRefDefault** | Pointer to [**[]StringEnumRef**](StringEnumRef.md) | | [optional] [default to ["success","failure"]]
|
**ArrayStringEnumRefDefault** | Pointer to [**[]StringEnumRef**](StringEnumRef.md) | | [optional] [default to {"success", "failure"}]
|
||||||
**ArrayStringEnumDefault** | Pointer to **[]string** | | [optional] [default to ["success","failure"]]
|
**ArrayStringEnumDefault** | Pointer to **[]string** | | [optional] [default to {"success", "failure"}]
|
||||||
**ArrayStringDefault** | Pointer to **[]string** | | [optional] [default to ["failure","skipped"]]
|
**ArrayStringDefault** | Pointer to **[]string** | | [optional] [default to {"failure", "skipped"}]
|
||||||
**ArrayIntegerDefault** | Pointer to **[]int32** | | [optional] [default to [1,3]]
|
**ArrayIntegerDefault** | Pointer to **[]int32** | | [optional] [default to {1, 3}]
|
||||||
**ArrayString** | Pointer to **[]string** | | [optional]
|
**ArrayString** | Pointer to **[]string** | | [optional]
|
||||||
**ArrayStringNullable** | Pointer to **[]string** | | [optional]
|
**ArrayStringNullable** | Pointer to **[]string** | | [optional]
|
||||||
**ArrayStringExtensionNullable** | Pointer to **[]string** | | [optional]
|
**ArrayStringExtensionNullable** | Pointer to **[]string** | | [optional]
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
Name | Type | Description | Notes
|
Name | Type | Description | Notes
|
||||||
------------ | ------------- | ------------- | -------------
|
------------ | ------------- | ------------- | -------------
|
||||||
**Id** | Pointer to **int64** | Query | [optional]
|
**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
|
## Methods
|
||||||
|
|
||||||
|
|||||||
@@ -1426,6 +1426,7 @@ func (a *FakeAPIService) TestEnumParametersExecute(r ApiTestEnumParametersReques
|
|||||||
parameterAddToHeaderOrQuery(localVarQueryParams, "enum_query_string", r.enumQueryString, "", "")
|
parameterAddToHeaderOrQuery(localVarQueryParams, "enum_query_string", r.enumQueryString, "", "")
|
||||||
} else {
|
} else {
|
||||||
var defaultValue string = "-efg"
|
var defaultValue string = "-efg"
|
||||||
|
parameterAddToHeaderOrQuery(localVarQueryParams, "enum_query_string", defaultValue, "", "")
|
||||||
r.enumQueryString = &defaultValue
|
r.enumQueryString = &defaultValue
|
||||||
}
|
}
|
||||||
if r.enumQueryInteger != nil {
|
if r.enumQueryInteger != nil {
|
||||||
|
|||||||
@@ -1752,6 +1752,7 @@ func (a *FakeAPIService) TestEnumParametersExecute(r ApiTestEnumParametersReques
|
|||||||
parameterAddToHeaderOrQuery(localVarQueryParams, "enum_query_string", r.enumQueryString, "form", "")
|
parameterAddToHeaderOrQuery(localVarQueryParams, "enum_query_string", r.enumQueryString, "form", "")
|
||||||
} else {
|
} else {
|
||||||
var defaultValue string = "-efg"
|
var defaultValue string = "-efg"
|
||||||
|
parameterAddToHeaderOrQuery(localVarQueryParams, "enum_query_string", defaultValue, "form", "")
|
||||||
r.enumQueryString = &defaultValue
|
r.enumQueryString = &defaultValue
|
||||||
}
|
}
|
||||||
if r.enumQueryInteger != nil {
|
if r.enumQueryInteger != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user