-
Notifications
You must be signed in to change notification settings - Fork 373
Support for polymorphic validation #241
Comments
Can you give a code snippet and some example? I ask because we have heavily tested validation so I'm not sure how to reproduce. |
Below a simplified version of my schema.
The issue is that commands that are sent to /api/commands are only validated against Command, regardless of the discriminator value. If I send the following it will still pass validation and call my controller function even though it's missing the required property "prop1" for the type "Command1".
|
I cannot reproduce this. Whenever I throw your definitions into my API and have my request/response use them, if I post only what you do, I get a request validation error (Parameter (command) failed schema validation]): {
[Error: Parameter (pet) failed schema validation]
code: 'SCHEMA_VALIDATION_FAILED',
failedValidation: true,
results: {
errors: [
{
"code": "OBJECT_MISSING_REQUIRED_PROPERTY",
"message": "Missing required property: prop1",
"path": []
}
],
warnings: []
},
path: [ 'paths', '/pets', 'post', 'parameters', '0' ],
paramName: 'pet'
} And when I post a full example, with the |
My package.json has "swagger-tools": "^0.8.7". I am using it with MEAN.js, and have the following in my express.js configuration file. I'm using the express router vs the swagger-tools one, but it since that middleware comes after the validator, and it's validating some requests I'm hope I've got it correctly configured. var swaggerDoc = require('../app/api/swagger.json');
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata());
// Validate Swagger requests
app.use(middleware.swaggerValidator());
// Route validated requests to appropriate controller
//app.use(middleware.swaggerRouter(options));
// Serve the Swagger documents and Swagger UI
app.use(middleware.swaggerUi());
}); |
Can you run with |
When I submit
the output of the request to the log is
|
Can you show me the rest of your server file, replacing sensitive data with reasonable mocks so as not to change the structure of the file? |
I hate to keep bugging you but I cannot reproduce it. If you'd rather not give me your server file, could you give me a recipe via a patch/PR that will showcase this issue in |
I was able to reproduce it. It required me to get outside of the test suite. Let me take a look. |
Awesome. I was just about to start putting together a skeleton app for you. Sorry for the delay. I was out grabbing some lunch. Let me know if you need the skeleton app anymore. |
I've figured it out, swagger-tools is working right which is why my test worked but the server didn't. Your parameter references |
Right, but the goal in reality is that I want to have Command1, Command2, Command3, etc. Is that not possible? |
Let me get @webron involved as I don't understand the |
Cool. I'm pretty new to swagger, so I may be understanding it incorrectly. I'll wait to hear what the verdict is. Thanks for being so responsive. |
I'm guessing TL;DR won't work here? 😊 Let me have a look. |
Okay, I hope I got everything needed from the thread. If the intention is to pass Two side notes - I'm happy to see that you've used the The other, which is slightly unrelated, for every model you should include |
Let me get this straight: If your model has an ancestor that uses the |
That was my understanding. Think of it like a function in a language with strong typing like Java or C++. I can declare a function that receives a base class or interface, and any function that uses that base class can also accept any subclass too. |
@whitlockjc - that is correct and the explanation @daveismith provided is fairly accurate. The |
Wow...that changes all kinds of things. |
@webron - So to support this, I would need to see if a schema had a |
Wouldn't it be the reverse? If the specified schema had a |
Ah yes, that is correct. |
Actually, it's not even that complex. When you receive an object that is specified by a schema with a |
It's not a complexity thing to understand, just a potentially complex thing to implement due to how we do validation right now. I could be wrong, it might be really simple. |
@webron mentioned recursion. I'm not 100% on that, I can think of two cases.
|
Can you join #swagger on irc.freenode.net? It might be easier to chat in real time there. |
I've got to head out now, but could tomorrow. |
Please do. I think I understand it but my understanding doesn't sound simple so I could be misunderstanding it. I'm jeremyw so highlight me and I'll respond. |
When I was talking about recursion, there are two cases:
|
This is the part I don't understand. I fully understand polymorphism, I've written more Java than I care to admit, but I don't see how the value specified in the All of this being said, it seems that any schema that uses a discriminator makes it so that when it is referenced in an |
So while discussing it directly with @whitlockjc, I wanted to use your example @daveismith to show him the usage, only to realize that I've misread it and my previous comment is incorrect. There is an issue with the example and it cannot be used properly. The problem is that when using the I have to say that this is an issue I've been wanting to discuss for a while now because it is not properly defined in the specification right now, and something is missing. I do have a workaround for it to ease things up but we need to open an official discussion over on swagger-spec to provide better clarification. The {
"Command": {
"discriminator": "type",
"properties": {
"type": {
"type": "string"
},
"propBase": {
"type": "string"
}
},
"required": ["type", "propBase"]
},
"Command1": {
"description": "Command 1",
"allOf": [
{
"$ref": "#/definitions/Command"
},
{
"properties": {
"prop1": {
"type": "string"
},
"type": {
"type": "string",
"enum": [ "command1" ]
}
},
"required": ["prop1"]
}
]
}
} As you can see, I had to duplicate the I do have a couple of suggestions on how to overcome this (one implicit which can be done within the scope of the current spec and one explicit which would break the current spec). However, further discussion should be taken officially to the swagger-spec repo instead of having it here, and then we can return here for the actual implementation. This is solvable, but for organization and documentation sake, let's do it in the right place. |
That clears up my misunderstanding but it definitely doesn't simplify the implementation. ;) However, that's not your problems and I'm sure I can figure it out. |
Completely get it. But we should clarify it before you start implementing it, because we may decide to go a different way than the one I stated above. This is not documented in the spec. |
So let me run my understanding by you. So whenever a schema defines a How do we do multiple levels of polymorphism? |
That's definitely an issue with it, but I'd rather we take the discussion to swagger-spec (for the reasons stated above). We can't resolve it here. |
Well, for the solution I need multiple levels of polymorphism shouldn't matter. I mean, a parameter or response can only be one base so as long as I find all descendants, it doesn't matter if any of the descendants are single or multiple polymorphic. |
Right, but we still need to decide on the implementation, so that's just the theory. |
For now, I have what I need. cc me on the swagger-spec stuff because I think we need an opinion on the matter that benefits more tools than just swagger-codegen. |
So what's the story with this topic? I have an issue validating my json object with As a result validateModel validates only fields, specifies in my base schema. But it ignores fiest, specified in the schema, which name if the value of the |
Any update on? |
Also looking for an update on this. Was this ever implemented? |
Is there any update on this issue? |
No update right now. I apologize but this isn't something likely to happen due to this project effectively being deprecated, see #335. |
I've been trying to use swagger-tools to validate requests to my controllers. It seems that when I perform a request with the body parameter specified as a base class, validation happens only against the specification of the base class model, but not against the model for the extended class identified by the discriminator.
The text was updated successfully, but these errors were encountered: