Skip to content
Merged
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
28 changes: 22 additions & 6 deletions docparse/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,8 @@ start:
typ = &ast.Ident{Name: resolvedName}
}
}
if p.Type == "enum" && len(p.Enum) == 0 {
if variations, err := getEnumVariations(ref.File, pkg, typ.Name); len(variations) > 0 {
p.Enum = variations
} else if err != nil {
return nil, err
}
if err := fillEnumVariations(&p, ref.File, pkg, typ.Name); err != nil {
return nil, err
}
if mappedType == "" {
// Only check for canonicalType if this isn't mapped.
Expand Down Expand Up @@ -396,6 +392,12 @@ start:
return nil, fmt.Errorf("cannot get canonical type: %v", err)
}
if canon != nil {
// Resolve enum variations before goto start: after re-entry the
// type name becomes the canonical primitive (e.g. "string") and
// getEnumVariations would look for the wrong type.
if err := fillEnumVariations(&p, ref.File, pkg, name.Name); err != nil {
return nil, err
}
sw = canon
goto start
}
Expand Down Expand Up @@ -675,6 +677,20 @@ func fillGenericsSchema(
return nil
}

// fillEnumVariations populates p.Enum if p.Type is "enum" and no values have
// been set yet. It is a no-op otherwise.
func fillEnumVariations(p *Schema, currentFile, pkgPath, typeName string) error {
if p.Type != "enum" || len(p.Enum) != 0 {
return nil
}
variations, err := getEnumVariations(currentFile, pkgPath, typeName)
if err != nil {
return err
}
p.Enum = variations
return nil
}

// Helper function to extract enum variations from a file.
func getEnumVariations(currentFile, pkgPath, typeName string) ([]string, error) {
resolvedPath, pkg, err := resolvePackage(currentFile, pkgPath)
Expand Down
39 changes: 39 additions & 0 deletions docparse/jsonschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,45 @@ func TestFieldToProperty(t *testing.T) {
}
})

t.Run("external_enum", func(t *testing.T) {
wantExternal := map[string]*Schema{
"status": {Type: "string", Enum: []string{"active", "inactive", "pending"}},
"statuses": {Type: "array", Items: &Schema{Type: "string", Enum: []string{"active", "inactive", "pending"}}},
}

prog := NewProgram(false)
ts, _, _, err := findType("./testdata/src/a/a.go", "a", "withExternalEnum")
if err != nil {
t.Fatalf("could not parse file: %v", err)
}

st, ok := ts.Type.(*ast.StructType)
if !ok {
t.Fatal("not a struct?!")
}

for _, f := range st.Fields.List {
out, err := fieldToSchema(prog, f.Names[0].Name, "json", Reference{
Package: "a",
File: "./testdata/src/a/a.go",
Context: "req",
}, f, nil)
if err != nil {
t.Fatal(err)
}

for _, n := range f.Names {
w, ok := wantExternal[n.Name]
if !ok {
t.Fatalf("no test case for %v", n.Name)
}
if d := diff.Diff(w, out); d != "" {
t.Errorf("%v: %v", n.Name, d)
}
}
}
})

t.Run("nested", func(t *testing.T) {
prog := NewProgram(false)
ts, _, _, err := findType("./testdata/src/a/a.go", "a", "nested")
Expand Down
13 changes: 12 additions & 1 deletion docparse/testdata/src/a/a.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package a

import "net/mail"
import (
"net/mail"

"b"
)

// GET /
//
Expand Down Expand Up @@ -78,3 +82,10 @@ type refAnother2 struct {
strct bar
pkg mail.Address
}

type withExternalEnum struct {
// {enum}
status b.StatusType
// {enum}
statuses []b.StatusType
}
9 changes: 9 additions & 0 deletions docparse/testdata/src/b/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package b

type StatusType string

const (
StatusTypeActive StatusType = "active"
StatusTypeInactive StatusType = "inactive"
StatusTypePending StatusType = "pending"
)
Loading