Optional discriminator property #4319
Replies: 4 comments 6 replies
-
as discriminator need to stand for 3.X , the only allowed is to releax constrains , so it could be , |
Beta Was this translation helpful? Give feedback.
-
Discussed in the Jan 23 TDC meeting and next step is for me to add actual examples. |
Beta Was this translation helpful? Give feedback.
-
Yes
In my proposal, I suggested that an optional discriminator property implicitly adds a variant to a
Exactly. That is the intent. It shouldn't map to any of them. But perhaps your objection is that the empty schema being "implicit" means that you can't just ignore the So maybe something like this: components:
schemas:
Pet:
type: object
discriminator:
propertyName: petType
mapping:
"Cat": #/components/schemas/Cat
"Dog": #/components/schemas/Dog
default: #/components/schemas/AnyPet
properties:
name:
type: string
petType:
type: string
required:
- name
oneOf:
- $ref: #/components/schemas/Cat
- $ref: #/components/schemas/Dog
- $ref: #/components/schemas/AnyPet
Cat:
description: A representation of a cat
type: object
properties:
huntingSkill:
type: string
description: The measured skill for hunting
enum:
- clueless
- lazy
- adventurous
- aggressive
required:
- huntingSkill
Dog:
description: A representation of a dog
type: object
properties:
packSize:
type: integer
format: int32
description: the size of the pack the dog is from
default: 0
minimum: 0
required:
- packSize
AnyPet:
description: Pet with unknown type
not:
- required: ["PetType"] |
Beta Was this translation helpful? Give feedback.
-
BTW, I wonder if we should de-emphasize the "allOf" discriminator pattern in the 3.2 (or later) spec, in favor of either "oneOf" or "anyOf". In particular, the only "complete" examples of polymorphism in the spec (there are two of them) currently uses the "allOf" pattern. I think we should be nudging folks away from this so changing these to either "allOf" or "anyOf" might be a good change. |
Beta Was this translation helpful? Give feedback.
-
I propose that we change the
discriminator
object in the next appropriate spec version to allow the discriminator property (named by thepropertyName
filed) to be an optional property. When the discriminator property is absent, this is interpreted as mapping to an implicit variant of theoneOf
/anyOf
/allOf
with an empty schema. (This might be problematic foroneOf
).This has been discussed in the past and I don't recall any strong objections to it.
Making the discriminator optional allows a more natural transition from a "monomorphic" schema (without a discriminator) to a polymorphic schema. In particular, objects that validate to the monomorphic schema (without a discriminator property) would continue to be valid in the polymorphic schema.
Beta Was this translation helpful? Give feedback.
All reactions