Swagger不想正常工作,因为我的模型属性是真的

lnlaulya  于 2022-11-04  发布在  Node.js
关注(0)|答案(1)|浏览(151)

所以我在探索swagger以及它的文档如何在yaml文件中工作,之前在控制器文件中尝试过@swagger,它们工作得很好,但是当我试图将它们切换到yaml文件以使它们更有组织时,除了从get patch中,它们不想工作,因为它告诉我模型中的属性是必需的:是的。
让我们以PATCH请求为例,我有一个名为isDeleted的属性,默认情况下该属性为false,并且是必需的,它将被更改为isDeleted:调用时为true,但它不起作用,因为swagger告诉我路径isDeleted是必需的,但当我将required更改为false时,它起作用,但我不希望PS:在 Postman ,他们工作完美,只有在昂首阔步我有这个问题。
Swagger配置文件:

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,
        },

下面你会发现swagger:

我得到的回答是:

非常感谢您抽出时间。

jk9hmnmh

jk9hmnmh1#

您在注解中混淆了OpenAPI 3.0和2.0的语法。
1.应使用requestBody关键字而不是body参数来定义请求正文。
1.结构描述应该在components.schemas区段中,而不是definitions
1.操作不需要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

相关问题