Skip to content

Commit d4b9383

Browse files
authored
fix: parse un-exploded query param to map (#101)
This fixes an issue where query parameters that are objects with additional properties cannot be parsed into their values. For example, a query parameter like `tags=foo,bar,bim,baz` should end up becoming: ```go map[string]string{ "foo": "bar", "bim": "baz", } ``` This adheres to the [spec](https://swagger.io/docs/specification/v3_0/serialization/#query-parameters) for paramters of style=form and explode=false. Fixes: oapi-codegen/oapi-codegen#698 Signed-off-by: Alex Dulin <alex@morningconsult.com>
1 parent bfe9ff9 commit d4b9383

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

bindparam.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ func BindQueryParameterWithOptions(style string, explode bool, required bool, pa
508508
} else {
509509
err = bindSplitPartsToDestinationArray(parts, output)
510510
}
511-
case reflect.Struct:
511+
case reflect.Struct, reflect.Map:
512512
// Some struct types (e.g. types.Date, time.Time) are scalar values
513513
// that should be bound from a single string, not decomposed as
514514
// key-value objects. Detect these via the Binder and

bindparam_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,21 @@ func TestBindQueryParameter(t *testing.T) {
542542
assert.Equal(t, types.Date{Time: time.Date(2023, 6, 15, 0, 0, 0, 0, time.UTC)}, *params.StartDate)
543543
})
544544

545+
t.Run("map_form_no_explode_optional", func(t *testing.T) {
546+
want := map[string]string{
547+
"foo": "bar",
548+
"bim": "baz",
549+
}
550+
queryParams := url.Values{
551+
"tags": {"foo,bar,bim,baz"},
552+
}
553+
var got map[string]string
554+
err := BindQueryParameter("form", false, false, "tags", queryParams, &got)
555+
assert.NoError(t, err)
556+
require.NotNil(t, got)
557+
assert.Equal(t, want, got)
558+
})
559+
545560
t.Run("optional", func(t *testing.T) {
546561
queryParams := url.Values{
547562
"time": {"2020-12-09T16:09:53+00:00"},

0 commit comments

Comments
 (0)