Skip to content

Commit

Permalink
allow arrays to not specify items
Browse files Browse the repository at this point in the history
  • Loading branch information
jakecoffman committed Apr 23, 2021
1 parent deb2097 commit 0182b6f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
29 changes: 19 additions & 10 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,16 +275,19 @@ func (f Field) Unknown(allow bool) Field {
func (f *Field) ToSwaggerParameters(in string) (parameters []Parameter) {
switch f.kind {
case KindArray:
items := f.arr.ToJsonSchema()
parameters = append(parameters, Parameter{
p := Parameter{
In: in,
Type: f.kind,
Items: &items,
CollectionFormat: "multi",
Required: f.required,
Description: f.description,
Default: f._default,
})
}
if f.arr != nil {
items := f.arr.ToJsonSchema()
p.Items = &items
}
parameters = append(parameters, p)
case KindObject:
for name, field := range f.obj {
param := Parameter{
Expand All @@ -299,8 +302,10 @@ func (f *Field) ToSwaggerParameters(in string) (parameters []Parameter) {
Maximum: field.max,
}
if field.kind == KindArray {
temp := field.arr.ToJsonSchema()
param.Items = &temp
if field.arr != nil {
temp := field.arr.ToJsonSchema()
param.Items = &temp
}
param.CollectionFormat = "multi"
}
if field.kind == KindObject {
Expand All @@ -320,8 +325,10 @@ func (f *Field) ToJsonSchema() JsonSchema {

switch f.kind {
case KindArray:
items := f.arr.ToJsonSchema()
schema.Items = &items
if f.arr != nil {
items := f.arr.ToJsonSchema()
schema.Items = &items
}
case KindObject:
schema.Properties = map[string]JsonSchema{}
for name, field := range f.obj {
Expand All @@ -341,8 +348,10 @@ func (f *Field) ToJsonSchema() JsonSchema {
prop.Maximum = *field.max
}
if prop.Type == KindArray {
items := field.arr.ToJsonSchema()
prop.Items = &items
if field.arr != nil {
items := field.arr.ToJsonSchema()
prop.Items = &items
}
}
schema.Properties[name] = prop
}
Expand Down
13 changes: 13 additions & 0 deletions prehandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ func TestQueryValidation(t *testing.T) {
Input: "testquery=b",
Expected: errEnumNotFound,
},
{
Schema: map[string]Field{
"testarray": Array(),
},
Input: "testarray=1&testarray=a",
Expected: nil,
},
{
Schema: map[string]Field{
"testquery": Array().Items(Number()),
Expand Down Expand Up @@ -394,6 +401,12 @@ func TestBodyValidation(t *testing.T) {
},
Input: `{"obj2":{"inner":"not a number"}}`,
Expected: errWrongType,
}, {
Schema: map[string]Field{
"arr1": Array(),
},
Input: `{"arr1":[1,"a"]}`,
Expected: nil,
}, {
Schema: map[string]Field{
"arr1": Array().Items(Number()),
Expand Down

0 comments on commit 0182b6f

Please sign in to comment.