Swagger:如何表示类型为一个类型列表的属性?

时间:2015-10-09 11:45:48

标签: swagger jsonschema json-schema-validator

我有一个对象,其属性是一个对象,其类型将是一个类型列表。 Swagger编辑器拒绝了我的所有尝试,但出现以下错误:

Data does not match any schemas from 'anyOf'
Jump to line 43
Details
Object
code: "ANY_OF_MISSING"
message: "Data does not match any schemas from 'anyOf'"
path: Array [7]
inner: Array [2]
level: 900
type: "Swagger Error"
description: "Data does not match any schemas from 'anyOf'"
lineNumber: 43

完整的招摇规范文件如下(相关字段为DataSetsInquiryRsp.dataSets.dataSet):

swagger: '2.0'
info:
  title: My API
  description: My Awesome API
  version: 1.0.0
paths:
  /dataSetsInquiry:
    get:
      description: Retrieve one or more data-sets.
      parameters:
        - name: ids
          in: query
          description: List of identifiers of requested data-sets.
          required: true
          type: array
          items:
            type: string
      responses:
        '200':
          description: Requested data-sets.
          schema:
            $ref: '#/definitions/DataSetsInquiryRsp'
        default:
          description: Unexpected error
          schema:
            $ref: '#/definitions/Error'
definitions:
  DataSetsInquiryRsp:
    type: object
    additionalProperties: false
    properties:
      sessionIdentifier:
        description: Identifier of the secure session with the server.
        type: number
      dataSets:
        type: object
        additionalProperties: false
        properties:
          id:
            type: string
          dataSet:
            type: array
            items:
              oneOf:
              - $ref: '#/definitions/Customer'
              - $ref: '#/definitions/Merchant'
  Customer:
    type: object
    additionalProperties: false
    properties:
      firstName:
        description: First name of the customer
        type: string
      lastName:
        description: Last name of the customer
        type: string
  Merchant:
    type: object
    additionalProperties: false
    properties:
      code:
        description: Code the Merchant.
        type: string
      name:
        description: Name of the Merchant.
        type: string

1 个答案:

答案 0 :(得分:2)

嗯,问题只是Swagger不支持oneOff这一事实。事实上,Swagger支持Json-Schema的子集并添加一些内容(例如数据类型file)。

这里的错误是Swagger返回的错误。它无济于事。到目前为止,我使用过的所有Json-Schema验证程序都是如此:is-my-json-validjsen