I want to define PaymentMethod
as below. Is oneOf supported in swagger.yaml?
PaymentMethod:
oneOf:
- $ref: '#/definitions/NewPaymentMethod'
- $ref: '#/definitions/ExistPaymentMethod'
The ExistPaymentMethod
will have just id, and cardNumber
where NewPaymentMethod
will have no id
, but all other details, e.g. cardNumber
, cardholderName
, cardholderAddress
etc.
Here oneOf is a keyword construct in the JSON Schema, which is used to provide an array of criteria where, if exactly one of them is valid, the whole block is valid. As per the exampe above, objects having ( "email" AND "password" ) OR ( "username" AND "password" ) attributes are considered valid.
oneOf – validates the value against exactly one of the subschemas. allOf – validates the value against all the subschemas. anyOf – validates the value against any (one or more) of the subschemas.
Using the definition above the swagger code generator generates an object that accepts byte[] array as the body field new Job(). setBody(new byte[1]) . After converting the API definition to OpenAPI the definition for that object stayed the same but the openapi code generator now requires org. springframework.
OpenAPI 3.0 data types are based on an extended subset JSON Schema Specification Wright Draft 00 (aka Draft 5). The data types are described using a Schema object. To learn how to model various data types, see the following topics: Data Types.
oneOf
is supported in OpenAPI version 3 (openapi: 3.0.0
), but not in Swagger version 2 (swagger: '2.0'
).
PaymentMethod:
oneOf:
- $ref: '#/components/schemas/NewPaymentMethod'
- $ref: '#/components/schemas/ExistPaymentMethod'
GitHub issue ref: https://github.com/OAI/OpenAPI-Specification/issues/333
For a list of changes in OpenAPI 3.0 compared to 2.0, see: https://blog.readme.io/an-example-filled-guide-to-swagger-3-2/
What Swagger uses is only inspired by JSON Schema. They haven't deviated too much from JSON Schema, but they leave some things out, add some things, and change some behaviors. One of the things Swagger leaves out is oneOf
.
More details can be found at http://swagger.io/specification/#schemaObject
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With