swaggo / swag

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

Swag fails to load type in external package. #1806

Open chirino opened 2 months ago

chirino commented 2 months ago

Describe the bug Swag fails to load type in external package.

To Reproduce

I created a super small project to illustrate the error:

git clone https://github.com/chirino/swag-bug-example/
cd swag-bug-example/main
swag init -g ./main.go -o .

Expected behavior no errors should occur.

Screenshots

2024/05/06 17:05:49 Generate swagger docs....
2024/05/06 17:05:49 Generate general API Info, search dir:./
2024/05/06 17:05:49 Generating main.Foo
2024/05/06 17:05:49 Error parsing type definition 'main.Foo': field: cannot find type definition: lib.Bar
2024/05/06 17:05:49 ParseComment error in file /Users/chirino/sandbox/swag-bug-example/main/main.go :Foo: field: cannot find type definition: lib.Bar
exit status 1

Your swag version v1.16.3

Your go version go1.22.

haminh0307 commented 2 months ago
swag init --parseDependency -g ./main.go -o .

You need to add option --parseDependency.

rabarar commented 2 months ago

I have the same problem - but it gives me an error saying the external package is missing when it's in both the go.mod and go.sum

running swag init --parseDependency produces:

missing go.sum entry for module providing package github.com/hcpd-solutions/pki; to add:
    go mod download github.com/hcpd-solutions/pki
missing go.sum entry for module providing package github.com/hcpd-solutions/pki; to add:
    go mod download github.com/hcpd-solutions/pki