Swagger 模式中的 oneOf 不起作用

oneOf in Swagger schema does not work

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

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

ExistPaymentMethod 将只有 id,而 cardNumber 其中 NewPaymentMethod 将没有 id,但所有其他详细信息,例如cardNumbercardholderNamecardholderAddress

Swagger 使用的只是受 JSON Schema 的启发。他们并没有偏离 JSON Schema 太多,但是他们遗漏了一些东西,添加了一些东西,并改变了一些行为。 Swagger 遗漏的一件事是 oneOf.

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

找到更多详细信息

oneOf 在 OpenAPI 版本 3 (openapi: 3.0.0) 中受支持,但在 Swagger 版本 2 (swagger: '2.0') 中不受支持。

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/

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

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

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

这是一个更有用的 link - https://github.com/swagger-api/swagger-ui/issues/3803