5

I am using swaggo generate API document based on godoc syntax.

Source folder and files

|-post
|--controller.go
|--response.go

For this definition:

controller.go

package post

...

// Index godoc
// @Summary Index Post
// @Success 200 {array} []*Response
// @Router /v1/posts [get]
func Index(ctx *gin.Context) {
  ...
}

The Response is been defined in an another file but the same package.

response.go

package post

// Response  is post response body
type Response struct {
    ID   int64  `json:"id"`
    Name string `json:"name"`

    CreatedAt string `json:"created_at"`
    UpdatedAt string `json:"updated_at"`
}

But when run swag init to generate swagger docs, it said:

2021/01/29 09:39:56 Generate swagger docs....
2021/01/29 09:39:56 Generate general API Info, search dir:./
2021/01/29 09:39:56 ParseComment error in file application/post/controller.go :cannot find type definition:

When I move the Response struct to controller.go, it works.

How to import it with godoc or swaggo?

peterh
  • 11,875
  • 18
  • 85
  • 108
iooi
  • 453
  • 2
  • 10
  • 23

4 Answers4

8

I also encountered this problem, but I solved it with the below command:

swag init --parseDependency --parseInternal
i773
  • 75
  • 1
  • 11
Lai Lee
  • 1,054
  • 14
  • 17
7

You need to append --parseDependency to the swag init command.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
obo
  • 71
  • 1
  • 3
2

For a quick solution, run

go get -u github.com/swaggo/swag/cmd/swag@v1.6.7

You can find more about this solution here.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
The Oracle
  • 2,373
  • 3
  • 26
  • 44
0

swag init --parseDependency true should solve your issue

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116