Skip to content

Commit b6aa2b1

Browse files
mromaszewiczclaude
andauthored
fix: explain nested arrays in styled parameters (#192)
An array nested inside a styled object, map, or array has no wire format defined by OpenAPI, the same as a nested object. It failed with a bare "unsupported type []T", which gave no hint about the cause. Return an error naming the limitation and the alternatives (a `content` parameter or `style: deepObject`), matching the existing nested-object error. Closes #191 Refs oapi-codegen/oapi-codegen#2109 Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
1 parent b173098 commit b6aa2b1

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎styleparam.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,14 @@ func primitiveToString(value interface{}) (string, error) {
517517
"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",
518518
reflect.TypeOf(value).String())
519519
}
520+
if (kind == reflect.Slice || kind == reflect.Array) && !isByteSlice(t) {
521+
// An array nested inside a styled object or array. Like
522+
// nested objects, OpenAPI leaves this undefined, so there
523+
// is no wire format we could produce here.
524+
return "", fmt.Errorf(
525+
"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",
526+
reflect.TypeOf(value).String())
527+
}
520528
return "", fmt.Errorf("unsupported type %s", reflect.TypeOf(value).String())
521529
}
522530

‎styleparam_test.go‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,3 +914,49 @@ func TestStyleParamNestedStructError(t *testing.T) {
914914
assert.Contains(t, err.Error(), "cannot serialize nested object of type runtime.Header")
915915
assert.Contains(t, err.Error(), "content: application/json")
916916
}
917+
918+
// TestStyleParamNestedArrayError covers oapi-codegen issue #2109: like
919+
// nested objects, an array inside a styled object (or inside another array)
920+
// has no defined wire format, so it must fail with an error that points at
921+
// the alternatives instead of a bare "unsupported type".
922+
func TestStyleParamNestedArrayError(t *testing.T) {
923+
type Currency string
924+
type Request struct {
925+
ID string `json:"id"`
926+
Currencies []Currency `json:"currencies"`
927+
}
928+
929+
const want = "cannot serialize nested array of type []runtime.Currency"
930+
931+
t.Run("struct field", func(t *testing.T) {
932+
for _, style := range []string{"form", "simple", "label", "matrix"} {
933+
for _, explode := range []bool{true, false} {
934+
_, err := StyleParamWithLocation(style, explode, "request", ParamLocationQuery, Request{})
935+
require.Error(t, err, "style %s explode %v", style, explode)
936+
assert.Contains(t, err.Error(), want)
937+
assert.Contains(t, err.Error(), "content: application/json")
938+
}
939+
}
940+
})
941+
942+
t.Run("map value", func(t *testing.T) {
943+
_, err := StyleParamWithLocation("form", true, "request", ParamLocationQuery,
944+
map[string][]Currency{"currencies": {"SEK"}})
945+
require.Error(t, err)
946+
assert.Contains(t, err.Error(), want)
947+
})
948+
949+
t.Run("array of arrays", func(t *testing.T) {
950+
_, err := StyleParamWithLocation("form", true, "request", ParamLocationQuery,
951+
[][]Currency{{"SEK"}})
952+
require.Error(t, err)
953+
assert.Contains(t, err.Error(), want)
954+
})
955+
956+
t.Run("deepObject still serializes arrays", func(t *testing.T) {
957+
result, err := StyleParamWithLocation("deepObject", true, "request", ParamLocationQuery,
958+
Request{ID: "x", Currencies: []Currency{"SEK", "EUR"}})
959+
require.NoError(t, err)
960+
assert.Equal(t, "request[currencies][0]=SEK&request[currencies][1]=EUR&request[id]=x", result)
961+
})
962+
}

0 commit comments

Comments
 (0)