Skip to content

Instantly share code, notes, and snippets.

@maelvls
Last active July 29, 2019 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maelvls/346e7fac80f6ce39ada109015847939d to your computer and use it in GitHub Desktop.
Save maelvls/346e7fac80f6ce39ada109015847939d to your computer and use it in GitHub Desktop.
OpenAPI example with discriminator (union types or 'inheritance')
openapi: 3.0.2
info:
title: Dogs and cats
description: some dogs and some cats
version: 1.0.0
paths:
/pets:
patch:
requestBody:
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
discriminator:
propertyName: pet_type
responses:
'200':
description: Updated
components:
schemas:
Pet:
type: object
required:
- pet_type
properties:
pet_type:
type: string
discriminator:
propertyName: pet_type
Dog: # "Dog" is a value for the pet_type property (the discriminator value)
allOf: # Combines the main `Pet` schema with `Dog`-specific properties
- $ref: '#/components/schemas/Pet'
- type: object
# all other properties specific to a `Dog`
properties:
bark:
type: boolean
breed:
type: string
enum: [Dingo, Husky, Retriever, Shepherd]
Cat: # "Cat" is a value for the pet_type property (the discriminator value)
allOf: # Combines the main `Pet` schema with `Cat`-specific properties
- $ref: '#/components/schemas/Pet'
- type: object
# all other properties specific to a `Cat`
properties:
hunts:
type: boolean
age:
type: integer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment