Skip to content

Commit

Permalink
remove support for uint8 arrays in query params
Browse files Browse the repository at this point in the history
this is an special case and is treated as a byte array enoded in base64
  • Loading branch information
ross96D committed Dec 31, 2023
1 parent e0893c4 commit 7ce928c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 32 deletions.
16 changes: 0 additions & 16 deletions huma.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,22 +851,6 @@ func Register[I, O any](api API, op Operation, handler func(context.Context, *I)
f.Set(reflect.ValueOf(vs))
pv = vs

case reflect.Uint8:
values := strings.Split(value, ",")
vs, err := parseArrElement(values, func(s string) (uint8, error) {
val, err := strconv.ParseUint(s, 10, 8)
if err != nil {
return 0, err
}
return uint8(val), nil
})
if err != nil {
res.Add(pb, value, "invalid integer")
return
}
f.Set(reflect.ValueOf(vs))
pv = vs

case reflect.Uint16:
values := strings.Split(value, ",")
vs, err := parseArrElement(values, func(s string) (uint16, error) {
Expand Down
39 changes: 25 additions & 14 deletions huma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,26 @@ func TestFeatures(t *testing.T) {
Method: http.MethodGet,
Path: "/test-params/{string}/{int}",
}, func(ctx context.Context, input *struct {
PathString string `path:"string"`
PathInt int `path:"int"`
QueryString string `query:"string"`
QueryInt int `query:"int"`
QueryDefault float32 `query:"def" default:"135" example:"5"`
QueryBefore time.Time `query:"before"`
QueryDate time.Time `query:"date" timeFormat:"2006-01-02"`
QueryUint uint32 `query:"uint"`
QueryBool bool `query:"bool"`
QueryStrings []string `query:"strings"`
QueryInts []int `query:"ints"`
QueryInts64 []int64 `query:"ints64"`
QueryUints []uint `query:"uints"`
PathString string `path:"string"`
PathInt int `path:"int"`
QueryString string `query:"string"`
QueryInt int `query:"int"`
QueryDefault float32 `query:"def" default:"135" example:"5"`
QueryBefore time.Time `query:"before"`
QueryDate time.Time `query:"date" timeFormat:"2006-01-02"`
QueryUint uint32 `query:"uint"`
QueryBool bool `query:"bool"`
QueryStrings []string `query:"strings"`
QueryInts []int `query:"ints"`
QueryInts8 []int8 `query:"ints8"`
QueryInts16 []int16 `query:"ints16"`
QueryInts32 []int32 `query:"ints32"`
QueryInts64 []int64 `query:"ints64"`
QueryUints []uint `query:"uints"`
// QueryUints8 []uint8 `query:"uints8"`
QueryUints16 []uint16 `query:"uints16"`
QueryUints32 []uint32 `query:"uints32"`
QueryUints64 []uint64 `query:"uints64"`
QueryFloats32 []float32 `query:"floats32"`
QueryFloats64 []float64 `query:"floats64"`
HeaderString string `header:"String"`
Expand All @@ -117,16 +123,21 @@ func TestFeatures(t *testing.T) {
assert.Equal(t, "baz", input.HeaderString)
assert.Equal(t, 789, input.HeaderInt)
assert.Equal(t, []int{2, 3}, input.QueryInts)
assert.Equal(t, []int8{4, 5}, input.QueryInts8)
assert.Equal(t, []int16{4, 5}, input.QueryInts16)
assert.Equal(t, []int32{4, 5}, input.QueryInts32)
assert.Equal(t, []int64{4, 5}, input.QueryInts64)
assert.Equal(t, []uint{1, 2}, input.QueryUints)
assert.Equal(t, []uint16{10, 15}, input.QueryUints16)
assert.Equal(t, []uint32{10, 15}, input.QueryUints32)
assert.Equal(t, []uint64{10, 15}, input.QueryUints64)
assert.Equal(t, []float32{2.2, 2.3}, input.QueryFloats32)
assert.Equal(t, []float64{3.2, 3.3}, input.QueryFloats64)
return nil, nil
})
},
Method: http.MethodGet,
URL: "/test-params/foo/123?string=bar&int=456&before=2023-01-01T12:00:00Z&date=2023-01-01&uint=1&bool=true&strings=foo,bar&ints=2,3&ints64=4,5&uints=1,2&uints32=10,15&floats32=2.2,2.3&floats64=3.2,3.3",
URL: "/test-params/foo/123?string=bar&int=456&before=2023-01-01T12:00:00Z&date=2023-01-01&uint=1&bool=true&strings=foo,bar&ints=2,3&ints8=4,5&ints16=4,5&ints32=4,5&ints64=4,5&uints=1,2&uints16=10,15&uints32=10,15&uints64=10,15&floats32=2.2,2.3&floats64=3.2,3.3",
Headers: map[string]string{
"string": "baz",
"int": "789",
Expand Down
2 changes: 0 additions & 2 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,6 @@ func Validate(r Registry, s *Schema, path *PathBuffer, mode ValidateMode, v any,
handleArray(r, s, path, mode, res, arr)
case []uint:
handleArray(r, s, path, mode, res, arr)
case []uint8:
handleArray(r, s, path, mode, res, arr)
case []uint16:
handleArray(r, s, path, mode, res, arr)
case []uint32:
Expand Down
50 changes: 50 additions & 0 deletions validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,56 @@ var validateTests = []struct {
typ: reflect.TypeOf([]int{}),
input: []int{1, 2, 3},
},
{
name: "array success",
typ: reflect.TypeOf([]int8{}),
input: []int8{1, 2, 3},
},
{
name: "array success",
typ: reflect.TypeOf([]int16{}),
input: []int16{1, 2, 3},
},
{
name: "array success",
typ: reflect.TypeOf([]int32{}),
input: []int32{1, 2, 3},
},
{
name: "array success",
typ: reflect.TypeOf([]int64{}),
input: []int64{1, 2, 3},
},
{
name: "array success",
typ: reflect.TypeOf([]uint{}),
input: []uint{1, 2, 3},
},
{
name: "array success",
typ: reflect.TypeOf([]uint16{}),
input: []uint16{1, 2, 3},
},
{
name: "array success",
typ: reflect.TypeOf([]uint32{}),
input: []uint32{1, 2, 3},
},
{
name: "array success",
typ: reflect.TypeOf([]uint64{}),
input: []uint64{1, 2, 3},
},
{
name: "array success",
typ: reflect.TypeOf([]float32{}),
input: []float32{1.1, 2.3, 3.52},
},
{
name: "array success",
typ: reflect.TypeOf([]float64{}),
input: []float64{3.1, 2.3, 3.62},
},
{
name: "expected array",
typ: reflect.TypeOf([]any{}),
Expand Down

0 comments on commit 7ce928c

Please sign in to comment.