cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Highlighted
New Member

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:
          type: string
        owner:
          type: string
      required:
        - id
        - name
        - owner

This works fine for a POST request's body. However, if I want to use this schema for a PATCH request's body, it wouldn't make sense as now the request body is required to have all properties in it. So, my question is, is it possible to make a schema that inherits from this schema, but with all required properties optional?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Moderator

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

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

Helen Kosova
SmartBear Documentation Team Lead
________________________

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

View solution in original post

3 REPLIES 3
Highlighted
New Contributor

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

While looking for same answer I've found your post. Did you fond an answer what's the best practice to define PATCH?

Highlighted
Moderator

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

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

Helen Kosova
SmartBear Documentation Team Lead
________________________

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

View solution in original post

Highlighted
New Contributor

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

Great, thank you.

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors