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

Error using generics: cannot find type definition: T #1345

Open
acim opened this issue Oct 12, 2022 · 8 comments
Open

Error using generics: cannot find type definition: T #1345

acim opened this issue Oct 12, 2022 · 8 comments
Labels

Comments

@acim
Copy link

acim commented Oct 12, 2022

Describe the bug

In package httpx I have the following struct:

type Response[T any] struct {
    StatusCode int      `json:"-"`
    Data       *T       `json:"data,omitempty"`
    Errors     []string `json:"errors,omitempty"`
}

And in the handler in another package I have the following annotation:

// @success 200 {object} httpx.Response[[]person]

person is a struct within the same package where this annotation is.

When I try to run swag, I get the following error:

2022/10/12 21:05:08 ParseComment error in file /...redacted.../internal/handler/persons.go :cannot find type definition: T

To Reproduce
Steps to reproduce the behavior:

I believe this is also explained above.

Expected behavior
Swagger docs should be generated.

Your swag version
1.8.6

Your go version
1.19.2

Desktop (please complete the following information):

  • OS: Linux
@JohnAD
Copy link

JohnAD commented Oct 13, 2022

In a very similar vein (I'm also running Linux w go 1.19.2 and swag 1.8.6)

I have:

type JsonResponseStruct[T any] struct {
	Data  *DataObject[T] `json:"data,omitempty"`
	Error *ErrorObject   `json:"error,omitempty"`
}

type DataObject[T any] struct {
	CurrentItemCount int `json:"currentItemCount,omitempty"`
	Items            []T `json:"items"`
}

with

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

I also tried:

type JsonResponseStruct[T any] struct {
	Data  *T           `json:"data,omitempty"`
	Error *ErrorObject `json:"error,omitempty"`
}

type DataObject[T any] struct {
	CurrentItemCount int `json:"currentItemCount,omitempty"`
	Items            []T `json:"items"`
}

with

// @Success 200 {object} JsonResponseStruct[DataObject[string]]

in the hope that a more blunt reference would do it. Go and Gin were happy with both.

In both cases I got:

PS C:\Users\jd\source\repos\apigwquery> make    
swag init
2022/10/13 08:30:22 Generate swagger docs....
2022/10/13 08:30:22 Generate general API Info, search dir:./
2022/10/13 08:30:27 Generating $main.JsonResponseStruct-main_DataObject_string
2022/10/13 08:30:27 ParseComment error in file C:\Users\jd\source\repos\apigwquery\main.go :cannot find type definition: T
make: *** [Makefile:9: swag-build] Error 1

@JohnAD
Copy link

JohnAD commented Oct 13, 2022

Also took it to the extreme:

type JsonResponseStruct[T any] struct {
	Data  *T           `json:"data,omitempty"`
	Error *ErrorObject `json:"error,omitempty"`
}

type DataObject[T any] struct {
	CurrentItemCount int `json:"currentItemCount,omitempty"`
	Items            *T  `json:"items"`
}

and

// @Success 200 {object} JsonResponseStruct[DataObject[[]string]]

I'm kinda glad that didn't work as that would have been an ugly workaround. :)

OT: until a fix is found, I'm simply adding:

type JsonResponseString JsonResponseStruct[string]

and

// @Success 200 {object} JsonResponseString

which still creates an error:

swag init
2022/10/13 09:18:37 Generate swagger docs....
2022/10/13 09:18:37 Generate general API Info, search dir:./
2022/10/13 09:19:23 Generating main.JsonResponseString
2022/10/13 09:19:23 Generating $main.JsonResponseStruct-string
2022/10/13 09:19:23 Generating main.DataObject
2022/10/13 09:19:23 Type definition of type '*ast.IndexExpr' is not supported yet. Using 'object' instead. (%!s(<nil>))
2022/10/13 09:19:23 create docs.go at  docs/docs.go
2022/10/13 09:19:23 create swagger.json at  docs/swagger.json
2022/10/13 09:19:23 create swagger.yaml at  docs/swagger.yaml

but at least it defaults to a generic object and let's me compile. I'll live with not having the data structure in the swagger for now.

@JohnAD
Copy link

JohnAD commented Oct 13, 2022

Possibly useful to programmers: if I use a second reference to the same generic (but with a different sub-type), I get:

2022/10/13 09:44:56 Skipping 'main.DataObject', recursion detected.

It's like it can't handle generics within generics as it is losing reference.

@sdghchj sdghchj added the bug label Oct 15, 2022
sdghchj added a commit that referenced this issue Oct 15, 2022
sdghchj pushed a commit that referenced this issue Oct 15, 2022
sdghchj added a commit that referenced this issue Oct 15, 2022
sdghchj pushed a commit that referenced this issue Oct 15, 2022
sdghchj added a commit that referenced this issue Oct 15, 2022
sdghchj added a commit that referenced this issue Oct 15, 2022
sdghchj added a commit that referenced this issue Oct 15, 2022
sdghchj added a commit that referenced this issue Oct 16, 2022
* fix issue #1345 about generics

* fix issue #1345 about generics

* fix issue #1345 about generics

* fix issue #1345 about generics

* fix tests coverage

* fix tests coverage

* fix tests coverage

* no need to wrap schema by map

Co-authored-by: sdghchj <sdghchj@qq.com>
@sdghchj
Copy link
Member

sdghchj commented Oct 17, 2022

Could you please try the latest PR and give me some feedback?

@acim
Copy link
Author

acim commented Oct 17, 2022

I have already tried fix-generics branch and it worked. Shall I still try the latest PR?

@sdghchj
Copy link
Member

sdghchj commented Oct 18, 2022

I have already tried fix-generics branch and it worked. Shall I still try the latest PR?

No need.

@JohnAD How about you?

@ekeyme
Copy link

ekeyme commented Nov 30, 2022

I found that this behavior could also be reproduced in an environment with the new swag v1.8.8 on Linux go v1.19, BUT with swag v1.8.7 works well.

@raulvc
Copy link

raulvc commented Jan 25, 2023

still broken in v1.8.9 ... (linux go 1.19)
fixed in 1.8.10 (released a few minutes ago)

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

No branches or pull requests

5 participants