Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Mojolicious::Plugin::OpenAPI strict or filter object representation

by Veltro (Friar)
on Mar 24, 2019 at 15:33 UTC ( #1231616=perlquestion: print w/replies, xml ) Need Help??
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

  • Comment on Mojolicious::Plugin::OpenAPI strict or filter object representation
  • Download Code

Replies are listed 'Best First'.
Re: Mojolicious::Plugin::OpenAPI strict or filter object representation
by 1nickt (Abbot) on Mar 25, 2019 at 00:45 UTC

    Hi, I am not sure if the Mojo plugin supports it but the OpenAPI attribute for this IIUC is additionalProperties. There's also additionalItems for arrays.

    Hope this helps!


    The way forward always starts with a minimal test.

      Thanks 1nickt, that solved the validation issues.

      I did read about additionalProperties in the OpenAPI spec. But they did not mention that you can just simply add additionalProperties=false. I found a better description here where they clearly state:

      The additionalProperties keyword may be either a boolean or an object. If additionalProperties is a boolean and set to false, no additional properties will be allowed.

      Veltro

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1231616]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2019-04-22 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I am most likely to install a new module from CPAN if:
















    Results (112 votes). Check out past polls.

    Notices?