繁体   English   中英

如何使 Swagger UI 接受带有可选字段的请求正文

[英]How to make Swagger UI accepts request body with optional field

好吧,标题很容易理解,但是要详细说明:

我有一个用 TypeScript 编写的 expressjs 应用程序。该应用程序收到一个请求,其中 JSON 正文如下所示:

{
    name: "name",
    description: "description",
    github: "github",
    logo: "logo",
    app: "app"
}

但问题是:app 属性是可选的,因为只有在声明了请求时,我才会将 object 插入数据库中。

我想知道是否可以让 Swagger 接受那个可选的东西,如果可以,该怎么做?

编辑:根据@Anatoly 的要求,这是 swagger 路由定义:

post: {
    summary: 'Create a new project',
    tags: ['projects'],
    requestBody: {
        content: {
            'application/json': {
                schema: {
                    $ref: '#/components/schemas/Project'
                }
            }
        }
    }
},
components: {
    schemas: {
        Project: {
            type: 'object',
            properties: {
                name: {
                    type: 'string'
                },
                description: {
                    type: 'string'
                },
                github: {
                    type: 'string'
                },
                logo: {
                    type: 'string'
                },
                app: {
                    type: 'string'
                }
            }
        }
    }
}

除了路径参数之外的所有参数都是可选的,除非它们具有 required 属性为真

app:
  type:string
  required:true

请参阅 https 中的“ Required and Optional Parameters ”标题://swagger.io/docs/specification/describing-parameters/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM