Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement required:false for parameter of type File #1154

Merged
merged 4 commits into from Sep 1, 2017

Conversation

khyew
Copy link
Contributor

@khyew khyew commented Aug 30, 2017

See #1151

The Swagger/OpenAPI spec indicates that required: false can be used to make any API parameters optional. The current implementation of go-swagger completely ignores this when generating the BindRequest for parameters of type file. The result is that all file parameters are mandatory and clients that omit any results in a HTTP 500 returned by the middleware layer.

This pull request makes a change to the parameter.gotmpl template to handle nullable file parameters.

Here's the generated output for required: true (current behaviour):

type PostV1FeedbackParams struct {
…
        Attachment runtime.File
…
}

func (o *PostV1FeedbackParams) BindRequest(r *http.Request, route *middleware.MatchedRoute) error {
...
        attachment, attachmentHeader, err := r.FormFile("attachment")
        if err != nil {
                res = append(res, errors.New(400, "reading file %q failed: %v", "attachment", err))
        } else {
                o.Attachment = runtime.File{Data: attachment, Header: attachmentHeader}
        }
...
}

Here's the generated output for required: false with this new template:

type PostV1FeedbackParams struct {
…
        Attachment *runtime.File
…
}

func (o *PostV1FeedbackParams) BindRequest(r *http.Request, route *middleware.MatchedRoute) error {
...
        attachment, attachmentHeader, err := r.FormFile("attachment")
        if err != nil && err != http.ErrMissingFile {
                res = append(res, errors.New(400, "reading file %q failed: %v", "attachment", err))
        } else if err == http.ErrMissingFile {
                // no-op for missing but optional file parameter
        } else {
                o.Attachment = &runtime.File{Data: attachment, Header: attachmentHeader}
        }
...
}

@codecov
Copy link

codecov bot commented Aug 30, 2017

Codecov Report

Merging #1154 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1154   +/-   ##
=======================================
  Coverage   69.27%   69.27%           
=======================================
  Files          21       21           
  Lines        7391     7391           
=======================================
  Hits         5120     5120           
  Misses       1801     1801           
  Partials      470      470
Impacted Files Coverage Δ
generator/bindata.go 60.32% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 098db8b...d776a12. Read the comment docs.

@casualjim casualjim merged commit e2bfe42 into go-swagger:master Sep 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants