Sobes.tech
Middle

What do the keys allOf, oneOf, anyOf do in OpenAPI? Explain with an example schema with allOf.

sobes.tech AI

Answer from AI

In OpenAPI, the keys allOf, oneOf, anyOf are used to combine multiple schemas and describe complex data structures.

  • allOf — the object must satisfy all the listed schemas simultaneously (logical AND).
  • oneOf — the object must satisfy exactly one of the listed schemas (exclusive OR).
  • anyOf — the object must satisfy at least one of the listed schemas (logical OR).

Example with allOf:

components:
  schemas:
    Address:
      type: object
      properties:
        street:
          type: string
        city:
          type: string
    User:
      allOf:
        - type: object
          properties:
            id:
              type: integer
            name:
              type: string
        - $ref: '#/components/schemas/Address'

Here, the User schema combines properties from two parts: its own fields id and name, and fields from the Address schema (street and city). Thus, the User object must have all these fields simultaneously.

What do the keys allOf, oneOf, anyOf do in OpenAPI… - sobes.tech