gogf / gf

GoFrame is a modular, powerful, high-performance and enterprise-class application development framework of Golang.
https://goframe.org
MIT License
11.09k stars 1.52k forks source link

net/goai: issue xxxReq没有必须的参数,requestBody不必必填 #3664

Open qinains opened 1 week ago

qinains commented 1 week ago

Go version

go version go1.22.0 windows/amd64

GoFrame version

2.7.1

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

  1. 修改openAPI的UI 编辑:internal\cmd\cmd.go
    
    const MySwaggerUITemplate = `
    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <title>Cloud Pet</title>
    </head>
    <body>
    <div id="openapi-ui-container" spec-url="{SwaggerUIDocUrl}" theme="light"></div>
    <script src="https://cdn.jsdelivr.net/npm/openapi-ui-dist@latest/lib/openapi-ui.umd.js"></script>
    </body>
    </html>
    `
    s.SetSwaggerUITemplate(MySwaggerUITemplate)

// 启动Http Server s.Run()

2. api文件如下:
api/v1/logout.go
```go
package v1

import (
    "github.com/gogf/gf/v2/frame/g"
)

type TokenReq struct {
    Authorization string `dc:"token验证信息,格式“Bearer {token}”" d:"Bearer {token}" in:"header"`
}

type LogoutReq struct {
    g.Meta `method:"post" path:"/logout" summary:"用户退出登录" tags:"auth"`
    TokenReq
}

type LogoutRes struct {
}
  1. 启动server:gf run main.go

What did you see happen?

生成的api.json文件为:

"/logout": {
            "post": {
                "parameters": [
                    {
                        "description": "token验证信息,格式“Bearer {token}”",
                        "in": "header",
                        "name": "Authorization",
                        "schema": {
                            "default": "Bearer {token}",
                            "description": "token验证信息,格式“Bearer {token}”",
                            "format": "string",
                            "type": "string"
                        }
                    }
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "properties": {},
                                "type": "object"
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "content": {
                            "application/json": {
                                "schema": {
                                    "properties": {
                                        "code": {
                                            "description": "错误码",
                                            "format": "int",
                                            "type": "integer"
                                        },
                                        "msg": {
                                            "description": "提示信息",
                                            "format": "string",
                                            "type": "string"
                                        },
                                        "data": {
                                            "description": "返回数据",
                                            "properties": {},
                                            "type": "object"
                                        }
                                    },
                                    "type": "object"
                                }
                            }
                        },
                        "description": ""
                    }
                },
                "summary": "用户退出登录",
                "tags": [
                    "auth"
                ]
            }
        },

image

What did you expect to see?

如果xxxReq没有必须的参数,生成的api.json的requestBody的required是否应该为false?如下

...
"requestBody": {
    "required": false,
    "content": {
        "application/json": {
            "schema": {
                "properties": {},
                "type": "object"
            }
        }
    }
},
...

image

P.S. 如果设置添加“mime:"application/x-www-form-urlencoded"”,就显示正常。(但是上面的例子,我又想让mime为application/json,可能以后添加其他参数) type LogoutReq struct { g.Meta mime:"application/x-www-form-urlencoded" method:"post" path:"/logout" summary:"用户退出登录" tags:"auth" TokenReq }

shuqingzai commented 1 week ago

这个问题,我目前的解决方式是在嵌入结构体中加指针引用,你可以试试

type LogoutReq struct {
    g.Meta `method:"post" path:"/logout" summary:"用户退出登录" tags:"auth"`

        *TokenReq
}
Issues-translate-bot commented 1 week ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


My current solution to this problem is to add pointer references to the embedded structure. You can try it.

typeLogoutReq struct {
g.Meta `method:"post" path:"/logout" summary:"User logs out" tags:"auth"`

        *TokenReq
}
qinains commented 1 week ago

这个问题,我目前的解决方式是在嵌入结构体中加指针引用,你可以试试

type LogoutReq struct {
  g.Meta `method:"post" path:"/logout" summary:"用户退出登录" tags:"auth"`

        *TokenReq
}

但是依然有以下问题:body必填。(期待的结果是body不必填) image

Issues-translate-bot commented 1 week ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


My current solution to this problem is to add pointer references to the embedded structure. You can try it.

type LogoutReq struct {
g.Meta `method:"post" path:"/logout" summary:"User logs out" tags:"auth"`

*TokenReq
}

But there are still the following problems: body is required. (The expected result is that the body does not need to be filled in) image