swag icon indicating copy to clipboard operation
swag copied to clipboard

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

Open jonathanwin opened this issue 1 year ago • 4 comments

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 []}

jonathanwin avatar Jan 09 '24 17:01 jonathanwin

swag init --parseDependency

sdghchj avatar Jan 10 '24 01:01 sdghchj

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)

jonathanwin avatar Feb 14 '24 11:02 jonathanwin

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 avatar Feb 16 '24 19:02 itaranto

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

itaranto avatar Feb 20 '24 20:02 itaranto