From d4586c7221564768f32cc275a531bd1ae7d75948 Mon Sep 17 00:00:00 2001 From: hackerman <3372410+aeneasr@users.noreply.github.com> Date: Tue, 6 Apr 2021 13:17:53 +0200 Subject: [PATCH] feat(decoderx): support query params in json payloads as well (#320) --- decoderx/http.go | 7 +++++++ decoderx/http_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/decoderx/http.go b/decoderx/http.go index 8217f654..49f22941 100644 --- a/decoderx/http.go +++ b/decoderx/http.go @@ -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 diff --git a/decoderx/http_test.go b/decoderx/http_test.go index 2b99ebf4..a103d92b 100644 --- a/decoderx/http_test.go +++ b/decoderx/http_test.go @@ -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 }`, }, {