swag
swag copied to clipboard
`swag init` doesn't parse 'module' dependencies for types, they must be vendored
When a type is declared in a dependency, swag init
fails with "cannot find type definition: ..."
To successfully generate docs, dependencies must be vendored with go mod vendor
and then use swag init -parseVendor
Is this intended/an unmentioned limitation?
Thanks!!
To Reproduce
- Setup project: example.com/swagmodtest (within or outside of GOPATH):
~/swagmodtest/main.go
or$GOPATH/example.com/swagmodtest/main.go
package main
import (
"fmt"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// @id test
// @Router /test [get]
// @Success 200 {array} primitive.Binary
func main() {
var t primitive.Binary
fmt.Println(t)
}
- go mod init example.com/swagmodtest
- go mod tidy
- swag init
[...]/main.go :cannot find type definition: primitive.Binary
Fix with vendoring
- go mod vendor
- swag init -parseVendor swagger.yaml:
definitions:
primitive.Binary:
properties:
data:
items:
type: integer
type: array
subtype:
type: integer
type: object
info:
contact: {}
paths:
/test:
get:
operationId: test
responses:
"200":
description: OK
schema:
items:
$ref: '#/definitions/primitive.Binary'
type: array
swagger: "2.0"
Your swag version
- swag version v1.16.2
Your go version
- go version go1.18.3 linux/amd64
Desktop:
- OS: debian bullseye
Additional context The code builds/runs fine with go modules and no vendoring
$ rm vendor/ -rf
$ go run main.go
{0 []}
swag init --parseDependency
Well, doh! That works
Maybe the documentation needs to be updated:
README.md indicates:
--parseDependency Parse go files in outside dependency folder, disabled by default
swag init --help indicates:
--parseDependency, --pd Parse go files inside dependency folder, disabled by default (default: false)
Go modules are not a single "dependency folder" AFAIK. Is there any source other than go.mod that --parseDependency enables?
Maybe a better explanation would be:
--parseDependency, --pd Parse dependencies from go modules (go.mod); disabled by default (default: false)
I've found that --parseGoList
does the job better, it allows you to reference any type without needing to do awkward "underscore" imports.
I wonder why this isn't the default...
I've found that
--parseGoList
does the job better, it allows you to reference any type without needing to do awkward "underscore" imports.I wonder why this isn't the default...
Oh, it is the default. I may have been confused somehow...