Skip to content

Commit

Permalink
feat(decoderx): support query params in json payloads as well (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr authored Apr 6, 2021
1 parent eb295dc commit d4586c7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions decoderx/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,13 @@ func (t *HTTP) decodeJSONForm(r *http.Request, destination interface{}, o *httpD
return true
})

if o.queryAndBody {
_ = r.ParseForm()
for k := range r.Form {
values.Set(k, r.Form.Get(k))
}
}

raw, err := t.decodeURLValues(values, paths, o)
if err != nil {
return err
Expand Down
39 changes: 39 additions & 0 deletions decoderx/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,45 @@ func TestHTTPFormDecoder(t *testing.T) {
"newsletter": true,
"consent": false,
"ratio": 0.9
}`,
},
{
d: "should pass JSON request formatted as a form",
request: newRequest(t, "POST", "/?age=29", bytes.NewBufferString(`{
"name.first": "Aeneas",
"name.last": "Rekkas",
"ratio": 0.9,
"consent": false,
"newsletter": true
}`), httpContentTypeJSON),
options: []HTTPDecoderOption{HTTPDecoderJSONFollowsFormFormat(),
HTTPJSONSchemaCompiler("stub/person.json", nil)},
expected: `{
"name": {"first": "Aeneas", "last": "Rekkas"},
"newsletter": true,
"consent": false,
"ratio": 0.9
}`,
},
{
d: "should pass JSON request formatted as a form",
request: newRequest(t, "POST", "/?age=29", bytes.NewBufferString(`{
"name.first": "Aeneas",
"name.last": "Rekkas",
"ratio": 0.9,
"consent": false,
"newsletter": true
}`), httpContentTypeJSON),
options: []HTTPDecoderOption{
HTTPDecoderUseQueryAndBody(),
HTTPDecoderJSONFollowsFormFormat(),
HTTPJSONSchemaCompiler("stub/person.json", nil)},
expected: `{
"name": {"first": "Aeneas", "last": "Rekkas"},
"age": 29,
"newsletter": true,
"consent": false,
"ratio": 0.9
}`,
},
{
Expand Down

0 comments on commit d4586c7

Please sign in to comment.