Skip to content

Commit

Permalink
Merge pull request #85 from Teamwork/fix/array-of-enums-not-showing
Browse files Browse the repository at this point in the history
Fix: Array of enums wasn't being documented
  • Loading branch information
jrdnull authored Oct 11, 2021
2 parents 1cc15ec + 829e9b8 commit b71ccf3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
13 changes: 11 additions & 2 deletions docparse/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,12 @@ arrayStart:

p.Items = &Schema{Type: JSONSchemaType(typ.Name)}

// Generally an item is an enum rather than the array itself
if p.Enum != nil {
p.Items.Enum = p.Enum
p.Enum = nil
}

// Map []byte to []string.
if typ.Name == "byte" {
p.Items = nil
Expand Down Expand Up @@ -556,8 +562,11 @@ arrayStart:
return err
}
if t != "" {
p.Type = t
if isPrimitive(p.Type) {
if isPrimitive(t) {
if p.Items == nil {
p.Items = &Schema{}
}
p.Items.Type = t
return nil
}
}
Expand Down
1 change: 1 addition & 0 deletions docparse/jsonschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestFieldToProperty(t *testing.T) {
"pkg": {Reference: "mail.Address"},
"pkgSlice": {Type: "array", Items: &Schema{Reference: "mail.Address"}},
"pkgSliceP": {Type: "array", Items: &Schema{Reference: "mail.Address"}},
"cSlice": {Type: "array", Items: &Schema{Type: "string"}},
"deeper": {Reference: "a.refAnother"},
"docs": {Type: "string", Description: "This has some documentation!",
Required: []string{"docs"},
Expand Down
1 change: 1 addition & 0 deletions docparse/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type foo struct {
pkg mail.Address
pkgSlice []mail.Address
pkgSliceP []*mail.Address
cSlice []customStr
deeper refAnother

// This has some documentation! {required}
Expand Down
7 changes: 7 additions & 0 deletions testdata/openapi2/src/params-embed/in.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package params

type customType string

type common struct {
// FieldTasks common description.
//
// {enum: name priority status description}
FieldTasks []string `query:"fields[tasks]"`

// CustomTypes common description.
//
// {enum: blue red yellow}
CustomTypes []customType `query:"customTypes"`
}

type queryRef struct {
Expand Down
20 changes: 15 additions & 5 deletions testdata/openapi2/src/params-embed/want.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@ paths:
type: array
items:
type: string
enum:
- name
- priority
- status
- description
enum:
- name
- priority
- status
- description
- name: customTypes
in: query
description: CustomTypes common description.
type: array
items:
type: string
enum:
- blue
- red
- yellow
responses:
200:
description: 200 OK (no data)
Expand Down

0 comments on commit b71ccf3

Please sign in to comment.