swaggo / swag

Automatically generate RESTful API documentation with Swagger 2.0 for Go.
MIT License
10.68k stars 1.2k forks source link

Swagger comments in Go files are not recursively parsed by Swaggo #1613

Open itoqsky opened 1 year ago

itoqsky commented 1 year ago

Issue Description Swaggo fails to recursively parse Swagger comments in Go files when using the swag init command with multiple directories and the --parseDependency and --parseInternal flags. Only the comments in the main file specified with the -g flag are parsed, while the comments in other files within the specified directories are not considered. docs directory is included in router(internal/transport/htpp/handler.go) and in cmd/app/main.go

commands I have run: swag init -d cmd/app/ -g main.go --parseDependency --parseInternal

swag init -d cmd/app/,internal/transport/http/v1 -g main.go --parseDependency --parseInternal

All the commands above produce the same result

Result in the swagger.yml

Screenshot 2023-06-27 at 16 24 50

Command Respond

Screenshot 2023-06-27 at 16 15 24

Structure

Screenshot 2023-06-27 at 16 15 50

Files that contain swaggo comments are internal/transport/http/v1/auth.go, internal/transport/http/v1/trip.go, and cmd/app/main.go

Comments in one of the files other than cmd/app/main.go

Screenshot 2023-06-27 at 16 16 10

Your swag version e.g. 1.8.12

Your go version e.g. 1.20.2

OS OSX

Monpoke commented 1 year ago

Hello, I have the same issue issue, with same kind of directory structure! :)

Monpoke commented 1 year ago

After a few more tries, I finally made it work this way: swag init --parseInternal --dir cmd/api/,internal/app/ --output internal/app/docs/

It generates a warning but I got my JSON/yaml file at the end! image

image

I hope it helps you!

scottmangiapane commented 10 months ago

I've been banging my head against the wall for so long and just figured it out 🤦‍♂️

swag init was all I needed, no extra params, even though my annotations were in the controllers directory:

My problem was that I had a blank line between my annotations and the function declaration. AKA this...

// @Summary sign up
...
// @Router /auth/sign-up [post]

func PostSignUp(c *gin.Context) {
    c.IndentedJSON(http.StatusBadRequest, gin.H{"code": "NOT_IMPLEMENTED", "message": "Not implemented"})
}

Needed to be this:

// @Summary sign up
...
// @Router /auth/sign-up [post]
func PostSignUp(c *gin.Context) {
    c.IndentedJSON(http.StatusBadRequest, gin.H{"code": "NOT_IMPLEMENTED", "message": "Not implemented"})
}