diff --git a/styleparam.go b/styleparam.go index 57ed41b..3d2946f 100644 --- a/styleparam.go +++ b/styleparam.go @@ -26,8 +26,9 @@ import ( "strings" "time" - "github.com/oapi-codegen/runtime/types" "github.com/google/uuid" + + "github.com/oapi-codegen/runtime/types" ) // 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 } return MarshalDeepObject(value, paramName) } - - dict, ok := value.(map[string]interface{}) - if !ok { - return "", errors.New("map not of type map[string]interface{}") - } + v := reflect.ValueOf(value) fieldDict := make(map[string]string) - for fieldName, value := range dict { - str, err := primitiveToString(value) + for _, fieldName := range v.MapKeys() { + str, err := primitiveToString(v.MapIndex(fieldName).Interface()) if err != nil { return "", fmt.Errorf("error formatting '%s': %s", paramName, err) } - fieldDict[fieldName] = str + fieldDict[fieldName.String()] = str } return processFieldDict(style, explode, paramName, paramLocation, fieldDict) } diff --git a/styleparam_test.go b/styleparam_test.go index 18aa745..b980140 100644 --- a/styleparam_test.go +++ b/styleparam_test.go @@ -14,11 +14,12 @@ package runtime import ( + "fmt" "testing" "time" - "github.com/oapi-codegen/runtime/types" "github.com/google/uuid" + "github.com/oapi-codegen/runtime/types" "github.com/stretchr/testify/assert" ) @@ -690,3 +691,32 @@ func TestStyleParam(t *testing.T) { assert.EqualValues(t, "972beb41-e5ea-4b31-a79a-96f4999d8769", result) } + +// Issue 37 - https://github.com/oapi-codegen/runtime/issues/37 +func TestIssue37(t *testing.T) { + styles := []string{ + "simple", + "spaceDelimited", + "pipeDelimited", + "deepObject", + "form", + "matrix", + "label", + } + values := []struct { + name string + value interface{} + }{ + {"int", map[string]int{"k1": 1, "k2": 2, "k3": 3}}, + {"string", map[string]string{"k1": "v1", "k2": "v2", "k3": "v3"}}, + {"any", map[string]any{"k1": "v1", "k2": 2, "k3": 3.5}}, + } + for _, style := range styles { + for _, value := range values { + t.Run(fmt.Sprintf("%s %s", value.name, style), func(t *testing.T) { + _, err := StyleParamWithLocation("form", true, "priority", ParamLocationQuery, value.value) + assert.NoError(t, err) + }) + } + } +}