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
Add support for Flask MethodView to apispec.ext.flask #134
Add support for Flask MethodView to apispec.ext.flask #134
Conversation
The `apispec.utils.load_yaml_from_docstring` function as used by the `apispec.ext.flask.path_from_view` helper supports both HTTP methods ('get', 'post', etc) as well as "x-^" extensions. This commit allows the `apispec.ext.flask.path_from_method_view` helper to extract "x-^" extensions YAML from the class docstring and method YAML from the individual methods' docstrings. It also demostrates the different in the `apispec.ext.flask` module docstring so that the usage is clear in the API docs.
@djanderson your code uses parameter name "method_view" != "view" in path_helper_function, then it cannot be used with apispec.ext.marshmallow (cause TypeError).
I think MethodView support is useful. Thanks! |
One more change is required to use it with apispec.ext.marshmallow. Currently operations constructed in the preceding extension is not passed to the subsequent extensions. |
Hi @yoichi, thanks for the feedback. I hate to ask, but could you give me an example of something that throws a TypeError? Like maybe propose a test case I can add that shows the error? I'm currently using this with |
I'm sorry for lack of explanation.
to APISpec constructor, TypeError is occured and ignored in add_path(method_view=method_view) (not thrown to outside) when calling on apispec.ext.marshmallow.schema_path_helper and the method never executed since argument 'view' is not given. As I noted, there was another problem (#138). While I fixing it, I found tornado extension uses 'urlspec' (!= 'view') argument and have the same problem as above, and I had to make 'view' argument of apispec.ext.marshmallow.schema_path_helper optional for backward compatibility. On the other hand, I think it is better that the implementation will not rely on the fact one of followings cause TypeError internally and not called:
I prefer an explicit conditional branch in path helper function instead. Sincelery, |
I see, thanks for the clarification. I don't disagree with you, but I probably won't address it in this PR since I don't think I'm abusing the TypeError in a way that's not currently intended. I appreciate the review, though! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've also put comments in the code to clarify what I mean.
But I will respect your choice if you decide that you will not deal with it.
Thanks,
def setup(spec): | ||
"""Setup for the plugin.""" | ||
spec.register_path_helper(path_from_view) | ||
spec.register_path_helper(path_from_method_view) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to register multiple path helper functions in one extension? I think this breaks the locality of the conditional branch whether the view is derived from MethodView or not.
@@ -72,6 +111,20 @@ def path_from_view(spec, view, **kwargs): | |||
path = Path(path=path, operations=operations) | |||
return path | |||
|
|||
def path_from_method_view(spec, method_view, **kwargs): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using 'method_view' (not 'view' as used in existing helper function) forces user to distinguish the view is derived from MethodView or not.
I would love to see this merged and released. Any chance you could find the time to resolve the last few things, @djanderson, @yoichi? |
@tangram, thanks for the encouragement. I think this implementation was faithful to the "ask forgiveness, not permission" style of the existing code, but @yoichi has raised some fair criticisms and his modifications in #139 look to provide the desired capability as well as an improved user experience. I'm closing this PR so that we can focus on landing #139. @yoichi, thanks again for the critical eye and improvements. |
Addresses issues #85 and #125.
There are only two slight differences when using a MethodView function:
spec.add_path(method_view=view)
Here's the patch in action:
Please review!