Skip to content

Commit

Permalink
Merge pull request #38 from andnow873/#37
Browse files Browse the repository at this point in the history
fix #37
  • Loading branch information
mromaszewicz authored Jun 4, 2024
2 parents 1f1f116 + 1b61163 commit 9fd2a77
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
15 changes: 6 additions & 9 deletions styleparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
32 changes: 31 additions & 1 deletion styleparam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
})
}
}
}

0 comments on commit 9fd2a77

Please sign in to comment.