Skip to content

Commit

Permalink
Add support for "application/x-yaml" (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
luminoso authored Sep 21, 2021
1 parent de8fc7e commit f589245
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
11 changes: 11 additions & 0 deletions openapi3filter/req_resp_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"mime"
Expand Down Expand Up @@ -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)
Expand All @@ -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".
Expand Down
12 changes: 12 additions & 0 deletions openapi3filter/req_resp_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f589245

Please sign in to comment.