Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

`APISpecError: Could not find endpoint for view` when using flask MethodViews #125

Closed
boosh opened this issue May 5, 2017 · 2 comments

Comments

@boosh
Copy link

commented May 5, 2017

I've seen #85 which seems to suggest Flask's MethodViews should be supported. I've added a URL rule with:

app.add_url_rule('/articles/<publication>', view_func=Articles.as_view('articles'))

But when I try to add a path for my schema with:

spec.definition('Article', schema=ArticleSchema)
spec.add_path(view=Articles)

I get an error:

apispec.exceptions.APISpecError: Could not find endpoint for view <class 'mypackage.resources.articles.Articles'>

I've tried passing various combinations of Articles, Articles.as_view('articles'), etc to add_path but nothing works. I've also tried the function in #85, but it still doesn't work.

I'm assuming this is a bug?

@djanderson

This comment has been minimized.

Copy link
Contributor

commented Jul 10, 2017

This is my current solution to this problem. If there's interest, I can do a PR to apispec.ext.flask.

from urllib.parse import urljoin

from apispec.ext.flask import _rule_for_view, flaskpath2swagger
from apispec import Path, utils
from flask import current_app

def path_from_method_view(spec, method_view, **kwargs):
    rule = _rule_for_view(method_view)
    path = flaskpath2swagger(rule.rule)
    app_root = current_app.config['APPLICATION_ROOT'] or '/'
    path = urljoin(app_root.rstrip('/') + '/', path.lstrip('/'))
    operations = {}
    for method in method_view.methods:
        method_name = method.lower()
        view = getattr(method_view.view_class, method_name)
        docstring_yaml = utils.load_yaml_from_docstring(view.__doc__)
        operations[method_name] =  docstring_yaml or dict()

    return Path(path=path, operations=operations or None)


spec.register_path_helper(path_from_method_view)


with application.test_request_context():
    for view in views:  # where `views` is an iterable of your `MethodView.as_view` funcs
        spec.add_path(method_view=view)  # now just use `method_view=view` instead of `view=view`

@sloria sloria added the bug label Jul 11, 2017

@sloria

This comment has been minimized.

Copy link
Member

commented Jul 11, 2017

I would certainly review and merge a PR fixing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.