Last active
March 24, 2022 14:43
-
-
Save eak24/5b1e11ffbcc8644c3872922fbfbdcdfc to your computer and use it in GitHub Desktop.
OpenApi Polymorphism
This file contains 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: OAI Specification example for Polymorphism | |
version: 1.0.0 | |
paths: | |
/pet: | |
get: | |
responses: | |
'200': | |
description: desc | |
content: | |
application/json: | |
schema: | |
$ref: '#/components/schemas/Pet' | |
/reptile: | |
get: | |
responses: | |
'200': | |
description: desc | |
content: | |
application/json: | |
schema: | |
$ref: '#/components/schemas/Reptile' | |
/mypets: | |
get: | |
responses: | |
'200': | |
description: desc | |
content: | |
application/json: | |
schema: | |
$ref: '#/components/schemas/MyPets' | |
components: | |
schemas: | |
PetBase: | |
type: object | |
required: | |
- petType | |
properties: | |
petType: | |
type: string | |
Pet: | |
oneOf: | |
- $ref: '#/components/schemas/Cat' | |
- $ref: '#/components/schemas/Dog' | |
- $ref: '#/components/schemas/Reptile' | |
- $ref: '#/components/schemas/Lizard' | |
- $ref: '#/components/schemas/Snake' | |
discriminator: | |
propertyName: petType | |
CatBase: | |
allOf: | |
- $ref: '#/components/schemas/PetBase' | |
- type: object | |
properties: | |
name: | |
type: string | |
Cat: | |
allOf: | |
- $ref: '#/components/schemas/CatBase' | |
ReptileBase: | |
allOf: | |
- $ref: '#/components/schemas/PetBase' | |
- type: object | |
properties: | |
lungSize: | |
type: number | |
Reptile: | |
oneOf: | |
- $ref: '#/components/schemas/Lizard' | |
- $ref: '#/components/schemas/Snake' | |
- type: object | |
allOf: | |
- $ref: '#/components/schemas/ReptileBase' | |
discriminator: | |
propertyName: petType | |
DogBase: | |
allOf: | |
- $ref: '#/components/schemas/PetBase' | |
- type: object | |
properties: | |
bark: | |
type: string | |
Dog: | |
allOf: | |
- $ref: '#/components/schemas/DogBase' | |
LizardBase: | |
allOf: | |
- $ref: '#/components/schemas/ReptileBase' | |
- type: object | |
properties: | |
lovesRocks: | |
type: boolean | |
Lizard: | |
allOf: | |
- $ref: '#/components/schemas/LizardBase' | |
SnakeBase: | |
allOf: | |
- $ref: '#/components/schemas/ReptileBase' | |
- type: object | |
properties: | |
hasLegs: | |
type: boolean | |
Snake: | |
allOf: | |
- $ref: '#/components/schemas/SnakeBase' | |
MyPets: | |
oneOf: | |
- $ref: '#/components/schemas/Cat' | |
- $ref: '#/components/schemas/Lizard' | |
discriminator: | |
propertyName: petType | |
# per https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#discriminatorObject | |
# this discriminator must be included to use it as a hint to pick a schema | |
MyPetsNoDisc: | |
oneOf: | |
- $ref: '#/components/schemas/Cat' | |
- $ref: '#/components/schemas/Lizard' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment