Swag: Version 1.6.9 - cannot find type definition

Created on 26 Oct 2020  ·  9Comments  ·  Source: swaggo/swag

Go version : 1.15.2 (1.15.3 gives installation error)
Swag version: 1.6.9

Error : ParseComment error in file app/services/api/v1/models.go :cannot find type definition: models.ResponseType

Import:
import models "rest-api/app/models/api/v1"

Usage :
// @Failure 400 {object} models.ResponseType

This works fine on version 1.6.8.

Most helpful comment

For those who need a quick solution:
go get -u github.com/swaggo/swag/cmd/[email protected]

All 9 comments

Same here: type used in @Success is not found.

package handlers

import (
    "github.com/labstack/echo/v4"
    "net/http"
)

//
// Headers godoc
// @Summary returns the HTTP headers
// @Description use this to inspect the headers set by the portal and received by the service
// @Produce json
// @Success 200 {object} http.Header
// @Router /v1/headers [get]
func (env *Env) Headers(c echo.Context) error {
    return c.JSON(http.StatusOK, c.Request().Header)
}

With 1.6.9, I get "_ParseComment error in file internal/handlers/headers.go :cannot find type definition: http.Header_".

If I change the annotation to

// @Success 200 {object} map[string][]string

which is the definition of http.Header, it works.

At the moment I've reverted to 1.6.7


Edit: ok, the answer is actually in closed issue #808

Parsing of external types has been disabled, because it was an incomplete feature, as far as I understand, it went only one level deep, and when the external type was a field of an internal struct, it also wouldn't work.

Apart from the fact, that this is a pretty breaking change and obviously a lot of people missed the issue, what exactly is the supported way to handle my situation?

I want to return an external type. I can't annotate anything with swaggertype, and after playing around with --parseDependency, I have to admin I don't understand how it works. Using it like swag init --parseDependency main.go -o internal/docs makes no difference.

Basically --parseDependency sounds like it would enable the incomplete feature, that was disabled recently. Is it that? If so, it does not seem to work.

For those who need a quick solution:
go get -u github.com/swaggo/swag/cmd/[email protected]

I'm here with same problem.
@easonlin404

Same problem, this seems to be a pretty breaking change that also possibly conflicts with the examples in the repository currently.

Downgrading to 1.6.7 caused other issues.

@amanessinger @agzuniverse @psyhonut
Hi, here I explain how to use --parseDependency.
Before you run swag init --parseDependency -g main.go, you have to import all your dependent packages in your main.go.

//main.go

package main

import _ "rest-api/app/models/api/v1"

//api.go

package API

import models "rest-api/app/models/api/v1"

// @failure 400 {object} models.ResponseType
func MyAPI( ) {

}

@amanessinger
http.Header is from go SDK, so it is an internal type.
--parseInternal depends on --parseDependency.

//main.go

package main

import _ "net/http"

//api.go

package API

import "net/http"

// @sucess 400 {object} http.Header
func MyAPI( ) {

}

You need to run:
swag init --parseDependency --parseInternal -g main.go

--parseDepth has been included for higher performance from v1.6.9, so you can run:
swag init --parseDependency --parseInternal --parseDepth 1 -g main.go

En, the usage of --parseDependency and --parseInternal needs to be included in readme.md in future, I think.

@sdghchj
OK, now that absolutely makes sense. Yes, documenting that usage prominently would be a good idea. Thanks for the answer.

For those who need a quick solution:
go get -u github.com/swaggo/swag/cmd/[email protected]

thanks! it works for me

Was this page helpful?
0 / 5 - 0 ratings