Skip to content

Instantly share code, notes, and snippets.

@eak24
Last active March 24, 2022 14:43
Show Gist options
  • Save eak24/5b1e11ffbcc8644c3872922fbfbdcdfc to your computer and use it in GitHub Desktop.
Save eak24/5b1e11ffbcc8644c3872922fbfbdcdfc to your computer and use it in GitHub Desktop.
OpenApi Polymorphism
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