-
Notifications
You must be signed in to change notification settings - Fork 422
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
Validate discriminator values #3909
Comments
Please take a look at related issue I raised some time ago: |
It seems there's a couple of variants, in which we can render discriminators & mappings. From what I understand these are:
It would be great to be able to use just a single representation, or at least constraint the possibilities to a set of "best ones". Though - which would those "best ones" be? (btw. - I think discriminators are useful if only for the fact that code generators might use them to create deserialization code efficiently) |
Const is just more explicit representation of what single-value enum does. See swagger-api/swagger.io-docs#348 (comment) I actually believe having Maybe reasoning like that:
In the end - for me - best final representation should match model described above - just oneOf+const, and named and required discriminator field. I have no idea why the whole discriminator mapping idea was added to openapi - if one can have exactly same functionality and performance with just one additional attribute attached to discriminator field definition. See how nicely this all scales:
First is enough to parse/validate correctly. Second is more strict about structure. Third allows to build mapping tables and optimize. |
In our use case, we use tapir for the backend and generate TypeScript clients based on the OpenApi Specification generated out of Tapir schema. So having "one-to-one" strictness of type definitions in Scala with what TypeScript is allowed to do would be the "best one" solution to me. And I think this can be only achieved with either enum validation or constant discriminator field. |
ok, thank you very much for the detailed explanations. Let's go with the const validator, as I think it's the most precise. Now we just need an implementation :) |
@adamw looking briefly into the support of Could you please leave some pointers of how to add support for |
Please see here #3765 We implemented this with additional It would be nice to simply have |
@seveneves @adamw I reopened our take on |
Tapir version: 1.10.12
Scala version: 3.3.3
Given following model
It will have the following OpenApi specification generated
Schemas for
Cat
andRat
are essentially the same and only can be distinguished by discriminator valuepet
. However, it seems that OpenAPI specification does not require discriminator to actually be enforced when validatingoneOf
type ofSchema
definition.This is answered in the discussion of OpenAPI specification OAI/OpenAPI-Specification#3608 where the suggestion is to use
anyOf
instead ofoneOf
to make validation work (a library I use does fail the validation ofoneOf
becauseRat
andCat
are the similar schemas structurally).One easy fix for a such problem is to add a validator of type
enum
to each discriminator fieldpet
. So then the definition would become as follows. Such validation will make sure that typeCat
andRat
are not subsets of each other.I would like to make a suggestion to extend
sttp.tapir.generic.Configuration
by introducing a new flag asvalidateDiscriminatorValue: Boolean = false
and then add enum validator in methodsttp.tapir.SchemaType.SCoproduct#addDiscriminatorField
.I can submit a change request if this is accepted
The text was updated successfully, but these errors were encountered: