Skip to content

Commit

Permalink
Fix handling of optional uploads (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored May 28, 2023
1 parent dee7f9f commit b911ccd
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 15 deletions.
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
}

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

0 comments on commit b911ccd

Please sign in to comment.