Skip to content

Commit 9fd2a77

Browse files
authored
Merge pull request #38 from andnow873/#37
fix #37
2 parents 1f1f116 + 1b61163 commit 9fd2a77

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

styleparam.go

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

29-
"github.com/oapi-codegen/runtime/types"
3029
"github.com/google/uuid"
30+
31+
"github.com/oapi-codegen/runtime/types"
3132
)
3233

3334
// Parameter escaping works differently based on where a header is found
@@ -288,19 +289,15 @@ func styleMap(style string, explode bool, paramName string, paramLocation ParamL
288289
}
289290
return MarshalDeepObject(value, paramName)
290291
}
291-
292-
dict, ok := value.(map[string]interface{})
293-
if !ok {
294-
return "", errors.New("map not of type map[string]interface{}")
295-
}
292+
v := reflect.ValueOf(value)
296293

297294
fieldDict := make(map[string]string)
298-
for fieldName, value := range dict {
299-
str, err := primitiveToString(value)
295+
for _, fieldName := range v.MapKeys() {
296+
str, err := primitiveToString(v.MapIndex(fieldName).Interface())
300297
if err != nil {
301298
return "", fmt.Errorf("error formatting '%s': %s", paramName, err)
302299
}
303-
fieldDict[fieldName] = str
300+
fieldDict[fieldName.String()] = str
304301
}
305302
return processFieldDict(style, explode, paramName, paramLocation, fieldDict)
306303
}

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)