Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions styleparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,14 @@ func primitiveToString(value interface{}) (string, error) {
"cannot serialize nested object of type %s: style-based parameter serialization ('style'/'schema') is only defined for primitives, arrays, and flat objects; declare the parameter with 'content: application/json' instead, or map the schema to a Go type implementing fmt.Stringer",
reflect.TypeOf(value).String())
}
if (kind == reflect.Slice || kind == reflect.Array) && !isByteSlice(t) {
// An array nested inside a styled object or array. Like
// nested objects, OpenAPI leaves this undefined, so there
// is no wire format we could produce here.
return "", fmt.Errorf(
"cannot serialize nested array of type %s: style-based parameter serialization ('style'/'schema') is only defined for primitives, arrays of primitives, and flat objects; declare the parameter with 'content: application/json' or 'style: deepObject' instead, or map the schema to a Go type implementing fmt.Stringer",
reflect.TypeOf(value).String())
}
return "", fmt.Errorf("unsupported type %s", reflect.TypeOf(value).String())
}

Expand Down
46 changes: 46 additions & 0 deletions styleparam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,3 +914,49 @@ func TestStyleParamNestedStructError(t *testing.T) {
assert.Contains(t, err.Error(), "cannot serialize nested object of type runtime.Header")
assert.Contains(t, err.Error(), "content: application/json")
}

// TestStyleParamNestedArrayError covers oapi-codegen issue #2109: like
// nested objects, an array inside a styled object (or inside another array)
// has no defined wire format, so it must fail with an error that points at
// the alternatives instead of a bare "unsupported type".
func TestStyleParamNestedArrayError(t *testing.T) {
type Currency string
type Request struct {
ID string `json:"id"`
Currencies []Currency `json:"currencies"`
}

const want = "cannot serialize nested array of type []runtime.Currency"

t.Run("struct field", func(t *testing.T) {
for _, style := range []string{"form", "simple", "label", "matrix"} {
for _, explode := range []bool{true, false} {
_, err := StyleParamWithLocation(style, explode, "request", ParamLocationQuery, Request{})
require.Error(t, err, "style %s explode %v", style, explode)
assert.Contains(t, err.Error(), want)
assert.Contains(t, err.Error(), "content: application/json")
}
}
})

t.Run("map value", func(t *testing.T) {
_, err := StyleParamWithLocation("form", true, "request", ParamLocationQuery,
map[string][]Currency{"currencies": {"SEK"}})
require.Error(t, err)
assert.Contains(t, err.Error(), want)
})

t.Run("array of arrays", func(t *testing.T) {
_, err := StyleParamWithLocation("form", true, "request", ParamLocationQuery,
[][]Currency{{"SEK"}})
require.Error(t, err)
assert.Contains(t, err.Error(), want)
})

t.Run("deepObject still serializes arrays", func(t *testing.T) {
result, err := StyleParamWithLocation("deepObject", true, "request", ParamLocationQuery,
Request{ID: "x", Currencies: []Currency{"SEK", "EUR"}})
require.NoError(t, err)
assert.Equal(t, "request[currencies][0]=SEK&request[currencies][1]=EUR&request[id]=x", result)
})
}