pythonflaskflask-restx

flask-restx how to create parser from model


I am using flask-restx for documenting and formatting my api

I have a app, including a directory, holding json schemas in the following format: http://json-schema.org/draft-07/schema#
(in my app they are saved as json file, in the below example I put it as a dict hard coded to simple the example)


using the schema, I would like to achieve 3 goals:
  1. documentation
  2. validation on request
  3. parsing the request

using @api.expect(request_model, validate=True) I managed to achieve (1) and (2), but I do not find a way to parse using the existing schema, I had to create a parser object reqparse.RequestParser(), and rewrite the params.
Is there a way to create the RequestParser from the model? (the model is created from the existing json schema file)

here is my code:


from flask_restx import Api, inputs

api = Api(app, doc='/my_doc_path')


request_schema = {
    'type': 'object',
    'properties': {
        'param1': {'type': 'string'},
        'the_date': {"type": "string", "format": "date-time"},
    },
    'required': ['param1'],
}
request_model = api.schema_model('my_api_model', request_schema)

@api.route('/my_api/<string:id>')
class MyApi(Resource):


    @api.expect(request_model, validate=True)
    def post(self, id):
        """
        my cool app
        """
        parser = reqparse.RequestParser()
        parser.add_argument('param1', help='some param 1')
        parser.add_argument('the_date', type=inputs.datetime_from_iso8601, help='some date_time')
        args = parser.parse_args()
        do_something(args['param1'], args['the_date'])

is there a way to to something like:

parser = reqparse.RequestParser(request_model)
args = parser.parse_args()

?


Solution

  • I ended up writing code to generate the parser form the schema:

    def build_parser(schema):
        parser = RequestParser()
    
        for prop, conf in schema['properties'].items():
            if conf.get('type') == 'string' and conf.get('format') == 'date-time':
                parser.add_argument(prop, type=inputs.datetime_from_iso8601)
            elif conf.get('type') == 'integer':
                parser.add_argument(prop, type=int)
            elif conf.get('type') == 'boolean':
                parser.add_argument(prop, type=bool, default=False)
            elif conf.get('type') == 'array':
                parser.add_argument(prop, default=list, action='append')
            elif conf.get('type') == "object":
                parser.add_argument(prop, type=dict, default=dict)
            else:
                parser.add_argument(prop)
         return parser
    

    I created the following decorator class:

    import functools
    import inspect
    
    class Rest:
    
        @staticmethod
        def route(api, route_str='', decorators=None):
            decorators = decorators or []
    
            def wrapper(cls):
                for name, method in inspect.getmembers(cls, inspect.isfunction):
                    schema_name = getattr(method, '_schema_name', None)
                    if schema_name:
                        schema = getattr(method, '_schema', None)
                        setattr(cls, name, api.expect(api.schema_model(schema_name, schema), validate=True)(method))
    
                cls.method_decorators = cls.method_decorators + decorators
                return api.route(route_str)(cls) if route_str else cls
    
            return wrapper
    
        @staticmethod
        def schema(schema_name, parse=False):
            def decorator(f):
                f._schema_name = schema_name
                f._schema = get_api_schema(schema_name) # this function reads the json file
                parser = build_parser(f._schema)
    
                @functools.wraps(f)
                def wrapper(*args, **kwargs):
                    if parse:
                        req_args = parser.parse_args()
                        return f(*args, req_args, **kwargs)
    
                    return f(*args, **kwargs)
    
                return wrapper
    
            return decorator
    

    and here is how I use it:

    @Rest.route(api, '/my_api/<string:id>')
    class MyApi(Resource):
    
    
        @Rest.schema('my_api_schema_name', parse=True)
        def post(self, args, id):
            do_something(args['param1'], args['the_date'])
    

    notice the decorator I wrote will parse the args and pass them to the post function