所以我正在探索 swagger 以及它的檔案如何在 yaml 檔案中作業,之前在控制器檔案中使用 @swagger 嘗試過,它們作業得很好,但是當我嘗試將它們切換到 yaml 檔案以使它們更有條理時,除了來自獲取補丁,他們不想作業,因為它告訴我模型中的屬性是必需的:true。
讓我們以 PATCH 請求為例,我有一個名為 isDeleted 的屬性,默認情況下它是 false 并且是必需的,它會在呼叫時更改為 isDeleted: true 但它不起作用,因為 swagger 告訴我路徑 isDeleted 是必需的,但是當我將 required 更改為 false 時,它??可以作業,但我不想要那個 PS:在郵遞員中,它們只能在招搖時完美地作業我有這個問題。
招搖組態檔:
import path from 'path';
export const swaggerDefinition = {
openapi: '3.0.0',
info: {
title: 'test',
version: '1.0.0',
description: 'test ',
contact: {
name: 'ymena ',
},
},
servers: [
{
url: 'http://localhost:5000',
description: 'Development server',
},
],
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
},
};
export const swaggerOptions = {
swaggerDefinition,
apis: ['./src/resources/**/docs/*.yaml'],
};
在應用程式檔案中招搖:
this.express.use(
'/docs',
SwaggerUI.serve,
SwaggerUI.setup(swaggerJSDoc(swaggerOptions)),
);
這是 yaml 檔案:
paths:
/api/order/deleteOrder/{orderId}:
patch:
tags:
- Order
summary: change the status of a certain order
produces:
- application/json
security:
- bearerAuth: []
parameters:
- name: orderId
in: path
description: path parameter takes the order id to be deleted
required: true
schema:
type: string
example: 6352e63e29c4c5439a435d56
- name: isDeleted
in: body
description: will change the order status from false to true
required: true
schema:
type: object
properties:
isDeleted:
type: boolean
responses:
200:
description: successfull operation
content:
application/json:
schema:
$ref: '#/definitions/sucesssDeleteOrderResponse'
500:
description: Unsuccessfull operation
content:
application/json:
schema:
$ref: '#/definitions/FailureDeleteOrderResponse'
definitions:
deleteOrder:
type: object
properties:
isDeleted:
type: boolean
sucesssDeleteOrderResponse:
type: object
properties:
message:
type: string
default: 'Orders status changed successfully'
statut:
type: number
default: 200
FailureDeleteOrderResponse:
type: object
properties:
message:
type: string
default: 'Failed to change status order'
statut:
type: number
default: 500
這是模型中的屬性:
isDeleted: {
type: Boolean,
default: false,
required: true,
},
在下面你會發現招搖:

我得到的回復:

非常感謝您的寶貴時間。
uj5u.com熱心網友回復:
您在注釋中混合了 OpenAPI 3.0 和 2.0 語法。由于您使用openapi: '3.0.0':
- 請求正文應該使用
requestBody關鍵字而不是正文引數來定義。 - Schemas 應該在
components.schemassection 而不是definitions. - 操作不需要
produces關鍵字。回應媒體型別由 定義responses.<code>.content.<media-type>。
正確的版本是:
paths:
/api/order/deleteOrder/{orderId}:
patch:
tags:
- Order
summary: change the status of a certain order
security:
- bearerAuth: []
parameters:
- name: orderId
in: path
description: path parameter takes the order id to be deleted
required: true
schema:
type: string
example: 6352e63e29c4c5439a435d56
requestBody: # <---------
description: will change the order status from false to true
required: true
content:
application/json:
schema:
type: object
properties:
isDeleted:
type: boolean
responses:
200:
description: successfull operation
content:
application/json:
schema:
$ref: '#/components/schemas/sucesssDeleteOrderResponse'
500:
description: Unsuccessfull operation
content:
application/json:
schema:
$ref: '#/components/schemas/FailureDeleteOrderResponse'
components: # <---------
schemas: # <---------
deleteOrder:
type: object
properties:
isDeleted:
type: boolean
sucesssDeleteOrderResponse:
type: object
properties:
message:
type: string
default: 'Orders status changed successfully'
statut:
type: number
default: 200
FailureDeleteOrderResponse:
type: object
properties:
message:
type: string
default: 'Failed to change status order'
statut:
type: number
default: 500
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/524243.html
上一篇:無法讓POST請求正常作業
