【问题标题】:Swagger: how to represent a property whose type is oneOf a list of types?Swagger:如何表示类型为类型列表之一的属性?
【发布时间】:2015-10-09 11:45:48
【问题描述】:

我有一个对象,它的属性是一个类型为类型列表之一的对象。我的所有尝试都被 Swagger Editor 拒绝,并出现以下错误:

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

完整的swagger规范文件如下(有问题的字段是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

【问题讨论】:

  • 我的问题是在 2015 年,您指出的问题被我重复了在 2016 年被问过。什么是重复的?另外,我已经回答了问题并说明了我遇到的错误的来源!
  • 该问题和答案有更多细节,具体来说,OpenAPI 3.0 现在支持oneOf/anyOf。只需将其链接到此处以供将来的访问者使用。
  • 将我的问题标记为新问题的重复是不准确的。最好更新我的答案并参考对新问题的回复。或者使用其他准确的机制。

标签: swagger jsonschema json-schema-validator


【解决方案1】:

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

这里不好的是Swagger 返回的错误。它没有帮助匹配。到目前为止,我使用过的所有 Json-Schema 验证器都是这种情况:is-my-json-validjsen

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    相关资源
    最近更新 更多