POST“尝试一下”中扬鞭3.0 UI不起作用

问题描述 投票:0回答:1

我已成立了一个扬鞭文档,并在UI看起来OK。然而,当我用“尝试一下”功能,我收到以下错误:

SyntaxError: Unexpected token # in JSON at position 0

我扬鞭文档的相关部分看起来是这样的:

post:
      summary: Creates a new cinema
      operationId: addCinema
      consumes: [ application/json ]
      parameters:
        - name: name
          in: body
          description: Name of the cinema
          schema:
            type: string
          example: "Bad cinema"
        - name: description
          in: body
          description: Description of the cinema
          schema:
            type: string
          example: "A pretty terrible cinema"
        - name: capacity
          in: body
          description: Capacity of the cinema
          schema:
            type: number
          example: 100
      responses:
        201:
          description: Creates the cinema
        400:
          description: 'Invalid request'

任何想法,为什么我看到这个错误?我想,也许是身体发出的,而不是JSON HTML,但我不明白,为什么那会是什么?

swagger swagger-ui
1个回答
1
投票

你的定义是无效的,它有OpenAPI的2.0和3.0的关键字组合。

在所述OpenAPI 2.0(swagger: '2.0'),可以有仅一个in: body参数,和如果所述主体是一个对象,该参数schema应该定义对象结构。

所以,如果你张贴此JSON:

{
  "name": "foo",
  "description": "bar",
  "capacity": 100
}

你的身体参数应该是这样的:

      parameters:
        - in: body
          name: cinema
          required: true
          schema:
            type: object
            properties:
              name:
                type: string
                example: Bad cinema
              description:
                type: string
                example: A pretty terrible cinema
              capacity:
                type: integer
                example: 100

或者如果你提取联架构成definitions命名模式:

      parameters:
        - in: body
          name: cinema
          required: true
          schema:
            $ref: '#/definitions/Cinema'

....

definitions:
  Cinema:
    type: object
    properties:
      name:
        type: string
        example: Bad cinema
      description:
        type: string
        example: A pretty terrible cinema
      capacity:
        type: integer
        example: 100
© www.soinside.com 2019 - 2024. All rights reserved.