如果 swaggo 无法解析嵌套的结构体,可以尝试手动在注释中添加对应结构体的定义。例如:

type User struct {
    ID   int    'json:"id"'
    Name string 'json:"name"'
}

// @Success 200 {object} ApiResponse
type ApiResponse struct {
    Code    int         'json:"code"'
    Message string      'json:"message"'
    Data    interface{} 'json:"data"'
}

// @Router /api/user [get]
// @Summary Get user by ID
// @Produce json
// @Param id path int true 'User ID'
// @Success 200 {object} User
// @Failure 404 {object} ApiResponse
func GetUserByID(c *gin.Context) {
    // ...
}

在上面的例子中,我们手动在注释中添加了 'User' 结构体的定义,使得 swaggo 能够正确解析嵌套结构体。同样的,如果返回结构体中还有其他嵌套的结构体,也可以采用类似的方式手动添加定义。


原文地址: https://www.cveoy.top/t/topic/oo5m 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录