swaggo / swag

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

crash on version 1.8.5 when running 'swag init --parseDependency' #1319

Open sunrui opened 2 years ago

sunrui commented 2 years ago

hi, i found a bug content as follows

swag init --parseDependency 2022/09/06 21:11:42 Generate swagger docs.... 2022/09/06 21:11:42 Generate general API Info, search dir:./ panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x2 addr=0x8 pc=0x102814d20]

goroutine 1 [running]: github.com/swaggo/swag.(PackagesDefinitions).parseFunctionScopedTypesFromFile(0x1400000e858, 0x14001732880, {0x1400037d8f0, 0x15}, 0x140026b3ad0) go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:168 +0x90 github.com/swaggo/swag.(PackagesDefinitions).ParseTypes(0x1400000e858) go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:110 +0xa0 github.com/swaggo/swag.(Parser).ParseAPIMultiSearchDir(0x140001b42a0, {0x140001da0b0?, 0x1?, 0x0?}, {0x10283f25a?, 0x7?}, 0x64) go/pkg/mod/github.com/swaggo/swag@v1.8.5/parser.go:362 +0x29c github.com/swaggo/swag/gen.(Gen).Build(0x140001ad980, 0x140001aeea0) go/pkg/mod/github.com/swaggo/swag@v1.8.5/gen/gen.go:177 +0x4a0 main.initAction(0x140001418c0?) go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:151 +0x654 github.com/urfave/cli/v2.(Command).Run(0x14000000fc0, 0x140001c6340) go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/command.go:163 +0x4fc github.com/urfave/cli/v2.(App).RunContext(0x1400011d6c0, {0x1029f5630?, 0x140000300b8}, {0x140000201b0, 0x3, 0x3}) go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:313 +0x860 github.com/urfave/cli/v2.(*App).Run(...) go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:224 main.main() go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:221 +0x65c

li1234yun commented 2 years ago

Me too

heafox commented 2 years ago

Me too

ShinJustinHolly3317 commented 2 years ago

use 1.8.4 instead, that's current stable version. and some people already working on it

mstrYoda commented 2 years ago

[Duplicate] Same on here: https://github.com/swaggo/swag/issues/1309

Fixed with the merged pr: https://github.com/swaggo/swag/pull/1310

You can use latest stable version v1.8.4.

movpushmov commented 2 years ago

[Duplicate] Same on here: #1309

Fixed with the merged pr: #1310

You can use latest stable version v1.8.4.

@mstrYoda

v1.8.4 doesn't work for me.

panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x2 addr=0x8 pc=0x10065cfc8]

goroutine 1 [running]: github.com/swaggo/swag.(PackagesDefinitions).parseFunctionScopedTypesFromFile(0x140001a8828, 0x140031c2a80, {0x140003bf8f0, 0x29}, 0x140057223c0) /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:168 +0x98 github.com/swaggo/swag.(PackagesDefinitions).ParseTypes(0x140001a8828) /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:110 +0xa0 github.com/swaggo/swag.(Parser).ParseAPIMultiSearchDir(0x140002602a0, {0x1400028e060?, 0x1?, 0x0?}, {0x16fb93765?, 0xe?}, 0x64) /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/parser.go:362 +0x2b0 github.com/swaggo/swag/gen.(Gen).Build(0x14000259980, 0x1400025f380) /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/gen/gen.go:177 +0x4a8 main.initAction(0x1400022b8c0?) /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:151 +0x658 github.com/urfave/cli/v2.(Command).Run(0x140002298c0, 0x1400026e4c0) /Users/movpushmov/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/command.go:163 +0x50c github.com/urfave/cli/v2.(App).RunContext(0x14000251520, {0x10082d670?, 0x140001b4008}, {0x140001b8000, 0x9, 0x9}) /Users/movpushmov/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:313 +0x888 github.com/urfave/cli/v2.(*App).Run(...) /Users/movpushmov/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:224 main.main() /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:221 +0x660 make: *** [generate-swagger] Error 2

mstrYoda commented 2 years ago

[Duplicate] Same on here: #1309 Fixed with the merged pr: #1310 You can use latest stable version v1.8.4.

@mstrYoda

v1.8.4 doesn't work for me.

panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x2 addr=0x8 pc=0x10065cfc8]

goroutine 1 [running]: github.com/swaggo/swag.(PackagesDefinitions).parseFunctionScopedTypesFromFile(0x140001a8828, 0x140031c2a80, {0x140003bf8f0, 0x29}, 0x140057223c0) /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:168 +0x98 github.com/swaggo/swag.(PackagesDefinitions).ParseTypes(0x140001a8828) /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:110 +0xa0 github.com/swaggo/swag.(Parser).ParseAPIMultiSearchDir(0x140002602a0, {0x1400028e060?, 0x1?, 0x0?}, {0x16fb93765?, 0xe?}, 0x64) /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/parser.go:362 +0x2b0 github.com/swaggo/swag/gen.(Gen).Build(0x14000259980, 0x1400025f380) /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/gen/gen.go:177 +0x4a8 main.initAction(0x1400022b8c0?) /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:151 +0x658 github.com/urfave/cli/v2.(Command).Run(0x140002298c0, 0x1400026e4c0) /Users/movpushmov/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/command.go:163 +0x50c github.com/urfave/cli/v2.(App).RunContext(0x14000251520, {0x10082d670?, 0x140001b4008}, {0x140001b8000, 0x9, 0x9}) /Users/movpushmov/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:313 +0x888 github.com/urfave/cli/v2.(*App).Run(...) /Users/movpushmov/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:224 main.main() /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:221 +0x660 make: *** [generate-swagger] Error 2

Are you sure?

Your error message says that you are still using v1.8.5: /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:221

You can use v1.8.4 in your go.mod file

FabianMartin commented 2 years ago

try the master branch of swaggo. You can install it with the following command.

go install github.com/swaggo/swag/cmd/swag@master
ghoshabhi commented 1 year ago

I am still getting the same error on latest master @FabianMartin

2022/09/15 09:16:34 ParseComment error in file /Users/username/workspace/my-project/pkg/handlers/completed_form.go :cannot find type definition: PaginatedResponse[MyTemplateType]

type Pagination struct {
    Index int `json:"index"`
    Size  int `json:"size"`
    Total int `json:"total"`
}
type PaginatedResponse[T any] struct {
    Data       []T        `json:"data"`
    Pagination Pagination `json:"pagination"`
}

type MyTemplateType struct {
    ID int `json:"id"`
        Name string `json:"name"`
}

Swag annotations:

// @Summary ....
// @Description ...
// @Tags ...
// @Security ...
// @Success 200 {object} PaginatedResponse[MyTemplateType]
// @Failure 403
// @Failure 500
// @Router ... [get]

Swaggo version:

My project has: github.com/swaggo/swag v1.8.6-0.20220915145914-b8f53dafbbf8 But for some reason when i install i get v1.8.5. Could this be why ?

FabianMartin commented 1 year ago

@ghoshabhi Can you provide a test repository?

ghoshabhi commented 1 year ago

i have an example now @FabianMartin https://github.com/ghoshabhi/go-swaggo-test

image

FabianMartin commented 1 year ago

@ghoshabhi PR https://github.com/swaggo/swag/pull/1328 contains a fix for your issue.

Currently, the generics are only detected if the definition contains the pkg name If you change the following line

// @Success 200 {object} PaginatedResponse[ResponseItem]

to

// @Success 200 {object} api.PaginatedResponse[api.ResponseItem]

it should find the definition.

After merging the PR, swaggo will also detect generics without pkg name.

ghoshabhi commented 1 year ago

thanks @FabianMartin ! let me try moving the definitions to a separate pkg. No rush, but do you have a rough eta on when v1.8.6 will be released ?

ghoshabhi commented 1 year ago

I got a different error after moving type definitions to a different pkg: image

Updated the test repo if you want to test

FabianMartin commented 1 year ago

The warning contains the solution to your issue. It cannot determine the package name.

Change your call to the following, and it should work.

swag init --parseDependency --parseInternal --dir ./cmd/api
FabianMartin commented 1 year ago

thanks @FabianMartin ! let me try moving the definitions to a separate pkg. No rush, but do you have a rough eta on when v1.8.6 will be released ?

@ubogdan releases new versions and in https://github.com/swaggo/swag/pull/1310#issuecomment-1248212132 he wrote that a new version will be released soon.

ghoshabhi commented 1 year ago
```shell
swag init --parseDependency --parseInternal --dir ./cmd/api

aha! yeah that worked. Weird, according to docs -g should've had the same effect. But glad latest version is working, can't wait for the release now! Thank you so much for working on this

hoppfrosch commented 1 year ago

Same here ... happens with (swagg@v1.8.5): ` import ( "encoding/xml" )

type MyTest struct { XMLName xml.Name xml:"xmltest" .... ) `

Running swag init .... (without --parseDependency) leads to following error: ".... :cannot find type definition: xml.Name"

Whereas swag init --parseDependency .... leads to the described error github.com/swaggo/swag.(PackagesDefinitions).parseFunctionScopedTypesFromFile(0x1400000e858, 0x14001732880, {0x1400037d8f0, 0x15}, 0x140026b3ad0)*

Switching back to swag@v1.8.4 does not show the --parseDependency error.

FabianMartin commented 1 year ago

1.8.6 contains a fix for this. Current Version is 1.8.7. Please update and test again.

hoppfrosch commented 1 year ago

Thx for fast response - updated to swag@v1.8.7

*swag init --parseDependency .... works correctly