flask-smorest icon indicating copy to clipboard operation
flask-smorest copied to clipboard

Is it possible to use other objects than json in the request body?

Open javad-amin opened this issue 2 years ago • 6 comments

Hi,

I am wondering if there is a way to have a list instead of the json object in the request body? My currect solution with a schema containing a field of type List looks like this:

{ "id_list": [ "3fa85f64-5717-4562-b3fc-2c963f66afa6" ] }

Desired request body schema: ["3fa85f64-5717-4562-b3fc-2c963f66afa6"]

Thank you.

javad-amin avatar Jan 12 '22 14:01 javad-amin

I don't see how to do that.

I think last time I needed this I added a "data" key, like {"data": [my list of stuff]}.

lafrech avatar Jan 12 '22 17:01 lafrech

Using many=true for the schema and pre_load decorator I could I achieve this. But this workaround still has a problem because the example in the swagger documentation looks like

[ { "data": "3fa85f64-5717-4562-b3fc-2c963f66afa6" } ]

and I need it to be like ["3fa85f64-5717-4562-b3fc-2c963f66afa6"]

Is there any easy way to overwrite the example value in the documentation and have a custom example?

javad-amin avatar Jan 13 '22 13:01 javad-amin

I just found out the answer to my question, there is an example method for the argument method of the blueprint decorator, so it just works.

javad-amin avatar Jan 13 '22 15:01 javad-amin

This will only fix the example, not the documentation.

A lot of things can be achieved using pre/post_load/dump but those can't be automatically documented by apispec, of course.

I don't see how to document this without subclassing apispec stuff and overriding methods there (schema2jsonschema, most probably) to introduce a special case for your schema.

lafrech avatar Jan 13 '22 15:01 lafrech

I just found out the answer to my question, there is an example method for the argument method of the blueprint decorator, so it just works.

hi, could you post your solution ? I needed this as well.

cmabastar avatar May 03 '22 08:05 cmabastar

Sorry for the late response! If you look at flask_smorest.Blueprint.arguments, it has a parameter called example which was what I needed.

https://flask-smorest.readthedocs.io/en/latest/api_reference.html#flask_smorest.Blueprint.arguments

javad-amin avatar Jun 20 '22 19:06 javad-amin