Last active
July 29, 2019 09:38
-
-
Save maelvls/346e7fac80f6ce39ada109015847939d to your computer and use it in GitHub Desktop.
OpenAPI example with discriminator (union types or 'inheritance')
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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