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

net/goai: if there're no required parameters in request, the generated requestBody field should not be required in api.json #3664

Closed
qinains opened this issue Jun 26, 2024 · 6 comments · Fixed by #3796
Assignees
Labels
bug It is confirmed a bug, but don't worry, we'll handle it. done This issue is done, which may be release in next version. enhancement

Comments

@qinains
Copy link

qinains commented Jun 26, 2024

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()
  1. api文件如下:
    api/v1/logout.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
}
@qinains qinains added the bug It is confirmed a bug, but don't worry, we'll handle it. label Jun 26, 2024
@qinains qinains changed the title net/goai: issue The openAPI document api.json generated by gf has a problem with the properties={} parameter setting in each field. net/goai: issue xxxReq没有必须的参数,requestBody不必必填 Jun 26, 2024
@shuqingzai
Copy link

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

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

@Issues-translate-bot
Copy link

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
Copy link
Author

qinains commented Jun 28, 2024

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

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

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

@Issues-translate-bot
Copy link

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

@gqcn gqcn changed the title net/goai: issue xxxReq没有必须的参数,requestBody不必必填 net/goai: if there're no required parameters in request, the generated requestBody field should not be required in api.json Sep 12, 2024
@gqcn
Copy link
Member

gqcn commented Sep 23, 2024

@qinains @shuqingzai 这里似乎更优雅的是不要在OpenAPIv3中默认required参数为true,并且允许用户自定义这个required标签。我这边处理一下。

@gqcn gqcn self-assigned this Sep 23, 2024
@Issues-translate-bot
Copy link

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


@qinains @shuqingzai What seems more elegant here is not to default the required parameter to true in OpenAPIv3, and allow users to customize the required tag. I'll handle it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it. done This issue is done, which may be release in next version. enhancement
Projects
None yet
4 participants