swag icon indicating copy to clipboard operation
swag copied to clipboard

Swagger comments in Go files are not recursively parsed by Swaggo

Open itoqsky opened this issue 1 year ago • 3 comments

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

itoqsky avatar Jun 27 '23 13:06 itoqsky

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

Monpoke avatar Jul 02 '23 14:07 Monpoke

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!

Monpoke avatar Jul 02 '23 14:07 Monpoke

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"})
}

scottmangiapane avatar Dec 30 '23 07:12 scottmangiapane