Search code examples
swaggeropenapi

Mutually excluding properties in swagger


How do I indicate that in my_object you can have property_1 or property_2, but not both, and at least one of them must be present?

my_object:
  type: object
  properties:
    property_1:
      type: string
    property_2:
      type: string

Solution

  • You may want to switch to OpenAPI 3.0, which supports oneOf keyword to define mutually exclusive conditions:

    here is an example :

     my_object:
     type: object
     properties:
       property_1:
         type: string
       property_2:
        type: integer
     oneOf:
    - required: [property_1]
    - required: [property_2]