Swag: ParseComment error when {object} is part of named package import or package with dot

Created on 21 Jul 2020  Â·  9Comments  Â·  Source: swaggo/swag

Describe the bug
In our handler function, the returned object in the success response is geojson.FeatureCollection struct, which we import as geojson "github.com/paulmach/go.geojson". Now the issue with swag is that it cannot parse either the named import declaration (geojson) or the package name which has a dot in its name (go.geojson). Meaning:

1.

// @Success 200 {object} go.geojson.FeatureCollection
ParseComment error in file xxx :can not find schema type: "go.geojson.FeatureCollection"

2.

// @success 200 {object} geojson.FeatureCollection
ParseComment error in file xxx :can not find schema type: "geojson.FeatureCollection"

Your swag version
e.g. 1.6.7

Your go version
e.g. 1.14

Desktop (please complete the following information):

  • OS: MacOS
  • Version: 10.15.6

Most helpful comment

I just had. I get another error with the newest version, though.

echo "Generating Swagger docs..."
Generating Swagger docs...
swag init --output ./doc -g cmd/xxx/main.go
2020/07/24 16:36:34 Generate swagger docs....
2020/07/24 16:36:34 Generate general API Info, search dir:./
2020/07/24 16:36:34 warning: failed to get package name in dir: ./, error: execute go list command, exit status 1, stdout:, stderr:can't load package: package .: no Go files in /xxx
2020/07/24 16:36:34 Type definition of type '*ast.InterfaceType' is not supported yet. Using 'object' instead.
2020/07/24 16:36:34 Type definition of type '*ast.InterfaceType' is not supported yet. Using 'object' instead.
2020/07/24 16:36:34 Generating geoArea
2020/07/24 16:36:34 ParseComment error in file internal/infra/http/partnerarea/router.go :cannot find type definition: geojson.FeatureCollection
make: *** [swag] Error 1

So, three new logs here: the failed to get package warning (which is new), the not supported logs (they aren't fatal though), and regarding the geojson.FeatureCollection:

The parse error you see has to do with a struct (called geoArea) which has the geojson.FeatureCollection struct as a field. I use this geoArea struct as a success response object in another handlerfunc on the same file. So the problem has now extended to a new place.

All 9 comments

It has been fixed by latest commit in master branch.
Just have a try.

I just had. I get another error with the newest version, though.

echo "Generating Swagger docs..."
Generating Swagger docs...
swag init --output ./doc -g cmd/xxx/main.go
2020/07/24 16:36:34 Generate swagger docs....
2020/07/24 16:36:34 Generate general API Info, search dir:./
2020/07/24 16:36:34 warning: failed to get package name in dir: ./, error: execute go list command, exit status 1, stdout:, stderr:can't load package: package .: no Go files in /xxx
2020/07/24 16:36:34 Type definition of type '*ast.InterfaceType' is not supported yet. Using 'object' instead.
2020/07/24 16:36:34 Type definition of type '*ast.InterfaceType' is not supported yet. Using 'object' instead.
2020/07/24 16:36:34 Generating geoArea
2020/07/24 16:36:34 ParseComment error in file internal/infra/http/partnerarea/router.go :cannot find type definition: geojson.FeatureCollection
make: *** [swag] Error 1

So, three new logs here: the failed to get package warning (which is new), the not supported logs (they aren't fatal though), and regarding the geojson.FeatureCollection:

The parse error you see has to do with a struct (called geoArea) which has the geojson.FeatureCollection struct as a field. I use this geoArea struct as a success response object in another handlerfunc on the same file. So the problem has now extended to a new place.

I am running into a similar issue(cannot find type definition) when I upgraded from 1.6.5 to 1.6.7. Was there any solution found for this?

@apanagiotou Any luck finding a solution for your above problem?

Nothing, unfortunately. I bypassed the above by using another struct for documenting the code.

+1

+1, i got the same error@sdghchj

I got the same error and I solved it by downgrade the swaggo version to v1.6.3.

Refer to #817

Was this page helpful?
0 / 5 - 0 ratings