Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce openapi3filter.RegisteredBodyDecoder #340

Merged
merged 1 commit into from
Apr 12, 2021
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
10 changes: 10 additions & 0 deletions openapi3filter/req_resp_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,10 +767,19 @@ type BodyDecoder func(io.Reader, http.Header, *openapi3.SchemaRef, EncodingFn) (
// By default, there is content type "application/json" is supported only.
var bodyDecoders = make(map[string]BodyDecoder)

// RegisteredBodyDecoder returns the registered body decoder for the given content type.
//
// If no decoder was registered for the given content type, nil is returned.
// This call is not thread-safe: body decoders should not be created/destroyed by multiple goroutines.
func RegisteredBodyDecoder(contentType string) BodyDecoder {
return bodyDecoders[contentType]
}

// RegisterBodyDecoder registers a request body's decoder for a content type.
//
// If a decoder for the specified content type already exists, the function replaces
// it with the specified decoder.
// This call is not thread-safe: body decoders should not be created/destroyed by multiple goroutines.
func RegisterBodyDecoder(contentType string, decoder BodyDecoder) {
if contentType == "" {
panic("contentType is empty")
Expand All @@ -784,6 +793,7 @@ func RegisterBodyDecoder(contentType string, decoder BodyDecoder) {
// UnregisterBodyDecoder dissociates a body decoder from a content type.
//
// Decoding this content type will result in an error.
// This call is not thread-safe: body decoders should not be created/destroyed by multiple goroutines.
func UnregisterBodyDecoder(contentType string) {
if contentType == "" {
panic("contentType is empty")
Expand Down
47 changes: 25 additions & 22 deletions openapi3filter/req_resp_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1187,39 +1187,42 @@ func newTestMultipartForm(parts []*testFormPart) (io.Reader, string, error) {
}

func TestRegisterAndUnregisterBodyDecoder(t *testing.T) {
var (
contentType = "text/csv"
decoder = func(body io.Reader, h http.Header, schema *openapi3.SchemaRef, encFn EncodingFn) (interface{}, error) {
data, err := ioutil.ReadAll(body)
if err != nil {
return nil, err
}
var vv []interface{}
for _, v := range strings.Split(string(data), ",") {
vv = append(vv, v)
}
return vv, nil
var decoder BodyDecoder
decoder = func(body io.Reader, h http.Header, schema *openapi3.SchemaRef, encFn EncodingFn) (decoded interface{}, err error) {
var data []byte
if data, err = ioutil.ReadAll(body); err != nil {
return
}
schema = openapi3.NewArraySchema().WithItems(openapi3.NewStringSchema()).NewRef()
encFn = func(string) *openapi3.Encoding { return nil }
body = strings.NewReader("foo,bar")
want = []interface{}{"foo", "bar"}
wantErr = &ParseError{Kind: KindUnsupportedFormat}
)
return strings.Split(string(data), ","), nil
}
contentType := "text/csv"
h := make(http.Header)
h.Set(headerCT, contentType)

originalDecoder := RegisteredBodyDecoder(contentType)
require.Nil(t, originalDecoder)

RegisterBodyDecoder(contentType, decoder)
require.Equal(t, fmt.Sprintf("%v", decoder), fmt.Sprintf("%v", RegisteredBodyDecoder(contentType)))

body := strings.NewReader("foo,bar")
schema := openapi3.NewArraySchema().WithItems(openapi3.NewStringSchema()).NewRef()
encFn := func(string) *openapi3.Encoding { return nil }
got, err := decodeBody(body, h, schema, encFn)

require.NoError(t, err)
require.Truef(t, reflect.DeepEqual(got, want), "got %v, want %v", got, want)
require.Equal(t, []string{"foo", "bar"}, got)

UnregisterBodyDecoder(contentType)
_, err = decodeBody(body, h, schema, encFn)

require.Error(t, err)
require.Truef(t, matchParseError(err, wantErr), "got error:\n%v\nwant error:\n%v", err, wantErr)
originalDecoder = RegisteredBodyDecoder(contentType)
require.Nil(t, originalDecoder)

_, err = decodeBody(body, h, schema, encFn)
require.Equal(t, &ParseError{
Kind: KindUnsupportedFormat,
Reason: prefixUnsupportedCT + ` "text/csv"`,
}, err)
}

func matchParseError(got, want error) bool {
Expand Down