Forum Discussion

peat-psuwit's avatar
peat-psuwit
New Member
6 years ago

How to make an "all optional" schema from a schema in OpenAPI 3

Suppose I have an OpenAPI 3.0 schema like this: components: schemas: Device: type: object properties: id: type: string readOnly: true name: ...
  • HKosova's avatar
    HKosova
    5 years ago

    Required properties of a base schema cannot be made optional. In other words, you can only add constraints but not remove them.

     

    You need to do it the other way round - define a base schema with optional properties, then "inherit" from that schema and mark the properties in question as required.

    components:
      schemas:
        # Schema for PATCH
        BaseDevice:
          type: object
          properties:
            id:
              type: string
              readOnly: true
            name:
              type: string
            owner:
              type: string
    
        # Schema for POST
        Device:
          allOf:
            - $ref: '#/components/schemas/BaseDevice'
          required:
            - id
            - name
            - owner