From d6e2c0c1f26f7fa1ed495fca5608d1bc642a044d Mon Sep 17 00:00:00 2001 From: iisteev Date: Mon, 24 Nov 2025 14:02:54 +0000 Subject: [PATCH 1/2] feat: support slice type on example tag Signed-off-by: iisteev --- openapi/generator.go | 11 +++++++++++ openapi/generator_test.go | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/openapi/generator.go b/openapi/generator.go index 78fa988..4ddf818 100644 --- a/openapi/generator.go +++ b/openapi/generator.go @@ -1304,6 +1304,17 @@ func parseExampleValue(t reflect.Type, value string) (interface{}, error) { return parseExampleValue(t.Elem(), value) case reflect.Struct: return nil, fmt.Errorf("type %s does not implement Exampler", t.String()) + case reflect.Slice: + vals := strings.Split(value, ",") + vs := reflect.MakeSlice(reflect.SliceOf(t.Elem()), 0, 0) + for i := range vals { + v, err := parseExampleValue(t.Elem(), vals[i]) + if err != nil { + return nil, err + } + vs = reflect.Append(vs, reflect.ValueOf(v)) + } + return vs.Interface(), nil default: return nil, fmt.Errorf("unsuported type: %s", t.String()) } diff --git a/openapi/generator_test.go b/openapi/generator_test.go index e63ac74..fdcdae1 100644 --- a/openapi/generator_test.go +++ b/openapi/generator_test.go @@ -1005,6 +1005,12 @@ func TestGenerator_parseExampleValue(t *testing.T) { "2022-02-07T18:00:00+09:00", customTime(time.Date(2022, time.February, 7, 18, 0, 0, 0, time.FixedZone("", 9*3600))), }, + { + "mapping to slice", + reflect.TypeOf([]string{}), + "coucou1,coucou2", + []string{"coucou1", "coucou2"}, + }, } for _, tc := range testCases { From 8fe335966aff93d731f4d249079f13f769b117b0 Mon Sep 17 00:00:00 2001 From: iisteev Date: Mon, 24 Nov 2025 14:04:02 +0000 Subject: [PATCH 2/2] feat: support slice type on default tag Signed-off-by: iisteev --- openapi/types.go | 12 ++++++++++++ openapi/types_test.go | 32 +++++++++++++++++--------------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/openapi/types.go b/openapi/types.go index cd3fd59..c17fd09 100644 --- a/openapi/types.go +++ b/openapi/types.go @@ -6,6 +6,7 @@ import ( "net/url" "reflect" "strconv" + "strings" "time" "github.com/gofrs/uuid" @@ -217,6 +218,17 @@ func stringToType(val string, t reflect.Type) (interface{}, error) { return strconv.ParseUint(val, 10, t.Bits()) case reflect.Float32, reflect.Float64: return strconv.ParseFloat(val, t.Bits()) + case reflect.Slice: + vals := strings.Split(val, ",") + vs := reflect.MakeSlice(reflect.SliceOf(t.Elem()), 0, 0) + for i := range vals { + v, err := stringToType(vals[i], t.Elem()) + if err != nil { + return nil, err + } + vs = reflect.Append(vs, reflect.ValueOf(v)) + } + return vs.Interface(), nil } return nil, fmt.Errorf("unknown type %s", t.String()) } diff --git a/openapi/types_test.go b/openapi/types_test.go index 3ee6852..977498d 100644 --- a/openapi/types_test.go +++ b/openapi/types_test.go @@ -212,20 +212,22 @@ func TestStringToType(t *testing.T) { t reflect.Type v interface{} }{ - "coucou": {rt(""), "coucou"}, - "0": {rt(int8(0)), int64(0)}, - "1": {rt(int16(0)), int64(1)}, - "2": {rt(int32(0)), int64(2)}, - "3": {rt(int64(0)), int64(3)}, - "4": {rt(uint8(4)), uint64(4)}, - "5": {rt(uint16(5)), uint64(5)}, - "6": {rt(uint32(6)), uint64(6)}, - "7": {rt(uint64(7)), uint64(7)}, - "true": {rt(true), true}, - "f": {rt(false), false}, - "invalid": {rt(true), false}, - "8": {rt(float32(8)), float64(8.0)}, - "9.1": {rt(float64(9.1)), float64(9.1)}, + "coucou": {rt(""), "coucou"}, + "0": {rt(int8(0)), int64(0)}, + "1": {rt(int16(0)), int64(1)}, + "2": {rt(int32(0)), int64(2)}, + "3": {rt(int64(0)), int64(3)}, + "4": {rt(uint8(4)), uint64(4)}, + "5": {rt(uint16(5)), uint64(5)}, + "6": {rt(uint32(6)), uint64(6)}, + "7": {rt(uint64(7)), uint64(7)}, + "true": {rt(true), true}, + "f": {rt(false), false}, + "invalid": {rt(true), false}, + "8": {rt(float32(8)), float64(8.0)}, + "9.1": {rt(float64(9.1)), float64(9.1)}, + "coucou1,coucou2": {rt([]string{}), []string{"coucou1", "coucou2"}}, + "9.1,9.2": {rt([]float64{}), []float64{float64(9.1), float64(9.2)}}, } for from, to := range tests { vv, err := stringToType(from, to.t) @@ -238,7 +240,7 @@ func TestStringToType(t *testing.T) { } // Expect error for unknown unsuported types // in conversion. - _, err := stringToType("", reflect.TypeOf([]string{})) + _, err := stringToType("", reflect.TypeOf(new(map[string]string))) if err == nil { t.Errorf("expected error to be non-nil for unsupported type") }