From d741dd7c7a13ac2fc40677c442d9efecf017b859 Mon Sep 17 00:00:00 2001 From: anon Date: Mon, 26 Feb 2024 09:36:26 -0300 Subject: [PATCH 1/2] fix #37 --- styleparam.go | 9 +++++++-- styleparam_test.go | 32 +++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/styleparam.go b/styleparam.go index 57ed41b..6c0525b 100644 --- a/styleparam.go +++ b/styleparam.go @@ -26,8 +26,8 @@ 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 @@ -97,7 +97,12 @@ func StyleParamWithLocation(style string, explode bool, paramName string, paramL case reflect.Struct: return styleStruct(style, explode, paramName, paramLocation, value) case reflect.Map: - return styleMap(style, explode, paramName, paramLocation, value) + dict := make(map[string]any, v.Len()) + for _, key := range v.MapKeys() { + // the key is guaranteed to be a string + dict[key.String()] = v.MapIndex(key).Interface() + } + return styleMap(style, explode, paramName, paramLocation, dict) default: return stylePrimitive(style, explode, paramName, paramLocation, value) } 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) + }) + } + } +} From 1b6116371730db55b7979177a1d8bbb335cc823a Mon Sep 17 00:00:00 2001 From: mromaszewicz Date: Tue, 4 Jun 2024 08:10:41 +0200 Subject: [PATCH 2/2] Change styleMap to iterate via reflection --- styleparam.go | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/styleparam.go b/styleparam.go index 6c0525b..3d2946f 100644 --- a/styleparam.go +++ b/styleparam.go @@ -27,6 +27,7 @@ import ( "time" "github.com/google/uuid" + "github.com/oapi-codegen/runtime/types" ) @@ -97,12 +98,7 @@ func StyleParamWithLocation(style string, explode bool, paramName string, paramL case reflect.Struct: return styleStruct(style, explode, paramName, paramLocation, value) case reflect.Map: - dict := make(map[string]any, v.Len()) - for _, key := range v.MapKeys() { - // the key is guaranteed to be a string - dict[key.String()] = v.MapIndex(key).Interface() - } - return styleMap(style, explode, paramName, paramLocation, dict) + return styleMap(style, explode, paramName, paramLocation, value) default: return stylePrimitive(style, explode, paramName, paramLocation, value) } @@ -293,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) }