Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Support for MethodView #85
I'm using Flask's
Basically, the idea is to have one class per endpoint and one class method for each HTTP method. The method name is the HTTP method, lowercase.
from flask.views import MethodView class UserAPI(MethodView): def get(self): """User detail view. --- get: responses: 200: schema: UserSchema """ users = User.query.all() ... def post(self): """User post. --- post: responses: 201: schema: UserSchema """ user = User.from_form_data(request.form) ... app.add_url_rule('/users/', view_func=UserAPI.as_view('users'))
AFAIU, there is currently no support for Flask's
I think I managed to get apispec to produce the correct OpenAPI file using the following patch:
The idea is that if the
Thinking of it, maybe the only difference is that this allows the user to keep each docstring in each method rather than grouping all of them at the top of the class. This can be seen as cosmetic, but I think it would be better this way.
I'd be happy to get any feedback.
If there is interest for this feature, I may clean this up, add tests and all, but before I move any further, I'd rather be sure I'm on the right tracks.
flask-apispec uses apispec and provides support for
I'm at a crossroad, right now. flask-apispec sounds appealing, but I'm still a bit reluctant. Not only because it says "flask-apispec isn't stable yet", but also because from what I've read, its code is far from trivial ("annotation" mechanism), and if I ever want to change or customize anything, I'm afraid I might end up losing more time than I gained using it, if I'm able to do it at all.
Anyway, I thought it was worth mentioning as there may be overlap between flask-apispec and apispec.ext.flask, and it could be worth thinking twice before duplicating the work/features.
Let's have the discussion here rather than in "line notes".
I guess you're right.
Thanks for taking the time to investigate and sorry if my point was unclear or uninformed.
As I wrote, there is no total blocker. One can use a
@lafrench Good to hear it isn't broken. You should be able to keep the docstrings on the individual class methods and register them individually. It seems like it might be more important to keep the docs with their respective methods at the expense of a little bit of duplicated registration logic.
I imagine you could add a helper utility until
def register_method_view(spec, method_view): for method in method_view.methods: view = getattr(view.view_class, method.lower()) spec.add_path(view=view)