Using a custom parser with flask_smorest and/or marshmallow

551 views Asked by At

I'm using flask-smorest, the Flask/Marshmallow-based REST API framework and would like to be able to parse requests made with the axios JavaScript library, specifically for arrays passed via the querystring.

Axios sends arrays like this:

/api/v1/items/?types[]=cd&types[]=dvd

I need to create a custom parser for this (see also the helpful responses on the ticket I created) so I was able to cobble together a rudimentary parser that serves my purpose.

But for the life of me, I can't figure out how to tell flask_smorest or marshmallow to use this custom parser for my API's MethodView-based endpoints.

I was also thinking of creating a custom field in my Schema, but there too I don't know how I can access the querystring so that it can be parsed correctly.

So what is the best way to integrate the parser for a specific type of querystring array encoding in flask_smorest/marshmallow?

1

There are 1 answers

0
Jérôme On BEST ANSWER

Assuming you created a parser as demonstrated in webargs docs, all you need to do is to tell your flask-smorest app to use this custom parser in place of default FlaskParser.

This is done by overriding Blueprint and overriding ARGUMENTS_PARSER from ArgumentsMixin:

import flask_smorest

class Blueprint(flask_smorest.Blueprint):

    ARGUMENTS_PARSER = CustomParser()