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

Version 1.6.9 - cannot find type definition #817

Closed
berayb opened this issue Oct 26, 2020 · 19 comments
Closed

Version 1.6.9 - cannot find type definition #817

berayb opened this issue Oct 26, 2020 · 19 comments

Comments

@berayb
Copy link

berayb commented Oct 26, 2020

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.

@amanessinger
Copy link

amanessinger commented Oct 27, 2020

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.

@berayb
Copy link
Author

berayb commented Oct 31, 2020

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

@ghost
Copy link

ghost commented Nov 2, 2020

I'm here with same problem.
@easonlin404

peanut-cc added a commit to peanut-cc/gin-admin that referenced this issue Nov 5, 2020
swag > 1.67 version need add --parseDependency
swaggo/swag#817
@agzuniverse
Copy link
Contributor

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.

@sdghchj
Copy link
Member

sdghchj commented Nov 20, 2020

@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( ) {

}

@sdghchj
Copy link
Member

sdghchj commented Nov 20, 2020

@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

@sdghchj
Copy link
Member

sdghchj commented Nov 20, 2020

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

@amanessinger
Copy link

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

@seven-jinxin
Copy link

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

thanks! it works for me

@mr-pascal
Copy link

mr-pascal commented Feb 20, 2021

@berayb

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

Unfortunately doesn't really work for me.
Good thing: It compiles
Bad thing: The swagger documentation isn't found anymore when trying to access it as before:
image

That is how I set up the route for the swagger documentation after hitting swag init

	app.Get("/swagger/*", swagger.New(swagger.Config{
		URL:         "/swagger/doc.json",
		DeepLinking: false,
	}))

Serves correctly with [email protected]

For me the swag init --parseDependency did the job 👍🏻 even though I very much dislike it that it takes sooo much longer to generate the docs, but at least it is being generated

@eduardo-mior
Copy link

swag init --parseDependency --parseInternal works for my

@yunlong-tang
Copy link

@Abszissex I add --parseDepth 1 to reduce the parsing time.

@IgorHalfeld
Copy link

swag init --parseDependency --parseInternal --parseDepth 1 worked like a charm

@rcrick
Copy link

rcrick commented Sep 9, 2021

swag init --parseDependency --parseInternal --parseDepth 1 worked like a charm

It work for me, thx!

@ubogdan ubogdan closed this as completed Sep 28, 2021
@zeromsi
Copy link

zeromsi commented Nov 8, 2021

swag init --parseDependency --parseInternal

@zx1986
Copy link

zx1986 commented Nov 8, 2021

swag init --parseDependency --parseInternal --parseDepth 1 not work for me :-(

golang:1.16

@lixd
Copy link

lixd commented Dec 28, 2021

I meet the same question:

// @Success      200  {object}  srv.Result{data=respmodel.AnswerItem}

and swag init error:

cannot find type definition: respmodel.AnswerItem

I tried --parseInternal depends on --parseDependency. it does not work for me.

finally, I find the true reason, There has some same name struct in respmodel packages

such as user project has a respmodel package def an AnswerItem, and admin project has a respmodel package def an AnswerItem.And those two projects are in one repo.

I changed those names, it works。

hope this has helped.

plops added a commit to plops/cl-golang-generator that referenced this issue Sep 18, 2022
it is quite difficult to make it find type definitions
swaggo/swag#817
@geoherna
Copy link

I meet the same question:

// @Success      200  {object}  srv.Result{data=respmodel.AnswerItem}

and swag init error:

cannot find type definition: respmodel.AnswerItem

I tried --parseInternal depends on --parseDependency. it does not work for me.

finally, I find the true reason, There has some same name struct in respmodel packages

such as user project has a respmodel package def an AnswerItem, and admin project has a respmodel package def an AnswerItem.And those two projects are in one repo.

I changed those names, it works。

hope this has helped.

This solved the issue for me. Thank you @lixd

@Vkutovoy92
Copy link

@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( ) {

}

Thanks a lot!!!!

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