Skip to content

Commit c11fda7

Browse files
committed
Use defaultMapping for unmapped discriminating values
1 parent 4018f80 commit c11fda7

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/oas.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3425,7 +3425,7 @@ Note that `discriminator` MUST NOT change the validation outcome of the schema.
34253425
| ---- | :----: | ---- |
34263426
| <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. |
34273427
| <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. |
34293429

34303430
This object MAY be extended with [Specification Extensions](#specification-extensions).
34313431

@@ -3455,20 +3455,24 @@ However, the exact nature of such conversions are implementation-defined.
34553455

34563456
##### Optional discriminating property
34573457

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.
34593459

34603460
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.
34613461

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.
34633465

34643466
```yaml
34653467
OtherPet:
34663468
type: object
3467-
not:
3468-
required: ['petType']
3469+
properties:
3470+
petType:
3471+
not:
3472+
enum: ['cat', 'dog']
34693473
```
34703474

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.
34723476

34733477
##### Examples
34743478

@@ -3543,11 +3547,13 @@ MyResponseType:
35433547
defaultMapping: OtherPet
35443548
OtherPet:
35453549
type: object
3546-
not:
3547-
required: ['petType']
3550+
properties:
3551+
petType:
3552+
not:
3553+
enum: ['Cat', 'Dog', 'Lizard']
35483554
```
35493555

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.
35513557

35523558
This example shows the `allOf` usage, which avoids needing to reference all child schemas in the parent:
35533559

0 commit comments

Comments
 (0)