Skip to content

Commit f16058d

Browse files
authored
Accept multipart/form-data's part without Content-Type (#399)
1 parent b35b144 commit f16058d

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

openapi3filter/req_resp_decoder.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,11 @@ const prefixUnsupportedCT = "unsupported content type"
809809
// The function returns ParseError when a body is invalid.
810810
func decodeBody(body io.Reader, header http.Header, schema *openapi3.SchemaRef, encFn EncodingFn) (interface{}, error) {
811811
contentType := header.Get(headerCT)
812+
if contentType == "" {
813+
if _, ok := body.(*multipart.Part); ok {
814+
contentType = "text/plain"
815+
}
816+
}
812817
mediaType := parseMediaType(contentType)
813818
decoder, ok := bodyDecoders[mediaType]
814819
if !ok {

openapi3filter/req_resp_decoder_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,7 @@ func TestDecodeBody(t *testing.T) {
974974
{name: "c", contentType: "text/plain", data: strings.NewReader("c2")},
975975
{name: "d", contentType: "application/json", data: bytes.NewReader(d)},
976976
{name: "f", contentType: "application/octet-stream", data: strings.NewReader("foo"), filename: "f1"},
977+
{name: "g", data: strings.NewReader("g1")},
977978
})
978979
require.NoError(t, err)
979980

@@ -987,10 +988,14 @@ func TestDecodeBody(t *testing.T) {
987988
{name: "a", contentType: "text/plain", data: strings.NewReader("a1")},
988989
{name: "x", contentType: "text/plain", data: strings.NewReader("x1")},
989990
})
991+
require.NoError(t, err)
992+
990993
multipartAdditionalProps, multipartMimeAdditionalProps, err := newTestMultipartForm([]*testFormPart{
991994
{name: "a", contentType: "text/plain", data: strings.NewReader("a1")},
992995
{name: "x", contentType: "text/plain", data: strings.NewReader("x1")},
993996
})
997+
require.NoError(t, err)
998+
994999
multipartAdditionalPropsErr, multipartMimeAdditionalPropsErr, err := newTestMultipartForm([]*testFormPart{
9951000
{name: "a", contentType: "text/plain", data: strings.NewReader("a1")},
9961001
{name: "x", contentType: "text/plain", data: strings.NewReader("x1")},
@@ -1075,8 +1080,9 @@ func TestDecodeBody(t *testing.T) {
10751080
WithProperty("b", openapi3.NewIntegerSchema()).
10761081
WithProperty("c", openapi3.NewArraySchema().WithItems(openapi3.NewStringSchema())).
10771082
WithProperty("d", openapi3.NewObjectSchema().WithProperty("d1", openapi3.NewStringSchema())).
1078-
WithProperty("f", openapi3.NewStringSchema().WithFormat("binary")),
1079-
want: map[string]interface{}{"a": "a1", "b": float64(10), "c": []interface{}{"c1", "c2"}, "d": map[string]interface{}{"d1": "d1"}, "f": "foo"},
1083+
WithProperty("f", openapi3.NewStringSchema().WithFormat("binary")).
1084+
WithProperty("g", openapi3.NewStringSchema()),
1085+
want: map[string]interface{}{"a": "a1", "b": float64(10), "c": []interface{}{"c1", "c2"}, "d": map[string]interface{}{"d1": "d1"}, "f": "foo", "g": "g1"},
10801086
},
10811087
{
10821088
name: "multipartExtraPart",

0 commit comments

Comments
 (0)