【OpenAPI】discriminator
discriminator はポリモーフィックなスキーマを表現するための仕組み。
同じエンドポイントで Cat や Dog など複数の型を返す場合、クライアント側がどのスキーマでパースすべきかを判別できるようにする。
discriminator は、JSON オブジェクトの中の特定のフィールドの値を見て、どのスキーマを使うかを決める。
propertyName :どのフィールドで型を判定するか
mapping :フィールドの値、実際のスキーマのマッピング
code:yaml
components:
schemas:
Pet:
type: object
required:
- petType
properties:
petType:
type: string
discriminator:
propertyName: petType
mapping:
cat: '#/components/schemas/Cat'
dog: '#/components/schemas/Dog'
Cat:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
huntingSkill:
type: string
description: The measured skill for hunting
Dog:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
packSize:
type: integer
description: Size of the pack the dog belongs to