You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/oas.md
+15-9Lines changed: 15 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -3425,7 +3425,7 @@ Note that `discriminator` MUST NOT change the validation outcome of the schema.
3425
3425
| ---- | :----: | ---- |
3426
3426
| <a name="property-name"></a>propertyName | `string` | **REQUIRED**. The name of the discriminating property in the payload that will hold the discriminating value. The discriminating property MAY be defined as required or optional, but when defined as optional the Discriminator Object MUST include a `defaultMapping` field that specifies which schema is expected to validate the structure of the model when the discriminating property is not present. |
3427
3427
| <a name="discriminator-mapping"></a> mapping | Map[`string`, `string`] | An object to hold mappings between payload values and schema names or URI references. |
3428
-
| <a name="default"></a> defaultMapping | `string` | The schema name or URI reference to a schema that is expected to validate the structure of the model when the discriminating property is not present in the payload. |
3428
+
| <a name="default"></a> defaultMapping | `string` | The schema name or URI reference to a schema that is expected to validate the structure of the model when the discriminating property is not present in the payload or contains a value for which there is no explicit or implicit mapping. |
3429
3429
3430
3430
This object MAY be extended with [Specification Extensions](#specification-extensions).
3431
3431
@@ -3455,20 +3455,24 @@ However, the exact nature of such conversions are implementation-defined.
3455
3455
3456
3456
##### Optional discriminating property
3457
3457
3458
-
When the discriminating property is defined as optional, the [Discriminator Object](#discriminator-object) MUST include a `defaultMapping` field that specifies a schema that is expected to validate the structure of the model when the discriminating property is not present in the payload. This allows the schema to still be validated correctly even if the discriminating property is missing.
3458
+
When the discriminating property is defined as optional, the [Discriminator Object](#discriminator-object) MUST include a `defaultMapping` field that specifies a schema that is expected to validate the structure of the model when the discriminating property is not present in the payload or contains a value for which there is no explicit or implicit mapping. This allows the schema to still be validated correctly even if the discriminating property is missing.
3459
3459
3460
3460
The primary use case for an optional discriminating property is to allow a schema to be extended with a discriminator without breaking existing clients that do not provide the discriminating property.
3461
3461
3462
-
Typically the schema specified in the `defaultMapping` field will specify that the discriminating property is not present, e.g.
3462
+
When the discriminating property is defined as optional, it is important that each subschema that defines a value for the discriminating property also define the property as required, since this is no longer enforced by the parent schema.
3463
+
3464
+
The `defaultMapping` schema is also expected to validate the structure of the model when the discriminating property is present but contains a value for which there is no explicit or implicit mapping. This is typically expressed in the `defaultMapping` schema by excluding any instances with mapped values of the discriminating property, e.g.
3463
3465
3464
3466
```yaml
3465
3467
OtherPet:
3466
3468
type: object
3467
-
not:
3468
-
required: ['petType']
3469
+
properties:
3470
+
petType:
3471
+
not:
3472
+
enum: ['cat', 'dog']
3469
3473
```
3470
3474
3471
-
This will prevent the default mapping schema from validating a payload that includes the discriminating property, which would cause a validation of the payload to fail when polymorphism is described using the `oneOf` JSON schema keyword.
3475
+
This prevents the `defaultMapping` schema from validating a payload that includes the discriminating property with a mapped discriminating value, which would cause a validation to fail when polymorphism is described using the `oneOf` JSON schema keyword.
3472
3476
3473
3477
##### Examples
3474
3478
@@ -3543,11 +3547,13 @@ MyResponseType:
3543
3547
defaultMapping: OtherPet
3544
3548
OtherPet:
3545
3549
type: object
3546
-
not:
3547
-
required: ['petType']
3550
+
properties:
3551
+
petType:
3552
+
not:
3553
+
enum: ['Cat', 'Dog', 'Lizard']
3548
3554
```
3549
3555
3550
-
In this example, if the `petType` property is not present in the payload, the payload should validate against the `OtherPet` schema.
3556
+
In this example, if the `petType` property is not present in the payload, or if the value of `petType` is not "Cat", "Dog", or "Lizard", then the payload should validate against the `OtherPet` schema.
3551
3557
3552
3558
This example shows the `allOf` usage, which avoids needing to reference all child schemas in the parent:
0 commit comments