Skip to content

Commit

Permalink
openapi3filter: ensure key matches param name before decoding in `(*u…
Browse files Browse the repository at this point in the history
…rlValuesDecoder) DecodeObject(..)` (#947)

Co-authored-by: Mateus Franchini de Freitas <[email protected]>
  • Loading branch information
MateusFrFreitas and Mateus Franchini de Freitas authored May 11, 2024
1 parent b9f83d9 commit 2ab0c0e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions openapi3filter/req_resp_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,10 @@ func (d *urlValuesDecoder) DecodeObject(param string, sm *openapi3.Serialization
propsFn = func(params url.Values) (map[string]string, error) {
props := make(map[string]string)
for key, values := range params {
if !regexp.MustCompile(fmt.Sprintf(`^%s\[`, regexp.QuoteMeta(param))).MatchString(key) {
continue
}

matches := regexp.MustCompile(`\[(.*?)\]`).FindAllStringSubmatch(key, -1)
switch l := len(matches); {
case l == 0:
Expand Down
11 changes: 11 additions & 0 deletions openapi3filter/req_resp_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,17 @@ func TestDecodeParameter(t *testing.T) {
query: "anotherparam=bar",
want: map[string]interface{}(nil),
},
{
name: "deepObject explode nested object - extraneous deep object param ignored",
param: &openapi3.Parameter{
Name: "param", In: "query", Style: "deepObject", Explode: explode,
Schema: objectOf(
"obj", objectOf("nestedObjOne", stringSchema, "nestedObjTwo", stringSchema),
),
},
query: "anotherparam[obj][nestedObjOne]=one&anotherparam[obj][nestedObjTwo]=two",
want: map[string]interface{}(nil),
},
{
name: "deepObject explode nested object - bad array item type",
param: &openapi3.Parameter{
Expand Down

0 comments on commit 2ab0c0e

Please sign in to comment.