Skip to content
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
40 changes: 36 additions & 4 deletions openapi3filter/req_resp_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,10 @@ func UnregisterBodyDecoder(contentType string) {

var headerCT = http.CanonicalHeaderKey("Content-Type")

const prefixUnsupportedCT = "unsupported content type"
const (
prefixUnsupportedCT = "unsupported content type"
prefixNotMatchingCT = "not matching content types"
)

func isBinary(schema *openapi3.SchemaRef) bool {
if schema == nil || schema.Value == nil {
Expand All @@ -1220,6 +1223,20 @@ func isBinary(schema *openapi3.SchemaRef) bool {
return schema.Value.Type.Is("string") && schema.Value.Format == "binary"
}

func getEncodingContentType(encFn EncodingFn) string {
var enc *openapi3.Encoding
if encFn != nil {
// encFn is passed to decodeBody only in form body decoders as a subEncFn, so key can be ""
// func(string) *openapi3.Encoding { return enc }
enc = encFn("")
}
if enc == nil {
return ""
}

return enc.ContentType
}

// decodeBody returns a decoded body.
// The function returns ParseError when a body is invalid.
func decodeBody(body io.Reader, header http.Header, schema *openapi3.SchemaRef, encFn EncodingFn) (
Expand All @@ -1235,11 +1252,26 @@ func decodeBody(body io.Reader, header http.Header, schema *openapi3.SchemaRef,
}

mediaType := parseMediaType(contentType)
decoder, ok := bodyDecoders[mediaType]
if !ok && isBinary(schema) {
ok, decoder = true, FileBodyDecoder
encodingContentType := getEncodingContentType(encFn)
if isBinary(schema) && encodingContentType == "" {
value, err := FileBodyDecoder(body, header, schema, encFn)
return mediaType, value, err
}

if encodingContentType != "" &&
mediaType != encodingContentType {
return "", nil, &ParseError{
Kind: KindOther,
Reason: fmt.Sprintf(
"%s: header %q, encoding %q",
prefixNotMatchingCT,
mediaType,
encodingContentType,
),
}
}

decoder, ok := bodyDecoders[mediaType]
if !ok {
return "", nil, &ParseError{
Kind: KindUnsupportedFormat,
Expand Down
76 changes: 76 additions & 0 deletions openapi3filter/req_resp_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,23 @@ func TestDecodeBody(t *testing.T) {
})
require.NoError(t, err)

multipartBinaryEncodingCT, multipartMimeBinaryEncodingCT, err := newTestMultipartForm([]*testFormPart{
{name: "b", contentType: "application/json", data: strings.NewReader(`{"bar1": "bar1"}`), filename: "b1"},
{name: "d", contentType: "application/pdf", data: strings.NewReader("doo1"), filename: "d1"},
{name: "f", contentType: "application/json", data: strings.NewReader(`{"foo1": "foo1"}`), filename: "f1"},
{name: "f", contentType: "application/pdf", data: strings.NewReader("foo2"), filename: "f2"},
})

multipartBinaryEncodingCTUnsupported, multipartMimeBinaryEncodingCTUnsupported, err := newTestMultipartForm([]*testFormPart{
{name: "b", contentType: "application/json", data: strings.NewReader(`{"bar1": "bar1"}`), filename: "b1"},
{name: "d", contentType: "application/pdf", data: strings.NewReader("doo1"), filename: "d1"},
})

multipartBinaryEncodingCTNotMatching, multipartMimeBinaryEncodingCTNotMatching, err := newTestMultipartForm([]*testFormPart{
{name: "b", contentType: "application/json", data: strings.NewReader(`{"bar1": "bar1"}`), filename: "b1"},
{name: "d", contentType: "application/pdf", data: strings.NewReader("doo1"), filename: "d1"},
})

testCases := []struct {
name string
mime string
Expand Down Expand Up @@ -1787,6 +1804,65 @@ func TestDecodeBody(t *testing.T) {
body: strings.NewReader("foo"),
want: "foo",
},
{
name: "multipartEncodingCT",
mime: multipartMimeBinaryEncodingCT,
body: multipartBinaryEncodingCT,
schema: openapi3.NewObjectSchema().
WithProperty("b", openapi3.NewStringSchema().WithFormat("binary")).
WithProperty("d", openapi3.NewStringSchema().WithFormat("binary")).
WithProperty("f", openapi3.NewArraySchema().WithItems(
openapi3.NewStringSchema().WithFormat("binary"),
)),
want: map[string]any{"b": `{"bar1": "bar1"}`, "d": "doo1", "f": []any{`{"foo1": "foo1"}`, "foo2"}},
},
{
name: "multipartEncodingCTUnsupported",
mime: multipartMimeBinaryEncodingCTUnsupported,
body: multipartBinaryEncodingCTUnsupported,
schema: openapi3.NewObjectSchema().
WithProperty("b", openapi3.NewStringSchema().WithFormat("binary")).
WithProperty("d", openapi3.NewStringSchema().WithFormat("binary")),
encoding: map[string]*openapi3.Encoding{
"b": {ContentType: "application/json"},
"d": {ContentType: "application/pdf"},
},
want: map[string]any{"b": map[string]any{"bar1": "bar1"}},
wantErr: &ParseError{
Kind: KindOther,
Cause: &ParseError{
Kind: KindUnsupportedFormat,
Reason: fmt.Sprintf("%s %q", prefixUnsupportedCT, "application/pdf"),
},
path: []any{"d"},
},
},
{
name: "multipartEncodingCTNotMatching",
mime: multipartMimeBinaryEncodingCTNotMatching,
body: multipartBinaryEncodingCTNotMatching,
schema: openapi3.NewObjectSchema().
WithProperty("b", openapi3.NewStringSchema().WithFormat("binary")).
WithProperty("d", openapi3.NewStringSchema().WithFormat("binary")),
encoding: map[string]*openapi3.Encoding{
"b": {ContentType: "application/json"},
"d": {ContentType: "application/test"},
},
want: map[string]any{"b": map[string]any{"bar1": "bar1"}},
wantErr: &ParseError{
Kind: KindOther,
Cause: &ParseError{
Kind: KindOther,
Reason: fmt.Sprintf(
"%s: header %q, encoding %q",
prefixNotMatchingCT,
"application/pdf",
"application/test",
),
},
path: []any{"d"},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions openapi3filter/zip_file_upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ paths:
file:
type: string
format: binary
encoding:
file:
contentType: application/zip
responses:
'200':
description: Created
Expand Down
Loading