Skip to content
Open
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
11 changes: 11 additions & 0 deletions openapi/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
6 changes: 6 additions & 0 deletions openapi/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions openapi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"
"reflect"
"strconv"
"strings"
"time"

"github.com/gofrs/uuid"
Expand Down Expand Up @@ -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())
}
Expand Down
32 changes: 17 additions & 15 deletions openapi/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
}
Expand Down