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

Support JSON variants #2572

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion bind.go
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/xml"
"errors"
"fmt"
"mime"
"net/http"
"reflect"
"strconv"
Expand Down Expand Up @@ -51,6 +52,14 @@ func (b *DefaultBinder) BindQueryParams(c Context, i interface{}) error {
return nil
}

func contentTypeIsJSON(mediaType string) bool {
parsed, _, err := mime.ParseMediaType(mediaType)
if err != nil {
return false
}
return parsed == "application/json" || strings.HasSuffix(parsed, "+json")
}

// BindBody binds request body contents to bindable object
// NB: then binding forms take note that this implementation uses standard library form parsing
// which parses form data from BOTH URL and BODY if content type is not MIMEMultipartForm
Expand All @@ -64,7 +73,7 @@ func (b *DefaultBinder) BindBody(c Context, i interface{}) (err error) {

ctype := req.Header.Get(HeaderContentType)
switch {
case strings.HasPrefix(ctype, MIMEApplicationJSON):
case contentTypeIsJSON(ctype):
if err = c.Echo().JSONSerializer.Deserialize(c, i); err != nil {
switch err.(type) {
case *HTTPError:
Expand Down
2 changes: 2 additions & 0 deletions bind_test.go
Expand Up @@ -192,8 +192,10 @@ func TestToMultipleFields(t *testing.T) {
func TestBindJSON(t *testing.T) {
testBindOkay(t, strings.NewReader(userJSON), nil, MIMEApplicationJSON)
testBindOkay(t, strings.NewReader(userJSON), dummyQuery, MIMEApplicationJSON)
testBindOkay(t, strings.NewReader(userJSON), dummyQuery, contentTypeJSONVariant)
testBindArrayOkay(t, strings.NewReader(usersJSON), nil, MIMEApplicationJSON)
testBindArrayOkay(t, strings.NewReader(usersJSON), dummyQuery, MIMEApplicationJSON)
testBindArrayOkay(t, strings.NewReader(usersJSON), dummyQuery, contentTypeJSONVariant)
testBindError(t, strings.NewReader(invalidContent), MIMEApplicationJSON, &json.SyntaxError{})
testBindError(t, strings.NewReader(userJSONInvalidType), MIMEApplicationJSON, &json.UnmarshalTypeError{})
}
Expand Down
1 change: 1 addition & 0 deletions echo_test.go
Expand Up @@ -38,6 +38,7 @@ const (
userJSONInvalidType = `{"id":"1","name":"Jon Snow"}`
userXMLConvertNumberError = `<user><id>Number one</id><name>Jon Snow</name></user>`
userXMLUnsupportedTypeError = `<user><>Number one</><name>Jon Snow</name></user>`
contentTypeJSONVariant = `application/external.dns.webhook+json;version=1`
)

const userJSONPretty = `{
Expand Down