Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

oneOf in Swagger schema does not work

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.

like image 416
fastcodejava Avatar asked Apr 26 '16 17:04

fastcodejava


People also ask

What is oneOf in JSON schema?

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.

What is oneOf in swagger?

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.

How does swagger define byte array?

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.

What is schema in swagger?

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.


2 Answers

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/

like image 155
Evan Torkos Avatar answered Sep 20 '22 19:09

Evan Torkos


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

like image 24
Jason Desrosiers Avatar answered Sep 18 '22 19:09

Jason Desrosiers