noirbizarre / flask-restplus

Fully featured framework for fast, easy and documented API development with Flask
http://flask-restplus.readthedocs.org
Other
2.73k stars 506 forks source link

Integration with webargs, documenting parameters with swagger #448

Open Midnighter opened 6 years ago

Midnighter commented 6 years ago

So I had a very simple file upload that worked beautifully with the swagger docs:

class Foo(Resource):

    upload_parser = api.parser()
    upload_parser.add_argument(
        "model", location="files", type=FileStorage, required=True,
        nullable=False, help="No model file was submitted in field 'model'.")

    @api.doc(parser=upload_parser)
    def post(self):
        """Load a metabolic model and submit it for testing by memote."""
        upload = self.upload_parser.parse_args(strict=True)["model"]
        model = self._load_model(upload)
        job_id = self._submit(model)
        return {"key": job_id}, 202

since the recommendation is to move to webargs, I did so and got the upload working but I'm struggling a little with getting the argument documented. I created a corresponding field and a use_kwargs parser. Putting the same line @api.doc(parser=...) does not generate a documentation for the parameter, however. Also I'm wondering if there is a better option than using a base webargs.fields.Field for a file.

martijnarts commented 6 years ago

Using webargs like this is currently not supported, afaik.

Midnighter commented 6 years ago

Can you point me to any best practices, examples, or the like? There seem to be plenty of ways to achieve the same thing and I feel a little paralyzed choosing one option.

martijnarts commented 6 years ago

Can you show the code you tried with a use_kwargs parser?

ashwinkumar01 commented 6 years ago

I had a similar problem. This will work if you use the use_kwargs decorator and you pass in a dictionary containing your file parameter. However, the parameter disappeared on the UI for me when I tried to pass in a Marshmallow Schema class

Midnighter commented 6 years ago

I'll post the code by next weekend latest. A bit under stress right now.