0

Because of project specificity I have to write my own model validator for Flask-restplus API app. Simply speaking - when validation error occurs, its format and status code (400) is not proper. It should return JSON object with messages in particular format with status code 422.

What I do is more or less this:


ns = api.namespace('somenamespace', description='blabla')

class MyModel(MyBaseModel):
    def __init__(self):
        self.id = fields.Integer()
        self.name = fields.String()


my_model = api.model('MyModel', MyModel())

@api.marshal_list_with(my_model, envelope='json')
@ns.route('/')
class SomeClass(Resource):

    @api.expect(my_model, validate=False)
    @api.doc(responses={
        200: 'Success',
        401: 'Authentication Error',
        403: 'Requested resource unavailable',
        409: 'Conflict, document already exists',
        422: 'Validation Error'
    })
    def post(self):
        """
        Save single document in the database.
        :return:
        """

        request_payload = json.loads(request.data)
        validated_payload, payload_errors = some_validation(request_payload)

        if payload_errors:
            return jsonify(payload_errors), 422
        else:
            return jsonify({'response': 'ok})
Instance of `MyModel` behaves basically like a dict, so no problem in registration. Problem is that when I send data in `-d`, be it through `curl` from command line, or swagger, I constantly get `400` instead of `422`. I assume this is due to the default, built-in validation of input data based on `MyModel`. This is cool, but I have to omit it, and apply my own validation.
Mateusz Korycinski
  • 1,037
  • 3
  • 10
  • 24
  • Maybe the problem does comes from `MyModel`. Have you tried the way it is described in the documentation of the decorator `api.expect` ? https://flask-restplus.readthedocs.io/en/stable/swagger.html#the-api-expect-decorator Does it behaves the same? If so, the maybe the problem comes from somewhere else. Also, can you specify what is the json you pass in your `curl` command? – SivolcC Feb 05 '19 at 05:00
  • Thanks for your interest. Basically everything started to work properly when I turned off validation globally when initiating `flask_restplus` api. It seems that if validate flag is set to True, it doesn't take into account flag from `@api.expect` decorator. – Mateusz Korycinski Feb 14 '19 at 09:57

1 Answers1

0

in the documentation and as @CloC says, one method is specifying the model as

your_model = ns.model('YourModel', {
    'id': fields.Integer(
        description='The user id'
    )
    'name': fields.String(
        description='The user name'
    )
})

... > profit

@ns.route('/', methods=["post"])
@ns.doc(params={
    "id": "the user id (int)",
    "name": "the user name (str)"
})
class SomeClass(Resource):

    @ns.expect(your_model)  # this defines the request 
    # @ns.marshal_list_with(your_model, envelope='json')  # this defines the response
    @ns.response(200, 'Success')
    ... > reponses
    def post(self):
        etc...

        return <response with format as model in marshal>

you might want to re-define the response model though unless you will return something of the form that you put in.. also maybe specify marshal_with as you do not return a list?

rfho_bdss
  • 170
  • 1
  • 18