swaggo / swag

Automatically generate RESTful API documentation with Swagger 2.0 for Go.
MIT License
10.46k stars 1.19k forks source link

Generics support cross package #1770

Closed zhanjiejun closed 6 months ago

zhanjiejun commented 6 months ago

To Reproduce

package response

type Response[T any] struct {
    Code    string `json:"code"`
    Data    T      `json:"data"`
    Message string `json:"message"`
}

type PageResponse[T any] struct {
    Total     int64 `json:"total"`
    TotalPage int   `json:"totalPage"`
    Rows []T `json:"rows"`
}
package resp

type UserPageResponse struct {
    Id       int    `json:"id"`
    Username string `json:"username"`
}
// @Success 200 {object} response.Response[response.PageResponse[resp.UserPageResponse]]

swag init error: 2024/03/04 11:04:20 Error parsing type definition 'response.PageResponse-resp_UserPageResponse': rows: cannot find type definition: resp.UserPageResponse 2024/03/04 11:04:20 Error parsing type definition 'response.Response-response_PageResponse-resp_UserPageResponse': data: response.PageResponse-resp_UserPageResponse: rows: cannot fin d type definition: resp.UserPageResponse

Your swag version 1.16.3

Your go version 1.21.6

Additional context But if I put UserPageResponse in package response, it's ok

package response

type Response[T any] struct {
    Code    string `json:"code"`
    Data    T      `json:"data"`
    Message string `json:"message"`
}

type PageResponse[T any] struct {
    Total     int64 `json:"total"`
    TotalPage int   `json:"totalPage"`
    Rows []T `json:"rows"`
}

type UserPageResponse struct {
    Id       int    `json:"id"`
    Username string `json:"username"`
}
// @Success 200 {object} response.Response[response.PageResponse[response.UserPageResponse]]
janbiasi commented 6 months ago

Encountering the same issue with Swag 1.16.3 and Go 1.22

sdghchj commented 6 months ago

Show your whole file content. I just guess you have not added import xxx/xxx/resp.

zhanjiejun commented 6 months ago

Show your whole file content. I just guess you have not added import xxx/xxx/resp.

Thanks ~ It's ok when I add import _ xxx/xxx/resp .