diff --git a/docparse/docparse.go b/docparse/docparse.go index ee8957e..768fe43 100644 --- a/docparse/docparse.go +++ b/docparse/docparse.go @@ -177,7 +177,7 @@ var ( ) // parseComment a single comment block in the file filePath. -func parseComment(prog *Program, comment, pkgPath, filePath string) ([]*Endpoint, int, error) { +func parseComment(prog *Program, comment, filePath string) ([]*Endpoint, int, error) { e := &Endpoint{} // Get start line and determine if this is a comment block. diff --git a/docparse/docparse_test.go b/docparse/docparse_test.go index 84f53e0..c95b67f 100644 --- a/docparse/docparse_test.go +++ b/docparse/docparse_test.go @@ -308,7 +308,7 @@ Response 400 (w00t): {empty} } tt.in = test.NormalizeIndent(tt.in) - out, _, err := parseComment(prog, tt.in, ".", "docparse.go") + out, _, err := parseComment(prog, tt.in, "docparse.go") if !test.ErrorContains(err, tt.wantErr) { t.Fatalf("wrong err\nout: %#v\nwant: %#v\n", err, tt.wantErr) } diff --git a/docparse/find.go b/docparse/find.go index c81a93a..22857bf 100644 --- a/docparse/find.go +++ b/docparse/find.go @@ -7,6 +7,7 @@ import ( "go/parser" "go/token" "io" + "os" "path" "path/filepath" "reflect" @@ -46,7 +47,7 @@ func FindComments(w io.Writer, prog *Program) error { } for _, c := range f.Comments { - e, relLine, err := parseComment(prog, c.Text(), pkg.PkgPath, fullPath) + e, relLine, err := parseComment(prog, c.Text(), fullPath) if err != nil { p := fset.Position(c.Pos()) allErr = append(allErr, fmt.Errorf("%v:%v %v", @@ -316,6 +317,9 @@ func GetReference(prog *Program, context string, isEmbed bool, lookup, filePath return &ref, nil } + // hack to bypass package resolution for debugging specific test scenario + originalPkg := pkg + // Find type. ts, foundPath, pkg, err := findType(filePath, pkg, name) if err != nil { @@ -342,7 +346,7 @@ func GetReference(prog *Program, context string, isEmbed bool, lookup, filePath ref := Reference{ Name: name, - Package: pkg, + Package: originalPkg, Lookup: filepath.Base(pkg) + "." + name, File: foundPath, Context: context, @@ -733,9 +737,13 @@ func resolveType(prog *Program, context string, isEmbed bool, typ *ast.Ident, fi // in the case of current package the filePath is used, e.g: // pkg: Dir(filePath), name: Foofunc ParseLookup(lookup string, filePath string) (name, pkg string) { func ParseLookup(lookup string, filePath string) (name, pkg string) { - if c := strings.LastIndex(lookup, "."); c > -1 { - // imported path: models.Foo - return lookup[c+1:], lookup[:c] + lookupParts := strings.Split(lookup, string(os.PathSeparator)) + if len(lookupParts) > 1 { + lookupWithoutPath := lookupParts[len(lookupParts)-1] + if c := strings.LastIndex(lookupWithoutPath, "."); c > -1 { + // imported path: models.Foo + return lookupWithoutPath[c+1:], lookupWithoutPath[:c] + } } // Current package: Foo diff --git a/docparse/jsonschema.go b/docparse/jsonschema.go index 5819c3d..1368561 100644 --- a/docparse/jsonschema.go +++ b/docparse/jsonschema.go @@ -421,7 +421,7 @@ start: // Check if the type resolves to a Go primitive. lookup := pkg + "." + name.Name - t, err := getTypeInfo(prog, lookup, ref.File) + t, err := getTypeInfo(lookup, ref.File) if err != nil { return nil, err } @@ -557,7 +557,7 @@ arrayStart: // Check if the type resolves to a Go primitive. lookup := pkg + "." + name.Name - t, err := getTypeInfo(prog, lookup, ref.File) + t, err := getTypeInfo(lookup, ref.File) if err != nil { return err } @@ -621,9 +621,7 @@ func JSONSchemaType(t string) string { return t } -func getTypeInfo(prog *Program, lookup, filePath string) (string, error) { - // TODO: REMOVE THE prog PARAM, as this function is not - // using it anymore. +func getTypeInfo(lookup, filePath string) (string, error) { dbg("getTypeInfo: %#v in %#v", lookup, filePath) name, pkg := ParseLookup(lookup, filePath) diff --git a/testdata/openapi2/src/typealias/in.go b/testdata/openapi2/src/typealias/in.go new file mode 100644 index 0000000..2db618a --- /dev/null +++ b/testdata/openapi2/src/typealias/in.go @@ -0,0 +1,22 @@ +package typealias + +// ReqRef is a request reference. +type ReqRef struct { + Anonymous struct { + Alias AliasExample `json:"alias,omitempty"` + } `json:"anonymous,omitempty"` +} + +// AliasExample is an example of type alias. +type AliasExample map[string]ExampleItem + +// ExampleItem is an example item. +type ExampleItem struct { + Field1 string `json:"field1"` + Field2 int `json:"field2"` +} + +// POST /path +// +// Request body: ReqRef +// Response 200: {empty} diff --git a/testdata/openapi2/src/typealias/want.yaml b/testdata/openapi2/src/typealias/want.yaml new file mode 100644 index 0000000..cda4dd5 --- /dev/null +++ b/testdata/openapi2/src/typealias/want.yaml @@ -0,0 +1,48 @@ +swagger: "2.0" +info: + title: x + version: x +consumes: + - application/json +produces: + - application/json +paths: + /path: + post: + operationId: POST_path + consumes: + - application/json + produces: + - application/json + parameters: + - name: typealias.ReqRef + in: body + required: true + schema: + $ref: '#/definitions/typealias.ReqRef' + responses: + 200: + description: 200 OK (no data) +definitions: + typealias.ReqRef: + title: ReqRef + description: ReqRef is a request reference. + type: object + properties: + anonymous: + type: object + properties: + alias: + type: object + additionalProperties: + $ref: '#/definitions/typealias.ExampleItem' + typealias.ExampleItem: + title: ExampleItem + description: ExampleItem is an example item. + type: object + properties: + properties: + field1: + type: string + field2: + type: integer