-
-
Notifications
You must be signed in to change notification settings - Fork 211
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
Default value ambiguity in oneOf
/ Support for discriminators?
#104
Comments
Actually, it could be that oneOf isn't working. I have schema types with different properties and they're not being distinguished from one another. |
Can you give an example? allOf is working well for me. |
So my schema is pretty big, but way down in it I have something like this: When I POST an instance of SimilarTypeOne, we're all set. When I POST an instance of SimilarTypeTwo the validator complains uniqueOne is missing. If I switch the order in the oneOf, instances of SimilarTypeOne now get rejected for not having uniqueTwo.
|
@cdimascio can't send you the whole schema for copyright / ip reasons and in simplifying / anonymizing for a reproducible test case, the problem goes away : ( If I figure out what the problem is, I'll let you know. |
@catadch i created a few tests to exercise the schema you sent. it appears to work in that i'm able to use the two oneOf instances there may be some other element that is contributing to the issue. feel free to modify my tests as well |
@cdimascio - I found the problem. In the schema below, it's the enums / defaults for the two "value" number type properties that causes the problem. Comment either one out and the problem goes away. I have a stand-alone project to demonstrate this is you want it.
|
hmm. i added a new test that adds the value properties in a manner similar to what you show above I'm still not able to reproduce please send along your sample project that is able to trigger the issue. also, feel free to review my test to make sure i'm exercising your case properly |
@cdimascio try this:
Let me know when you'e cloned it and I'll make it private again. |
i cloned it, ran |
As in the README : ) |
gotcha. just picked up the readme change. i can run the test. thanks! |
No worries. It's 01:00 where I am and I've been working on this for 48 hours straight, so i'm out of here too. |
get some sleep :) hopefully we'll have some answers soon. |
This is odd. I added your spec and tests into the test suite and they pass O.o the api spec: https://github.com/cdimascio/express-openapi-validator/pull/107/files#diff-ed3d36f532fcf4bdd12dfce177b18308R1 There must be something else in the project that's playing a role. will poke around further in your example. |
nevermind. it's now reproducible. |
This looks like it may be an issue with Ajv and how defaults are handled. When in the context of The details of the Ajv feature and ambiguity is described here https://github.com/epoberezkin/ajv/issues/42 Getting back to your example, I observed a couple of behaviors:
Ultimately, the best course of action may be to try to reproduce this using Ajv only. Other options might be to:
// Install middleware prior to the validator to handle these ambiguous defaults and inject their values prior to validation
app.use((req, res, next) => {
if (Array.isArray(req.body.somethings)) {
req.body.somethings.forEach(s => {
if (s.type === 'TypeOne' && !s.value) {
s.value = 1;
}
if (s.type === 'TypeTwo' && !s.value) {
s.value = 2;
}
})
}
next();
});
// install validator
new OpenApiValidator(opts).install(app);
// actual routes below here
// .... |
oneOf
types (orig title: Support for discriminators?)
All in all, discriminators should work for most cases, however, as we see here, when ambiguity is present in the schema and the discriminator is necessary to inform a choice, the validator may fail. Ideally, Ajv would support this. However, in the case that it will not, a performant solution is non trivial. I'm open to ideas and suggestions. On the bright side, this issue is limited to cases where oneOf or anyOf are used AND conflicting defaults exist in that oneOf or anyOf branch. |
Keeping this open. Will investigate possible solutions for this case |
oneOf
types (orig title: Support for discriminators?)oneOf
/ Support for discriminators?
@catadch curious to know your thoughts on my comments above and how you are currently planning to proceed |
@catadch we now have support for top level discriminators. See the discriminator example here |
Hey @cdimascio, did you have any thoughts on the direction you'd see being taken to support discriminators within the request body? We're currently using the validator (and loving it so far) where we're supporting a request body which contains a top level attribute where the value is a map of elements where each could inherit from a base schema. Each one of those elements has their own set of required fields (though they share common fields which are required across each). To use the validation provided by this middleware, we're using
While the validation works as we'd expect, we do have an amount of duplication as we're not using polymorphism - not a biggie. As we're using the anyOf approach and not using discriminators, we do receive a verbose error output which we'd like to reduce, when sending an invalid request body as the validator is validating the invalid question against each schema. Each schema has a I'm happy to take a stab at this, just wondering had you any thoughts on how to go about this? Is there any gotchas to be aware of? |
I can't work out if discriminators are supposed to be supported, or not. I certainly can't get them to be honored. If I use express-ajv-swagger-validation then they are honored, but default values are not. Kind of hoping express-openapi-validator might be a one-stop-shop as everything else works a treat.
The text was updated successfully, but these errors were encountered: