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

swagger测试tag时,无法调用任何结构,参入参数无法解析,是否是注释问题? #3

Closed
minibear2333 opened this issue Jun 17, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@minibear2333
Copy link
Member

http://localhost:8000/swagger/index.html

@minibear2333 minibear2333 added the bug Something isn't working label Jun 17, 2021
@LVM-23
Copy link

LVM-23 commented Jun 19, 2021

1、\app\form.go 里面对应接口入参封装 使用的是err := c.ShouldBind(v) ,这个的对应 表单提交类型,解析不了swagger提交的json对象类型,需要改成 err := c.ShouldBindJSON(v),就可以了。

2、就是封装入参的对象得加个对json解析的映射,例如json:"name",
例:
type CreateTagRequest struct {
Name string form:"name" json:"name" binding:"required,min=2,max=100"
CreatedBy string form:"created_by" json:"created_by" binding:"required,min=2,max=100"
State uint8 form:"state,default=1" json:"state" binding:"oneof=0 1"
}

3、swagger注释修改, internal\routers\api\v1\tag.go

例如:
// @summary 新增标签
// @produce json
// @accept json
// @param data body service.CreateTagRequest true "数据封装"
// @success 200 {object} model.Tag "成功"
// @failure 400 {object} errcode.Error "请求错误"
// @failure 500 {object} errcode.Error "内部错误"
// @router /api/v1/tags [post]
func (t Tag) Create(c *gin.Context) { .......

还好是写得比较多java 接口,对swager还算熟悉,看着原接口每个参数都标有in body,觉得怪怪的,然后最后一个参数的内容覆盖前面的参数在请求体里面只有最后一个参数值,发现了问题。

跟着熊哥学go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants