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

Fix handling of optional uploads #160

Merged
merged 2 commits into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions _examples/advanced-generic/file_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func fileUploader() usecase.Interactor {
u := usecase.NewInteractor(func(ctx context.Context, in upload, out *info) (err error) {
out.Query = in.Query
out.Simple = in.Simple
if in.Upload1 == nil {
return nil
}

out.Filename = in.Upload1.Filename
out.Header = in.Upload1.Header
out.Size = in.Upload1.Size
Expand Down
4 changes: 2 additions & 2 deletions _examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ replace github.com/swaggest/rest => ../
require (
github.com/bool64/ctxd v1.2.1
github.com/bool64/dev v0.2.27
github.com/bool64/httpmock v0.1.10
github.com/bool64/httpmock v0.1.13
github.com/bool64/httptestbench v0.1.4
github.com/go-chi/chi/v5 v5.0.8
github.com/kelseyhightower/envconfig v1.4.0
github.com/rs/cors v1.9.0
github.com/stretchr/testify v1.8.2
github.com/swaggest/assertjson v1.8.0
github.com/swaggest/assertjson v1.8.1
github.com/swaggest/jsonschema-go v0.3.51
github.com/swaggest/openapi-go v0.2.30
github.com/swaggest/swgui v1.6.2
Expand Down
8 changes: 4 additions & 4 deletions _examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ github.com/bool64/dev v0.2.5/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zR
github.com/bool64/dev v0.2.25/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
github.com/bool64/dev v0.2.27 h1:mFT+B74mFVgUeUmm/EbfM6ELPA55lEXBjQ/AOHCwCOc=
github.com/bool64/dev v0.2.27/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
github.com/bool64/httpmock v0.1.10 h1:cP+JS+nuIe47uwSheiKtoesziyacxbvWfOVo1zczbek=
github.com/bool64/httpmock v0.1.10/go.mod h1:vodDyrRL4hfWZ6/nvahVzSV4VSK6MSPf0MxyHNQXzuA=
github.com/bool64/httpmock v0.1.13 h1:3QpRXQ5kwHLW8xnVT8+Ug7VS6RerhdEFV+RWYC61aVo=
github.com/bool64/httpmock v0.1.13/go.mod h1:YMTLaypQ3o5DAx78eA/kDRSLec0f+42sLMDmHdmeY+E=
github.com/bool64/httptestbench v0.1.4 h1:35f9RwWqcnQRXM+sA+GUhWVGSa6XEFlKpNSH9oYzOjI=
github.com/bool64/httptestbench v0.1.4/go.mod h1:b9ItXGtscs9AcR5oUDnWCTLosQ/DCjiG4euDbHN8T40=
github.com/bool64/shared v0.1.5 h1:fp3eUhBsrSjNCQPcSdQqZxxh9bBwrYiZ+zOKFkM0/2E=
Expand Down Expand Up @@ -55,8 +55,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/swaggest/assertjson v1.8.0 h1:XSg4p6iOZMjtpV2tW2SXfD1GsOOTsWcm+sOADODu/DU=
github.com/swaggest/assertjson v1.8.0/go.mod h1:/8kNRmDZAZfavS5VeWYtCimLGebn0Ak1/iErFUi+DEM=
github.com/swaggest/assertjson v1.8.1 h1:Be2EHY9S2qwKWV+xWZB747Cd7Y79YK6JLdeyrgFvyMo=
github.com/swaggest/assertjson v1.8.1/go.mod h1:/8kNRmDZAZfavS5VeWYtCimLGebn0Ak1/iErFUi+DEM=
github.com/swaggest/form/v5 v5.0.4 h1:hTg+gjUEZG0vgsDdXOPi7I0Xu5OowXKoLZIGYZsoddg=
github.com/swaggest/form/v5 v5.0.4/go.mod h1:X1hraaoONee20PMnGNLQpO32f9zbQ0Czfm7iZThuEKg=
github.com/swaggest/jsonschema-go v0.3.51 h1:Cl0hFQ/jtBIP8NlHNuwW6ka3J7zzW5r2jxbLSCUByGY=
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ go 1.17

require (
github.com/bool64/dev v0.2.27
github.com/bool64/httpmock v0.1.10
github.com/bool64/httpmock v0.1.13
github.com/bool64/shared v0.1.5
github.com/cespare/xxhash/v2 v2.2.0
github.com/go-chi/chi/v5 v5.0.8
github.com/santhosh-tekuri/jsonschema/v3 v3.1.0
github.com/stretchr/testify v1.8.2
github.com/swaggest/assertjson v1.8.0
github.com/swaggest/assertjson v1.8.1
github.com/swaggest/form/v5 v5.0.4
github.com/swaggest/jsonschema-go v0.3.51
github.com/swaggest/openapi-go v0.2.30
Expand Down
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/bool64/dev v0.2.24/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8
github.com/bool64/dev v0.2.25/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
github.com/bool64/dev v0.2.27 h1:mFT+B74mFVgUeUmm/EbfM6ELPA55lEXBjQ/AOHCwCOc=
github.com/bool64/dev v0.2.27/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
github.com/bool64/httpmock v0.1.10 h1:cP+JS+nuIe47uwSheiKtoesziyacxbvWfOVo1zczbek=
github.com/bool64/httpmock v0.1.10/go.mod h1:vodDyrRL4hfWZ6/nvahVzSV4VSK6MSPf0MxyHNQXzuA=
github.com/bool64/httpmock v0.1.13 h1:3QpRXQ5kwHLW8xnVT8+Ug7VS6RerhdEFV+RWYC61aVo=
github.com/bool64/httpmock v0.1.13/go.mod h1:YMTLaypQ3o5DAx78eA/kDRSLec0f+42sLMDmHdmeY+E=
github.com/bool64/shared v0.1.5 h1:fp3eUhBsrSjNCQPcSdQqZxxh9bBwrYiZ+zOKFkM0/2E=
github.com/bool64/shared v0.1.5/go.mod h1:081yz68YC9jeFB3+Bbmno2RFWvGKv1lPKkMP6MHJlPs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
Expand Down Expand Up @@ -68,8 +68,9 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/swaggest/assertjson v1.8.0 h1:XSg4p6iOZMjtpV2tW2SXfD1GsOOTsWcm+sOADODu/DU=
github.com/swaggest/assertjson v1.8.0/go.mod h1:/8kNRmDZAZfavS5VeWYtCimLGebn0Ak1/iErFUi+DEM=
github.com/swaggest/assertjson v1.8.1 h1:Be2EHY9S2qwKWV+xWZB747Cd7Y79YK6JLdeyrgFvyMo=
github.com/swaggest/assertjson v1.8.1/go.mod h1:/8kNRmDZAZfavS5VeWYtCimLGebn0Ak1/iErFUi+DEM=
github.com/swaggest/form/v5 v5.0.4 h1:hTg+gjUEZG0vgsDdXOPi7I0Xu5OowXKoLZIGYZsoddg=
github.com/swaggest/form/v5 v5.0.4/go.mod h1:X1hraaoONee20PMnGNLQpO32f9zbQ0Czfm7iZThuEKg=
github.com/swaggest/jsonschema-go v0.3.50/go.mod h1:QfUB5HaZ8y5TiFtCPhM7QwvPNKxTsYxDJaLHTLq6jgU=
Expand Down
2 changes: 2 additions & 0 deletions request/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func setFile(r *http.Request, field reflect.StructField, v reflect.Value) error
if field.Tag.Get("required") == "true" {
return fmt.Errorf("%w: %q", ErrMissingRequiredFile, name)
}

return nil
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is the fix.

}

return fmt.Errorf("failed to get file %q from request: %w", name, err)
Expand Down
29 changes: 28 additions & 1 deletion request/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import (
"github.com/swaggest/rest/openapi"
"github.com/swaggest/rest/request"
"github.com/swaggest/rest/response"
"github.com/swaggest/rest/web"
"github.com/swaggest/usecase"
)

type ReqEmb struct {
Simple string `formData:"simple"`
UploadHeader *multipart.FileHeader `formData:"upload"`
UploadsHeaders []*multipart.FileHeader `formData:"uploads"`
}
Expand All @@ -33,7 +35,32 @@ type fileReqTest struct {
Uploads []multipart.File `formData:"uploads"`
}

func TestMapper_Decode_fileUploadTag(t *testing.T) {
func TestDecoder_Decode_fileUploadOptional(t *testing.T) {
u := usecase.NewIOI(new(ReqEmb), nil, func(ctx context.Context, input, output interface{}) error {
return nil
})

s := web.DefaultService()
s.Post("/", u)

b := bytes.NewBuffer(nil)
w := multipart.NewWriter(b)
require.NoError(t, w.WriteField("simple", "def"))
require.NoError(t, w.Close())

req, err := http.NewRequest(http.MethodPost, "/", b)
require.NoError(t, err)

req.Header.Set("Content-Type", w.FormDataContentType())

rw := httptest.NewRecorder()
s.ServeHTTP(rw, req)

assert.Equal(t, http.StatusNoContent, rw.Code)
assert.Equal(t, ``, rw.Body.String())
}

func TestDecoder_Decode_fileUploadTag(t *testing.T) {
r := chirouter.NewWrapper(chi.NewRouter())
apiSchema := openapi.Collector{}
decoderFactory := request.NewDecoderFactory()
Expand Down
8 changes: 5 additions & 3 deletions response/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,13 @@ func (h *Encoder) SetupOutput(output interface{}, ht *rest.HandlerTrait) {
func (h *Encoder) successStatus(output interface{}) int {
if outputWithStatus, ok := output.(rest.OutputWithHTTPStatus); ok {
return outputWithStatus.HTTPStatus()
} else if h.skipRendering && !h.outputWithWriter {
}

if h.skipRendering && !h.outputWithWriter {
return http.StatusNoContent
} else {
return http.StatusOK
}

return http.StatusOK
}

type jsonEncoder struct {
Expand Down