Skip to content

Commit 19fd48e

Browse files
grokifywing328
authored andcommitted
[Golang][client] fix formData file parameter name use (#506)
* support form-data file MIME `name` parameter * update test spec and Go client for form-data file name * update samples for ensure-up-to-date * update mustache template * update samples for Go client * improve assignment location * update samples
1 parent 9383234 commit 19fd48e

101 files changed

Lines changed: 536 additions & 489 deletions

File tree

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/api.mustache

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ type {{{nickname}}}Opts struct {
6767
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) {
6868
var (
6969
localVarHttpMethod = strings.ToUpper("{{httpMethod}}")
70-
localVarPostBody interface{}
71-
localVarFileName string
72-
localVarFileBytes []byte
70+
localVarPostBody interface{}
71+
localVarFormFileName string
72+
localVarFileName string
73+
localVarFileBytes []byte
7374
{{#returnType}}
7475
localVarReturnValue {{{returnType}}}
7576
{{/returnType}}
@@ -177,6 +178,7 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams}
177178
{{#hasFormParams}}
178179
{{#formParams}}
179180
{{#isFile}}
181+
localVarFormFileName = "{{baseName}}"
180182
{{#required}}
181183
localVarFile := {{paramName}}
182184
{{/required}}
@@ -254,7 +256,7 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams}
254256

255257
{{/isApiKey}}
256258
{{/authMethods}}
257-
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
259+
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFileName, localVarFileName, localVarFileBytes)
258260
if err != nil {
259261
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, err
260262
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ func (c *APIClient) prepareRequest(
167167
headerParams map[string]string,
168168
queryParams url.Values,
169169
formParams url.Values,
170+
formFileName string,
170171
fileName string,
171172
fileBytes []byte) (localVarRequest *http.Request, err error) {
172173
@@ -209,7 +210,7 @@ func (c *APIClient) prepareRequest(
209210
if len(fileBytes) > 0 && fileName != "" {
210211
w.Boundary()
211212
//_, fileNm := filepath.Split(fileName)
212-
part, err := w.CreateFormFile("file", filepath.Base(fileName))
213+
part, err := w.CreateFormFile(formFileName, filepath.Base(fileName))
213214
if err != nil {
214215
return nil, err
215216
}

modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ paths:
987987
description: Additional data to pass to server
988988
required: false
989989
type: string
990-
- name: file
990+
- name: requiredFile
991991
in: formData
992992
description: file to upload
993993
required: true
@@ -1526,7 +1526,7 @@ definitions:
15261526
$ref: "#/definitions/File"
15271527
File:
15281528
type: object
1529-
desription: 'Must be named `File` for test.'
1529+
description: 'Must be named `File` for test.'
15301530
properties:
15311531
sourceURI:
15321532
description: 'Test capitalization'

modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
openapi: 3.0.0
2-
servers:
3-
- url: 'http://petstore.swagger.io:80/v2'
42
info:
53
description: >-
64
This spec is mainly for testing Petstore server and contains fake endpoints,
@@ -573,7 +571,6 @@ paths:
573571
parameters:
574572
- name: enum_header_string_array
575573
in: header
576-
explode: true
577574
description: Header parameter enum test (string array)
578575
schema:
579576
type: array
@@ -964,12 +961,14 @@ paths:
964961
additionalMetadata:
965962
description: Additional data to pass to server
966963
type: string
967-
file:
964+
requiredFile:
968965
description: file to upload
969966
type: string
970967
format: binary
971968
required:
972-
- file
969+
- requiredFile
970+
servers:
971+
- url: 'http://petstore.swagger.io:80/v2'
973972
components:
974973
requestBodies:
975974
UserArray:

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,12 +1008,12 @@ paths:
10081008
additionalMetadata:
10091009
description: Additional data to pass to server
10101010
type: string
1011-
file:
1011+
requiredFile:
10121012
description: file to upload
10131013
format: binary
10141014
type: string
10151015
required:
1016-
- file
1016+
- requiredFile
10171017
required: true
10181018
responses:
10191019
200:
@@ -1507,6 +1507,7 @@ components:
15071507
$ref: '#/components/schemas/Animal'
15081508
type: array
15091509
File:
1510+
description: Must be named `File` for test.
15101511
example:
15111512
sourceURI: sourceURI
15121513
properties:

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ To test special tags
3434
func (a *AnotherFakeApiService) TestSpecialTags(ctx context.Context, client Client) (Client, *http.Response, error) {
3535
var (
3636
localVarHttpMethod = strings.ToUpper("Patch")
37-
localVarPostBody interface{}
38-
localVarFileName string
39-
localVarFileBytes []byte
37+
localVarPostBody interface{}
38+
localVarFormFileName string
39+
localVarFileName string
40+
localVarFileBytes []byte
4041
localVarReturnValue Client
4142
)
4243

@@ -66,7 +67,7 @@ func (a *AnotherFakeApiService) TestSpecialTags(ctx context.Context, client Clie
6667
}
6768
// body params
6869
localVarPostBody = &client
69-
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
70+
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFileName, localVarFileName, localVarFileBytes)
7071
if err != nil {
7172
return localVarReturnValue, nil, err
7273
}

0 commit comments

Comments
 (0)