diff --git a/go.mod b/go.mod index f84f470c1..9da0ae552 100644 --- a/go.mod +++ b/go.mod @@ -7,5 +7,5 @@ require ( github.com/go-openapi/jsonpointer v0.19.5 github.com/gorilla/mux v1.8.0 github.com/stretchr/testify v1.5.1 - gopkg.in/yaml.v2 v2.3.0 // indirect + gopkg.in/yaml.v2 v2.3.0 ) diff --git a/openapi3filter/req_resp_decoder.go b/openapi3filter/req_resp_decoder.go index 2d2728b24..2dc392864 100644 --- a/openapi3filter/req_resp_decoder.go +++ b/openapi3filter/req_resp_decoder.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "gopkg.in/yaml.v2" "io" "io/ioutil" "mime" @@ -832,6 +833,8 @@ func decodeBody(body io.Reader, header http.Header, schema *openapi3.SchemaRef, func init() { RegisterBodyDecoder("text/plain", plainBodyDecoder) RegisterBodyDecoder("application/json", jsonBodyDecoder) + RegisterBodyDecoder("application/x-yaml", yamlBodyDecoder) + RegisterBodyDecoder("application/yaml", yamlBodyDecoder) RegisterBodyDecoder("application/problem+json", jsonBodyDecoder) RegisterBodyDecoder("application/x-www-form-urlencoded", urlencodedBodyDecoder) RegisterBodyDecoder("multipart/form-data", multipartBodyDecoder) @@ -854,6 +857,14 @@ func jsonBodyDecoder(body io.Reader, header http.Header, schema *openapi3.Schema return value, nil } +func yamlBodyDecoder(body io.Reader, header http.Header, schema *openapi3.SchemaRef, encFn EncodingFn) (interface{}, error) { + var value interface{} + if err := yaml.NewDecoder(body).Decode(&value); err != nil { + return nil, &ParseError{Kind: KindInvalidFormat, Cause: err} + } + return value, nil +} + func urlencodedBodyDecoder(body io.Reader, header http.Header, schema *openapi3.SchemaRef, encFn EncodingFn) (interface{}, error) { // Validate schema of request body. // By the OpenAPI 3 specification request body's schema must have type "object". diff --git a/openapi3filter/req_resp_decoder_test.go b/openapi3filter/req_resp_decoder_test.go index 7ae863b82..34e63712d 100644 --- a/openapi3filter/req_resp_decoder_test.go +++ b/openapi3filter/req_resp_decoder_test.go @@ -1035,6 +1035,18 @@ func TestDecodeBody(t *testing.T) { body: strings.NewReader("\"foo\""), want: "foo", }, + { + name: "x-yaml", + mime: "application/x-yaml", + body: strings.NewReader("foo"), + want: "foo", + }, + { + name: "yaml", + mime: "application/yaml", + body: strings.NewReader("foo"), + want: "foo", + }, { name: "urlencoded form", mime: "application/x-www-form-urlencoded",