Discriminator Mapping vs. Strict oneOf for Polymorphic Validation
0 reputation · 28 Apr 2024, 07:48 UTC
0 reputation · 28 Apr 2024, 07:48 UTC
When defining request bodies that can accept multiple object types, developers typically choose between using a discriminator object for explicit mapping or relying on oneOf for structural validation. This decision impacts how tooling handles type dispatch and how validators react to ambiguous payloads.
The discriminator object facilitates automatic class hierarchy generation in tools like OpenAPI Generator. However, because the discriminator is not a native JSON Schema validation keyword, some validators ignore it and fall back to the underlying oneOf or anyOf logic. This creates a discrepancy where a payload might be logically invalid according to the discriminator mapping but structurally valid according to the schema.
Conversely, using oneOf without a discriminator requires the validator to check every possible schema. In scenarios where sub-schemas overlap, this can lead to ambiguous matches or performance degradation during validation.
Given these differing failure modes, what is the recommended strategy for ensuring that a discriminator field is strictly enforced as required during runtime validation across diverse toolchains? Should the discriminator be mirrored as a required property within each sub-schema to bridge the gap between metadata and validation?
34775 reputation · 28 Apr 2024, 18:31 UTC
Yes. Mirror the discriminator field as a required property with a fixed value in each sub-schema. The OpenAPI discriminator object alone is not enforced by generic JSON Schema validators, so runtime validation across diverse toolchains requires the discriminator to also exist as schema constraints.
discriminator object is OpenAPI-specific metadata for documentation and code generation. It is not a native JSON Schema validation keyword.discriminator and validate only the underlying oneOf/anyOf structure.discriminator for class hierarchy and dispatch even when validators ignore it. This creates a gap between code generation expectations and runtime validation.When the discriminator is only metadata, a payload missing the field or containing an unknown value can still be structurally valid under oneOf if the subschemas overlap. Code generators may emit an optional discriminator property unless it is explicitly required in each subschema, leading to silent failures at runtime.
discriminator at the parent schema for tooling and documentation.required and constrain its value:properties:
type:
const: "cat"
required:
- type
Use const for a single mapping value or enum if multiple values map to the same schema.
oneOf matches.One missing diagnostic detail that changes the recommendation: which JSON Schema draft and validator are used in production runtime validation. If the runtime uses an OpenAPI-aware validator that enforces discriminator natively, the mirroring can be relaxed to documentation only. Confirm the validator.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.