Skip to content

Commit

Permalink
fix: use 415 status for bad input content type
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgtaylor committed Nov 22, 2023
1 parent 23c78a4 commit 56a5692
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
5 changes: 4 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package huma
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
Expand All @@ -19,6 +20,8 @@ import (

var rxSchema = regexp.MustCompile(`#/components/schemas/([^"]+)`)

var ErrUnknownContentType = errors.New("unknown content type")

// Resolver runs a `Resolve` function after a request has been parsed, enabling
// you to run custom validation or other code that can modify the request and /
// or return errors.
Expand Down Expand Up @@ -229,7 +232,7 @@ func (a *api) Unmarshal(contentType string, data []byte, v any) error {
}
f, ok := a.formats[ct]
if !ok {
return fmt.Errorf("unknown content type: %s", contentType)
return fmt.Errorf("%w: %s", ErrUnknownContentType, contentType)
}
return f.Unmarshal(data, v)
}
Expand Down
5 changes: 4 additions & 1 deletion huma.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package huma
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -824,8 +825,10 @@ func Register[I, O any](api API, op Operation, handler func(context.Context, *I)
// expected struct type to call the handler.
var parsed any
if err := api.Unmarshal(ctx.Header("Content-Type"), body, &parsed); err != nil {
// TODO: handle not acceptable
errStatus = http.StatusBadRequest
if errors.Is(err, ErrUnknownContentType) {
errStatus = http.StatusUnsupportedMediaType
}
res.Errors = append(res.Errors, &ErrorDetail{
Location: "body",
Message: err.Error(),
Expand Down
25 changes: 25 additions & 0 deletions huma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,31 @@ func TestFeatures(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, resp.Code)
},
},
{
Name: "request-body-unsupported-media-type",
Register: func(t *testing.T, api huma.API) {
huma.Register(api, huma.Operation{
Method: http.MethodPut,
Path: "/body",
}, func(ctx context.Context, input *struct {
RawBody []byte
Body struct {
Name string `json:"name"`
}
}) (*struct{}, error) {
assert.Equal(t, `{"name":"foo"}`, string(input.RawBody))
assert.Equal(t, "foo", input.Body.Name)
return nil, nil
})
},
Method: http.MethodPut,
URL: "/body",
Headers: map[string]string{"Content-Type": "application/foo"},
Body: `abcd`,
Assert: func(t *testing.T, resp *httptest.ResponseRecorder) {
assert.Equal(t, http.StatusUnsupportedMediaType, resp.Code)
},
},
{
Name: "request-body-file-upload",
Register: func(t *testing.T, api huma.API) {
Expand Down

0 comments on commit 56a5692

Please sign in to comment.