Swagger架构中的oneOf不起作用

时间:2016-04-26 17:52:57

标签: yaml swagger jsonschema

我想定义PaymentMethod,如下所示。是否支持swagger.yaml?

PaymentMethod:
      oneOf:
        - $ref: '#/definitions/NewPaymentMethod'
        - $ref: '#/definitions/ExistPaymentMethod'

ExistPaymentMethod只有id,cardNumber NewPaymentMethod没有id,但所有其他细节,例如cardNumbercardholderNamecardholderAddress等。

3 个答案:

答案 0 :(得分:18)

OpenAPI版本3(oneOf)支持

openapi: 3.0.0,但Swagger版本2(swagger: '2.0')不支持。{/ p>

PaymentMethod:
  oneOf:
    - $ref: '#/components/schemas/NewPaymentMethod'
    - $ref: '#/components/schemas/ExistPaymentMethod'

GitHub问题参考:https://github.com/OAI/OpenAPI-Specification/issues/333

有关OpenAPI 3.0与2.0相比的更改列表,请参阅:https://blog.readme.io/an-example-filled-guide-to-swagger-3-2/

答案 1 :(得分:7)

Swagger使用的仅受JSON Schema的启发。它们与JSON Schema没有太大的偏差,但它们会遗漏一些东西,添加一些东西,并改变一些行为。 Swagger遗漏的一件事是oneOf

可在http://swagger.io/specification/#schemaObject

找到更多详情

答案 2 :(得分:0)

swagger 2.0不支持OneOf,anyOf和其他类似的指令,但在Open API 3.0规范中受支持。

您需要将Swagger 2.0文件转换为Open API 3.0文件。

这是链接- https://blog.runscope.com/posts/tutorial-upgrading-swagger-2-api-definition-to-openapi-3

这是另一个有用的链接- https://github.com/swagger-api/swagger-ui/issues/3803