Skip to content

Commit

Permalink
[Golang][client] fix formData file parameter name use (OpenAPIToo…
Browse files Browse the repository at this point in the history
…ls#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
  • Loading branch information
grokify authored and wing328 committed Jul 12, 2018
1 parent 4f82e57 commit 29ce03a
Show file tree
Hide file tree
Showing 101 changed files with 536 additions and 489 deletions.
10 changes: 6 additions & 4 deletions modules/openapi-generator/src/main/resources/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ type {{{nickname}}}Opts struct {
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) {
var (
localVarHttpMethod = strings.ToUpper("{{httpMethod}}")
localVarPostBody interface{}
localVarFileName string
localVarFileBytes []byte
localVarPostBody interface{}
localVarFormFileName string
localVarFileName string
localVarFileBytes []byte
{{#returnType}}
localVarReturnValue {{{returnType}}}
{{/returnType}}
Expand Down Expand Up @@ -177,6 +178,7 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams}
{{#hasFormParams}}
{{#formParams}}
{{#isFile}}
localVarFormFileName = "{{baseName}}"
{{#required}}
localVarFile := {{paramName}}
{{/required}}
Expand Down Expand Up @@ -254,7 +256,7 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams}

{{/isApiKey}}
{{/authMethods}}
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFileName, localVarFileName, localVarFileBytes)
if err != nil {
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func (c *APIClient) prepareRequest(
headerParams map[string]string,
queryParams url.Values,
formParams url.Values,
formFileName string,
fileName string,
fileBytes []byte) (localVarRequest *http.Request, err error) {
Expand Down Expand Up @@ -209,7 +210,7 @@ func (c *APIClient) prepareRequest(
if len(fileBytes) > 0 && fileName != "" {
w.Boundary()
//_, fileNm := filepath.Split(fileName)
part, err := w.CreateFormFile("file", filepath.Base(fileName))
part, err := w.CreateFormFile(formFileName, filepath.Base(fileName))
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ paths:
description: Additional data to pass to server
required: false
type: string
- name: file
- name: requiredFile
in: formData
description: file to upload
required: true
Expand Down Expand Up @@ -1526,7 +1526,7 @@ definitions:
$ref: "#/definitions/File"
File:
type: object
desription: 'Must be named `File` for test.'
description: 'Must be named `File` for test.'
properties:
sourceURI:
description: 'Test capitalization'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
openapi: 3.0.0
servers:
- url: 'http://petstore.swagger.io:80/v2'
info:
description: >-
This spec is mainly for testing Petstore server and contains fake endpoints,
Expand Down Expand Up @@ -573,7 +571,6 @@ paths:
parameters:
- name: enum_header_string_array
in: header
explode: true
description: Header parameter enum test (string array)
schema:
type: array
Expand Down Expand Up @@ -964,12 +961,14 @@ paths:
additionalMetadata:
description: Additional data to pass to server
type: string
file:
requiredFile:
description: file to upload
type: string
format: binary
required:
- file
- requiredFile
servers:
- url: 'http://petstore.swagger.io:80/v2'
components:
requestBodies:
UserArray:
Expand Down
5 changes: 3 additions & 2 deletions samples/client/petstore/go/go-petstore/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1008,12 +1008,12 @@ paths:
additionalMetadata:
description: Additional data to pass to server
type: string
file:
requiredFile:
description: file to upload
format: binary
type: string
required:
- file
- requiredFile
required: true
responses:
200:
Expand Down Expand Up @@ -1507,6 +1507,7 @@ components:
$ref: '#/components/schemas/Animal'
type: array
File:
description: Must be named `File` for test.
example:
sourceURI: sourceURI
properties:
Expand Down
9 changes: 5 additions & 4 deletions samples/client/petstore/go/go-petstore/api_another_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ To test special tags
func (a *AnotherFakeApiService) TestSpecialTags(ctx context.Context, client Client) (Client, *http.Response, error) {
var (
localVarHttpMethod = strings.ToUpper("Patch")
localVarPostBody interface{}
localVarFileName string
localVarFileBytes []byte
localVarPostBody interface{}
localVarFormFileName string
localVarFileName string
localVarFileBytes []byte
localVarReturnValue Client
)

Expand Down Expand Up @@ -66,7 +67,7 @@ func (a *AnotherFakeApiService) TestSpecialTags(ctx context.Context, client Clie
}
// body params
localVarPostBody = &client
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFileName, localVarFileName, localVarFileBytes)
if err != nil {
return localVarReturnValue, nil, err
}
Expand Down
Loading

0 comments on commit 29ce03a

Please sign in to comment.