POST"尝试一下"在Swagger 3.0 UI中不起作用

时间:2018-03-05 19:31:49

标签: swagger swagger-ui

我已经设置了一个Swagger文档,它在UI中看起来很好。但是,当我使用"试试看"功能,我收到以下错误:

SyntaxError: Unexpected token # in JSON at position 0

我的Swagger文档的相关部分如下所示:

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'

知道为什么我会看到这个错误吗?我想也许正在发送HTML而不是JSON,但我无法弄清楚为什么会这样?

1 个答案:

答案 0 :(得分:0)

您的定义无效,它包含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