Skip to content

Commit 7afd350

Browse files
authored
[WIP] Escape import with underscore in go-experimental client (#3659)
* escape import with underscore in go client * update ioutil * Fix collectionFormat=multi request bug in go experimental
1 parent 7e772e1 commit 7afd350

53 files changed

Lines changed: 606 additions & 404 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ package {{packageName}}
33

44
{{#operations}}
55
import (
6-
"context"
7-
"io/ioutil"
8-
"net/http"
9-
"net/url"
6+
_context "context"
7+
_ioutil "io/ioutil"
8+
_nethttp "net/http"
9+
_neturl "net/url"
1010
{{#imports}} "{{import}}"
1111
{{/imports}}
1212
)
1313

1414
// Linger please
1515
var (
16-
_ context.Context
16+
_ _context.Context
1717
)
1818

1919
type {{classname}}Service service
@@ -24,7 +24,7 @@ type {{classname}}Service service
2424
{{#notes}}
2525
{{notes}}
2626
{{/notes}}
27-
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
27+
* @param ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
2828
{{#allParams}}
2929
{{#required}}
3030
* @param {{paramName}}{{#description}} {{{.}}}{{/description}}
@@ -63,9 +63,9 @@ type {{{nickname}}}Opts struct {
6363
}
6464

6565
{{/hasOptionalParams}}
66-
func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams}}, {{/hasParams}}{{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}localVarOptionals *{{{nickname}}}Opts{{/hasOptionalParams}}) ({{#returnType}}{{{returnType}}}, {{/returnType}}*http.Response, error) {
66+
func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#hasParams}}, {{/hasParams}}{{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}localVarOptionals *{{{nickname}}}Opts{{/hasOptionalParams}}) ({{#returnType}}{{{returnType}}}, {{/returnType}}*_nethttp.Response, error) {
6767
var (
68-
localVarHttpMethod = http.Method{{httpMethod}}
68+
localVarHttpMethod = _nethttp.Method{{httpMethod}}
6969
localVarPostBody interface{}
7070
localVarFormFileName string
7171
localVarFileName string
@@ -77,11 +77,11 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams}
7777

7878
// create path and map variables
7979
localVarPath := a.client.cfg.BasePath + "{{{path}}}"{{#pathParams}}
80-
localVarPath = strings.Replace(localVarPath, "{"+"{{baseName}}"+"}", url.QueryEscape(fmt.Sprintf("%v", {{paramName}})), -1){{/pathParams}}
80+
localVarPath = strings.Replace(localVarPath, "{"+"{{baseName}}"+"}", _neturl.QueryEscape(fmt.Sprintf("%v", {{paramName}})), -1){{/pathParams}}
8181

8282
localVarHeaderParams := make(map[string]string)
83-
localVarQueryParams := url.Values{}
84-
localVarFormParams := url.Values{}
83+
localVarQueryParams := _neturl.Values{}
84+
localVarFormParams := _neturl.Values{}
8585
{{#allParams}}
8686
{{#required}}
8787
{{#minItems}}
@@ -132,11 +132,37 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams}
132132
{{#hasQueryParams}}
133133
{{#queryParams}}
134134
{{#required}}
135+
{{#isCollectionFormatMulti}}
136+
t:={{paramName}}
137+
if reflect.TypeOf(t).Kind() == reflect.Slice {
138+
s := reflect.ValueOf(t)
139+
for i := 0; i < s.Len(); i++ {
140+
localVarQueryParams.Add("{{baseName}}", parameterToString(s.Index(i), "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
141+
}
142+
} else {
143+
localVarQueryParams.Add("{{baseName}}", parameterToString(t, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
144+
}
145+
{{/isCollectionFormatMulti}}
146+
{{^isCollectionFormatMulti}}
135147
localVarQueryParams.Add("{{baseName}}", parameterToString({{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
148+
{{/isCollectionFormatMulti}}
136149
{{/required}}
137150
{{^required}}
138151
if localVarOptionals != nil && localVarOptionals.{{vendorExtensions.x-exportParamName}}.IsSet() {
152+
{{#isCollectionFormatMulti}}
153+
t:=localVarOptionals.{{vendorExtensions.x-exportParamName}}.Value()
154+
if reflect.TypeOf(t).Kind() == reflect.Slice {
155+
s := reflect.ValueOf(t)
156+
for i := 0; i < s.Len(); i++ {
157+
localVarQueryParams.Add("{{baseName}}", parameterToString(s.Index(i), "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
158+
}
159+
} else {
160+
localVarQueryParams.Add("{{baseName}}", parameterToString(t, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
161+
}
162+
{{/isCollectionFormatMulti}}
163+
{{^isCollectionFormatMulti}}
139164
localVarQueryParams.Add("{{baseName}}", parameterToString(localVarOptionals.{{vendorExtensions.x-exportParamName}}.Value(), "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
165+
{{/isCollectionFormatMulti}}
140166
}
141167
{{/required}}
142168
{{/queryParams}}
@@ -192,7 +218,7 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams}
192218
}
193219
{{/required}}
194220
if localVarFile != nil {
195-
fbs, _ := ioutil.ReadAll(localVarFile)
221+
fbs, _ := _ioutil.ReadAll(localVarFile)
196222
localVarFileBytes = fbs
197223
localVarFileName = localVarFile.Name()
198224
localVarFile.Close()
@@ -277,7 +303,7 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams}
277303
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHttpResponse, err
278304
}
279305

280-
localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body)
306+
localVarBody, err := _ioutil.ReadAll(localVarHttpResponse.Body)
281307
localVarHttpResponse.Body.Close()
282308
if err != nil {
283309
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHttpResponse, err
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.1.0-SNAPSHOT
1+
4.1.1-SNAPSHOT

samples/client/petstore/go-experimental/go-petstore/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Class | Method | HTTP request | Description
4646
*FakeApi* | [**TestGroupParameters**](docs/FakeApi.md#testgroupparameters) | **Delete** /fake | Fake endpoint to test group parameters (optional)
4747
*FakeApi* | [**TestInlineAdditionalProperties**](docs/FakeApi.md#testinlineadditionalproperties) | **Post** /fake/inline-additionalProperties | test inline additionalProperties
4848
*FakeApi* | [**TestJsonFormData**](docs/FakeApi.md#testjsonformdata) | **Get** /fake/jsonFormData | test json serialization of form data
49+
*FakeApi* | [**TestQueryParameterCollectionFormat**](docs/FakeApi.md#testqueryparametercollectionformat) | **Put** /fake/test-query-paramters |
4950
*FakeClassnameTags123Api* | [**TestClassname**](docs/FakeClassnameTags123Api.md#testclassname) | **Patch** /fake_classname_test | To test class name in snake case
5051
*PetApi* | [**AddPet**](docs/PetApi.md#addpet) | **Post** /pet | Add a new pet to the store
5152
*PetApi* | [**DeletePet**](docs/PetApi.md#deletepet) | **Delete** /pet/{petId} | Deletes a pet

samples/client/petstore/go-experimental/go-petstore/api/openapi.yaml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,59 @@ paths:
11001100
tags:
11011101
- fake
11021102
x-codegen-request-body-name: body
1103+
/fake/test-query-paramters:
1104+
put:
1105+
description: To test the collection format in query parameters
1106+
operationId: testQueryParameterCollectionFormat
1107+
parameters:
1108+
- explode: false
1109+
in: query
1110+
name: pipe
1111+
required: true
1112+
schema:
1113+
items:
1114+
type: string
1115+
type: array
1116+
style: form
1117+
- in: query
1118+
name: ioutil
1119+
required: true
1120+
schema:
1121+
items:
1122+
type: string
1123+
type: array
1124+
- in: query
1125+
name: http
1126+
required: true
1127+
schema:
1128+
items:
1129+
type: string
1130+
type: array
1131+
style: spaceDelimited
1132+
- explode: false
1133+
in: query
1134+
name: url
1135+
required: true
1136+
schema:
1137+
items:
1138+
type: string
1139+
type: array
1140+
style: form
1141+
- explode: true
1142+
in: query
1143+
name: context
1144+
required: true
1145+
schema:
1146+
items:
1147+
type: string
1148+
type: array
1149+
style: form
1150+
responses:
1151+
200:
1152+
content: {}
1153+
description: Success
1154+
tags:
1155+
- fake
11031156
/fake/{petId}/uploadImageWithRequiredFile:
11041157
post:
11051158
operationId: uploadFileWithRequiredFile

samples/client/petstore/go-experimental/go-petstore/api_another_fake.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,29 @@
1010
package petstore
1111

1212
import (
13-
"context"
14-
"io/ioutil"
15-
"net/http"
16-
"net/url"
13+
_context "context"
14+
_ioutil "io/ioutil"
15+
_nethttp "net/http"
16+
_neturl "net/url"
1717
)
1818

1919
// Linger please
2020
var (
21-
_ context.Context
21+
_ _context.Context
2222
)
2323

2424
type AnotherFakeApiService service
2525

2626
/*
2727
AnotherFakeApiService To test special tags
2828
To test special tags and operation ID starting with number
29-
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
29+
* @param ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
3030
* @param body client model
3131
@return Client
3232
*/
33-
func (a *AnotherFakeApiService) Call123TestSpecialTags(ctx context.Context, body Client) (Client, *http.Response, error) {
33+
func (a *AnotherFakeApiService) Call123TestSpecialTags(ctx _context.Context, body Client) (Client, *_nethttp.Response, error) {
3434
var (
35-
localVarHttpMethod = http.MethodPatch
35+
localVarHttpMethod = _nethttp.MethodPatch
3636
localVarPostBody interface{}
3737
localVarFormFileName string
3838
localVarFileName string
@@ -44,8 +44,8 @@ func (a *AnotherFakeApiService) Call123TestSpecialTags(ctx context.Context, body
4444
localVarPath := a.client.cfg.BasePath + "/another-fake/dummy"
4545

4646
localVarHeaderParams := make(map[string]string)
47-
localVarQueryParams := url.Values{}
48-
localVarFormParams := url.Values{}
47+
localVarQueryParams := _neturl.Values{}
48+
localVarFormParams := _neturl.Values{}
4949

5050
// to determine the Content-Type header
5151
localVarHttpContentTypes := []string{"application/json"}
@@ -76,7 +76,7 @@ func (a *AnotherFakeApiService) Call123TestSpecialTags(ctx context.Context, body
7676
return localVarReturnValue, localVarHttpResponse, err
7777
}
7878

79-
localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body)
79+
localVarBody, err := _ioutil.ReadAll(localVarHttpResponse.Body)
8080
localVarHttpResponse.Body.Close()
8181
if err != nil {
8282
return localVarReturnValue, localVarHttpResponse, err

0 commit comments

Comments
 (0)