嗯,標題很容易理解,但要詳細說明:
我有一個用 TypeScript 撰寫的 expressjs 應用程式。應用程式收到一個請求,其中的 JSON 正文是這樣的:
{
name: "name",
description: "description",
github: "github",
logo: "logo",
app: "app"
}
但問題是: app 屬性是可選的,因為只有在宣告了請求的情況下,我才會使用該屬性將物件插入到資料庫中。
我想知道是否有可能讓 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'
}
}
}
}
}
uj5u.com熱心網友回復:
除了路徑引數之外的所有引數都是可選的,除非它們的 required 屬性為 true
app:
type:string
required:true
請參閱標題Required and Optional Parameters為https://swagger.io/docs/specification/describing-parameters/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/314035.html
