Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require discriminator instance to be an object #206

Merged
merged 1 commit into from
Jan 25, 2025

Conversation

davishmcclurg
Copy link
Owner

Previously, all non-hash instances were valid when using discriminator, which I think is incorrect and confusing. There are a couple options:

  1. Require instances to be objects when using discriminator
  2. Fall back to regular allOf/anyOf/oneOf processing for non-object instances

I went with option 1, because it seems safer and I don't know if it makes sense to ever allow non-object instances. discriminator depends on propertyName, which itself requires hash instances. Allowing null is still possible by wrapping the discriminator schema in another oneOf (like the test example: nullable_union_schema).

The OpenAPI specification doesn't say anything about non-object instances directly, but it does say:

The expectation now is that a property with name petType MUST be present in the response payload, and the value will correspond to the name of a schema defined in the OAS document.

Which I think can be interpreted as requiring the response payload to be an object.

Fixes: #204
Related: be45f04

Previously, all non-hash instances were valid when using
`discriminator`, which I think is incorrect and confusing. There are a
couple options:

1. Require instances to be objects when using `discriminator`
2. Fall back to regular `allOf`/`anyOf`/`oneOf` processing for
   non-object instances

I went with option 1, because it seems safer and I don't know if it
makes sense to ever allow non-object instances. `discriminator` depends
on `propertyName`, which itself requires hash instances. Allowing `null`
is still possible by wrapping the `discriminator` schema in another
`oneOf` (like the test example: `nullable_union_schema`).

The [OpenAPI specification][0] doesn't say anything about non-object
instances directly, but it does say:

> The expectation now is that a property with name petType MUST be present in the response payload, and the value will correspond to the name of a schema defined in the OAS document.

Which I think can be interpreted as requiring the response payload to be
an object.

Fixes: #204
Related: be45f04

[0]: https://github.com/oai/openapi-specification/blob/main/versions/3.1.0.md#discriminator-object
@davishmcclurg davishmcclurg force-pushed the non-object-discriminator branch from 9684462 to 5f20eaa Compare January 19, 2025 22:51
@davishmcclurg davishmcclurg merged commit ed3f419 into main Jan 25, 2025
58 checks passed
@davishmcclurg davishmcclurg deleted the non-object-discriminator branch January 25, 2025 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to control nullability of discriminated unions
1 participant