Skip to content

Commit d741dd7

Browse files
author
anon
committed
fix #37
1 parent 1f1f116 commit d741dd7

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

styleparam.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import (
2626
"strings"
2727
"time"
2828

29-
"github.com/oapi-codegen/runtime/types"
3029
"github.com/google/uuid"
30+
"github.com/oapi-codegen/runtime/types"
3131
)
3232

3333
// Parameter escaping works differently based on where a header is found
@@ -97,7 +97,12 @@ func StyleParamWithLocation(style string, explode bool, paramName string, paramL
9797
case reflect.Struct:
9898
return styleStruct(style, explode, paramName, paramLocation, value)
9999
case reflect.Map:
100-
return styleMap(style, explode, paramName, paramLocation, value)
100+
dict := make(map[string]any, v.Len())
101+
for _, key := range v.MapKeys() {
102+
// the key is guaranteed to be a string
103+
dict[key.String()] = v.MapIndex(key).Interface()
104+
}
105+
return styleMap(style, explode, paramName, paramLocation, dict)
101106
default:
102107
return stylePrimitive(style, explode, paramName, paramLocation, value)
103108
}

styleparam_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
package runtime
1515

1616
import (
17+
"fmt"
1718
"testing"
1819
"time"
1920

20-
"github.com/oapi-codegen/runtime/types"
2121
"github.com/google/uuid"
22+
"github.com/oapi-codegen/runtime/types"
2223
"github.com/stretchr/testify/assert"
2324
)
2425

@@ -690,3 +691,32 @@ func TestStyleParam(t *testing.T) {
690691
assert.EqualValues(t, "972beb41-e5ea-4b31-a79a-96f4999d8769", result)
691692

692693
}
694+
695+
// Issue 37 - https://github.com/oapi-codegen/runtime/issues/37
696+
func TestIssue37(t *testing.T) {
697+
styles := []string{
698+
"simple",
699+
"spaceDelimited",
700+
"pipeDelimited",
701+
"deepObject",
702+
"form",
703+
"matrix",
704+
"label",
705+
}
706+
values := []struct {
707+
name string
708+
value interface{}
709+
}{
710+
{"int", map[string]int{"k1": 1, "k2": 2, "k3": 3}},
711+
{"string", map[string]string{"k1": "v1", "k2": "v2", "k3": "v3"}},
712+
{"any", map[string]any{"k1": "v1", "k2": 2, "k3": 3.5}},
713+
}
714+
for _, style := range styles {
715+
for _, value := range values {
716+
t.Run(fmt.Sprintf("%s %s", value.name, style), func(t *testing.T) {
717+
_, err := StyleParamWithLocation("form", true, "priority", ParamLocationQuery, value.value)
718+
assert.NoError(t, err)
719+
})
720+
}
721+
}
722+
}

0 commit comments

Comments
 (0)