Skip to content

Commit

Permalink
Debug: Test case scenario where definition is missing
Browse files Browse the repository at this point in the history
Attempt to figure out why the definition is missing on a specific scenario:

```
$ go test -run TestOpenAPI2/typealias
--- FAIL: TestOpenAPI2 (0.03s)
    --- FAIL: TestOpenAPI2/typealias (0.03s)
        main_test.go:86: incorrect output:
            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'
        main_test.go:87: diff

            --- expected
            +++ actual
            @@ -37,13 +37,3 @@
                         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

FAIL
exit status 1
FAIL	github.com/teamwork/kommentaar	1.220s
```
  • Loading branch information
rafaeljusto committed Jun 25, 2024
1 parent 6b34e3b commit 2ff59d6
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docparse/docparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docparse/docparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
18 changes: 13 additions & 5 deletions docparse/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"go/parser"
"go/token"
"io"
"os"
"path"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions docparse/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)

Expand Down
22 changes: 22 additions & 0 deletions testdata/openapi2/src/typealias/in.go
Original file line number Diff line number Diff line change
@@ -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}
48 changes: 48 additions & 0 deletions testdata/openapi2/src/typealias/want.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2ff59d6

Please sign in to comment.