Swagger如何描述JSON请求体参数

huangapple go评论145阅读模式
英文:

Swagger How to describe JSON body parameter

问题

我正在尝试为我的 Rest API(Gin 框架)添加文档,并在尝试构建 JSON 请求体参数时遇到了一些问题。

目前,我有以下 API 描述操作:

  1. // @Summary logins a user
  2. // @ID login-user
  3. // @Accept json
  4. // @Produce json
  5. // @Param email formData string true "user email"
  6. // @Param password formData string true "user password"
  7. // @Success 200 {object} gin.H "login response"
  8. // @Failure 400 {object} gin.H "error response"
  9. // @Router /login [post]
  10. func (server *Server) handleLoginUser() gin.HandlerFunc {
  11. return func(ctx *gin.Context) {
  12. var req loginUserRequest
  13. if err := ctx.ShouldBindJSON(&req); err != nil {
  14. ctx.JSON(http.StatusBadRequest, utils.ErrorResponse(err))
  15. return
  16. }
  17. // 一些代码
  18. ctx.JSON(http.StatusOK, response)
  19. }
  20. }

当我通过 Swagger UI 提交数据时,我收到以下错误:

  1. {
  2. "error": "invalid character 'e' looking for beginning of value"
  3. }

此外,这是生成的 cURL 命令:

  1. curl -X 'POST' \
  2. 'http://localhost:8080/api/login' \
  3. -H 'accept: application/json' \
  4. -H 'Content-Type: application/json' \
  5. -d 'email=my%40email.com&password=password'

值得一提的是,当我在 Postman 中使用原始 JSON 请求体提交相同的数据时,它可以正常工作。这是一个典型的 JSON 示例(也是 loginUserRequest):

  1. {
  2. "email": "my@mail.com",
  3. "password": "password"
  4. }

由于我对 Swagger 还不熟悉,我相信问题与 Swagger 的 [attribute documentation] 中定义的 emailpassword 参数类型有关。

那么,我应该如何更好地描述 loginRequest 的 JSON 请求体呢?

英文:

I am trying to add documentation to my Rest API (Gin framework) and I stepped in some problems while trying to structure a JSON body parameter.

Currently, I have the following API description operations:

  1. // @Summary logins a user
  2. // @ID login-user
  3. // @Accept json
  4. // @Produce json
  5. // @Param email formData string true "user email"
  6. // @Param password formData string true "user password"
  7. // @Success 200 {object} gin.H "login response"
  8. // @Failure 400 {object} gin.H "error response"
  9. // @Router /login [post]
  10. func (server *Server) handleLoginUser() gin.HandlerFunc {
  11. return func(ctx *gin.Context) {
  12. var req loginUserRequest
  13. if err := ctx.ShouldBindJSON(&req); err != nil {
  14. ctx.JSON(http.StatusBadRequest, utils.ErrorResponse(err))
  15. return
  16. }
  17. // some code
  18. ctx.JSON(http.StatusOK, response)
  19. }
  20. }

When I submit the data through Swagger UI I get the following error:

  1. {
  2. "error": "invalid character 'e' looking for beginning of value"
  3. }

Also, this is the generated cURL:

  1. curl -X 'POST' \
  2. 'http://localhost:8080/api/login' \
  3. -H 'accept: application/json' \
  4. -H 'Content-Type: application/json' \
  5. -d 'email=my%40email.com&password=password'

It's worth to mention that whenever I submit the same data in Postman with body Raw JSON it works. This is what a usual JSON looks like (also, loginUserRequest):

  1. {
  2. "email": "my@mail.com",
  3. "password": "password"
  4. }

Since I am new to Swagger, I am pretty sure it's something related to the email & password param type defined on the Swagger's [attribute documentation].

So, how should I describe better the loginRequest JSON body?

答案1

得分: 5

这很容易,但我猜他们在文档中省略了这一部分。我只是按照以下方式更改了参数:

  1. // @Param loginUserRequest body loginUserRequest true "user password"

然后,在运行 swag init --parseDependency --parseInternal --parseDepth 1 时它就可以工作了。

英文:

It was pretty easy, but I guess they omitted that in the documentation. I just changed the parameter as following:

  1. // @Param loginUserRequest body loginUserRequest true "user password"

And then, when running swag init --parseDependency --parseInternal --parseDepth 1 it works.

huangapple
  • 本文由 发表于 2022年2月18日 07:29:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/71166668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定