Veltro has asked for the wisdom of the Perl Monks concerning the following question:
Dear monks,
I am looking for a possibility to indicate that the schema definition must be 'exact' in the sense that if more parameters are specified I want the validation to 'fail' for the request, and 'fail' or 'filtered' for the response (hopefully by specifying this in the OpenAPI spec). I wonder if anyone knows if this can be done in swagger 2.0 (or newer versions)?
As far as I understand now, swagger 2.0 allows you to tell that parameter are 'required' by defining the parameters like this (Just as an example a 'animal' object):
# In a POST request: ... parameters: - in: body name: body schema: $ref: '#/definitions/animal' required: true ... # In a resonse: ... responses: 200: description: animal response schema: $ref: '#/definitions/animal' ... # and then create the animal definition: definitions: animal: description: animal properties: animalid: description: The animal ID type: integer animalname: description: The animal name type: string type: object required: [ 'animalid', 'animalname' ]
However, there is nothing that stops anyone from sending an animal with many more parameters other than 'animalid' or 'animalname' or could even add completely unrelated data in the request. Similarly it also does not stop the back-end from creating an object containing many more parameters and send those to the client as well.
What if I would send a 'user' object and I had accidentally created a database query resulting in an object containing the password as well (because let's say I was turning beer instead of coffee into code one night). I did not specify the password as a valid parameter in the spec, but it gets send anyhow! How could I potentially either block the entire response, or if desired, filter out the password and send the response with just those parameters that were specified in the spec?
Another reason why this would be desired is to detect typos, In case a parameter is optional and e.g. called animalname, but is send as animalName, this is not detected. But when the definition would have been taken 'strict' then this typo would have been detected. The response error could say something like: animalName is not a valid parameter!
I was digging through the entire OpenAPI spec, but have not been able to find anything. Has anyone found a solution for this?
Thanks,
Veltro
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Mojolicious::Plugin::OpenAPI strict or filter object representation
by 1nickt (Canon) on Mar 25, 2019 at 00:45 UTC | |
by Veltro (Hermit) on Mar 25, 2019 at 11:40 UTC |