Skip to content

Instantly share code, notes, and snippets.

@walker-tx
Created February 25, 2025 17:07
Show Gist options
  • Select an option

  • Save walker-tx/fdc528d8dc4e48769b3db5974b1a3f1c to your computer and use it in GitHub Desktop.

Select an option

Save walker-tx/fdc528d8dc4e48769b3db5974b1a3f1c to your computer and use it in GitHub Desktop.
Example of Valid Nested Discriminated Unions in OAS 3.1.0
openapi: 3.1.0
info:
title: Grocery API
description: A simple API for querying grocery produce
version: 1.0.0
servers:
- url: http://localhost:8080
paths:
/produce:
get:
summary: Get produce information
description: Returns a list of produce items (fruits and vegetables)
operationId: getProduce
responses:
'200':
description: Successful response
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Produce'
components:
schemas:
Produce:
oneOf:
- $ref: '#/components/schemas/Fruit'
- $ref: '#/components/schemas/Vegetable'
discriminator:
propertyName: produceType
mapping:
fruit: '#/components/schemas/Fruit'
vegetable: '#/components/schemas/Vegetable'
Fruit:
type: object
required: [produceType, fruitType]
properties:
produceType:
type: string
const: fruit
additionalProperties:
oneOf:
- $ref: '#/components/schemas/Apple'
- $ref: '#/components/schemas/Banana'
- $ref: '#/components/schemas/Orange'
discriminator:
propertyName: fruitType
mapping:
apple: '#/components/schemas/Apple'
banana: '#/components/schemas/Banana'
orange: '#/components/schemas/Orange'
Apple:
type: object
required: [variety, color, fruitType, produceType]
properties:
produceType:
type: string
const: fruit
fruitType:
type: string
const: "apple"
variety:
type: string
example: "Granny Smith"
color:
type: string
example: "green"
Banana:
type: object
required: [ripeness, length, fruitType, produceType]
properties:
produceType:
type: string
const: fruit
fruitType:
type: string
const: "banana"
ripeness:
type: string
enum: [green, yellow, spotted, brown]
length:
type: number
format: float
example: 7.5
Orange:
type: object
required: [seedless, origin, fruitType, produceType]
properties:
produceType:
type: string
const: fruit
fruitType:
type: string
const: "orange"
seedless:
type: boolean
origin:
type: string
example: "Florida"
Vegetable:
type: object
required: [produceType]
properties:
produceType:
type: string
const: vegetable
additionalProperties:
oneOf:
- $ref: '#/components/schemas/Carrot'
- $ref: '#/components/schemas/Broccoli'
- $ref: '#/components/schemas/Spinach'
discriminator:
propertyName: vegetableType
mapping:
carrot: '#/components/schemas/Carrot'
broccoli: '#/components/schemas/Broccoli'
spinach: '#/components/schemas/Spinach'
Carrot:
type: object
required: [length, organic, vegetableType, produceType]
properties:
produceType:
type: string
const: vegetable
vegetableType:
type: string
const: "carrot"
length:
type: number
format: float
example: 6.0
organic:
type: boolean
Broccoli:
type: object
required: [floretSize, stemLength, vegetableType, produceType]
properties:
produceType:
type: string
const: vegetable
vegetableType:
type: string
const: "broccoli"
floretSize:
type: string
enum: [small, medium, large]
stemLength:
type: number
format: float
example: 4.5
Spinach:
type: object
required: [babyLeaves, prewashed, vegetableType, produceType]
properties:
produceType:
type: string
const: vegetable
vegetableType:
type: string
const: "spinach"
babyLeaves:
type: boolean
prewashed:
type: boolean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment