Search code examples
restswaggeropenapidata-modeling

How to exclude a property from a referenced schema?


I have an endpoint to create an address and one to update it. Describing this in an OpenAPI spec I'd like to use a component for the address so that I don't have to specify the address twice. Now the problem is, that the address object used for updating should include a property "id", but the one used for creating doesn't.

So basically, I'm looking for a way to describe the full address (incl. the id property) in the components section and then reference to the create endpoint, but excluding the "id" property there.


Solution

  • You can extend another type using the allOf keyword, as documented here

    In the example below, creationType has only a name property, but updateType has all the properties of creationType as well as an id property.

    Example:

    components:
      schemas:
        creationType:
          type: object
          properties:
            name:
              type: string
        updateType:
          allOf:
          - $ref: '#/components/schemas/creationType'
          - type: object
            required:
            - id
            properties:
              id:
                type: string