-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Comments
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:
|
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:
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. |
Possibly useful to programmers: if I use a second reference to the same generic (but with a different sub-type), I get:
It's like it can't handle generics within generics as it is losing reference. |
* 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 <[email protected]>
Could you please try the latest PR and give me some feedback? |
I have already tried fix-generics branch and it worked. Shall I still try the latest PR? |
No need. @JohnAD How about you? |
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. |
|
Describe the bug
In package httpx I have the following struct:
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):
The text was updated successfully, but these errors were encountered: