Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swag init doesn't parse 'module' dependencies for types, they must be vendored #1734

Open
jonathanwin opened this issue Jan 9, 2024 · 4 comments

Comments

@jonathanwin
Copy link

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

  1. 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)
}
  1. go mod init example.com/swagmodtest
  2. go mod tidy
  3. swag init
    [...]/main.go :cannot find type definition: primitive.Binary

Fix with vendoring

  1. go mod vendor
  2. 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 []}
@sdghchj
Copy link
Member

sdghchj commented Jan 10, 2024

swag init --parseDependency

@jonathanwin
Copy link
Author

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)

@itaranto
Copy link

itaranto commented Feb 16, 2024

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...

@itaranto
Copy link

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants