Swagger中的多态性在鉴别器上产生奇怪的错误信息

时间:2018-02-10 10:35:28

标签: swagger-2.0

我正在编写一个带有GET的服务,该服务可以返回五种不同但密切相关的类型中的一种。由于用户想要一次搜索所有五种类型的选项,因此它必须是单次获取呼叫。我正在返回JSON,它可以轻松处理任何类型。

我正在尝试使用他们的多态性功能在Swagger中执行此操作,这是我以前从未尝试过的。我正在这样做,就像在示例中,除了“定义”而不是“组件/模式”。但我收到一条我无法理解的奇怪错误信息。招摇文件如下。错误说明了这一点:

定义中的模式错误['Event']。discriminator应该是字符串

它在第49行给出了这一点,即discriminator:

所以,我的两个问题是:我该如何解决?这甚至会给我我需要的东西吗?

swagger: '2.0'
info:
  description: RESTful API to retrieve Titles Metadata
  version: 1.0.0
  title: Swagger Mystery

schemes:
- https
paths:
  /event:
    get:
      operationId: getEvent
      summary: searches names
      description: |
        Search by names, across all types, or by a specific type.
      produces:
      - application/json
      parameters:
      - in: query
        name: title
        description: name to search for
        required: true
        type: string
      - in: query
        name: start
        required: false
        type: boolean
      - in: query
        name: type
        required: false
        type: string
        description: |
          May be "contest", "partner", "sponsor", or "dancer". If missing, will search for all types.
      responses:
        '200':
          description: search results
# I also don't know why I need to comment these out.
          # content:
          #   application/json:
          #     schema:
          #       type: array
          #       items:
          #         $ref: '#/definitions/Event'
        '400':
          description: bad input parameter
definitions:
  Event:
    type: object
    discriminator:
      propertyName: eventType
    properties:
      eventType:
        type: string
      id:
        type: integer
        format: int64
      name:
        type: string
      description:
        type: string
      contests:
        type: array
        items: 
          $ref: '#/definitions/Contest'
    required:
    - id
    - name
    - description
    - contests
    - eventType
  Contest:
    allOf:
    - $ref: '#/definitions/Event'
    - type: object
      properties:
        parentEvent:
          type: string
        venue:
          type: string
      required:
      - parentEvent
      - venue
  Dancer:
    allOf:
    - $ref: '#/definitions/Event'
    - type: object
      properties:
        eventInvitationDate:
          type: string
          format: date
        venue:
          type: string
      required:
      - eventInvitationDate
      - venue
  # Sponsor:
  #   allOf:
  #   - $ref: '#/definitions/Event'
  #   - type: object
  #     properties:
  #       invitationDate:
  #         type: string
  #         format: date
  #       parentEvent:
  #         type: string
  #       partners:
  #         type: array
  #         items:
  #           $ref: '#/definitions/Partner'
  Partner:
    allOf:
    - $ref: '#/definitions/Event'
    - type: object
      properties:
        invitationDate:
          type: string
          format: date
        parentEvent:
          type: string
        venue:
          type: string
      required:
      - invitationDate
      - parentEvent
      - venue
# two problems:
# 1. Schema error at definitions['Event'].discriminator
#    should be string on line 49 (discriminator:)
# 2. Resolver error:
#    e is undefined
#    (no line number)
#    (This error goes away when I comment out Sponsor.)

1 个答案:

答案 0 :(得分:2)

发生错误是因为您正在混合使用OpenAPI 2.0和3.0语法。

您的规范是swagger: '2.0',但以下是3.0语法:

discriminator:
  propertyName: eventType

在OpenAPI 2.0中,discriminator的值是属性名称:

discriminator: eventType

此外,OpenAPI 2.0假定discriminator属性的可能值(在本例中为eventType)与definitions中的模型名称完全相同。那就是:

  • 如果eventType可以是eventpartner等小写,则模型名称也必须为小写 - event,而不是Event
  • 如果eventType是某些代码,例如epd等,则型号名称必须为epd等,而不是EventPartner等。

查看有关OpenAPI 2.0中discriminator用法的更多示例的问题: