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

Add "ParsedMethodView" and example #222

Merged
merged 1 commit into from
Aug 27, 2018

Conversation

strongbugman
Copy link
Contributor

#220

@rochacbruno I don't kwon if the file/class name/structure is suitable, feel free to tell me if I'm wrong.

@coveralls
Copy link

coveralls commented Aug 1, 2018

Coverage Status

Coverage increased (+0.9%) to 91.184% when pulling be670d6 on strongbugman:feature/add_parsed_view into 559764e on rochacbruno:master.

@rochacbruno
Copy link
Member

This is a great improvement, I'll try to test and review by this weekend!

If you can also take a look @javabrett :)

@rochacbruno
Copy link
Member

rochacbruno commented Aug 10, 2018

I Like the implementation with the ParseMethodView as a base class.

But I also think we can provide the same functionality for when only function based views are used, and also when no viewa at all are used. (this would address the issues #202 and #216

Example:

Swagger(.....parse_spec_path="path/to/docs")

Then we just parse the YAML and generate the swagger UI even without having any view defined.

@strongbugman
Copy link
Contributor Author

Thanks your suggestion, you are right, I will update this PR soon

@strongbugman strongbugman force-pushed the feature/add_parsed_view branch 12 times, most recently from 222caa6 to eb3c622 Compare August 15, 2018 15:18
@strongbugman
Copy link
Contributor Author

@rochacbruno You can review this PR now, the base idea is same with earlier "ParseMethodView", but it has more applicability:

  1. Auto find view function/method's swagger docs file by endpoint's name when "doc_dir" is configured
  2. Parse and validate all documented endpoint's request when pass "parse=True" to flasgger app.
  3. Move most code from APISpecsView to Flasgger, now we can get spec json by Flasgger.get_apispecs

Any suggestions or criticisms are welcome

@rochacbruno
Copy link
Member

@strongbugman this is awesome! I'll review ASAP!

Copy link
Collaborator

@javabrett javabrett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is fairly current, but please rebase to current master for final review.

From http://localhost:5000/parsed_view_func/apidocs/#/item/get_api_items__id__ I' I'm getting this error:

Errors
Hide
Resolver error at paths./api/items/{id}/.get.responses.200.schema.$ref
Could not resolve reference because of: Could not resolve pointer: /definitions/item does not exist in document

@@ -0,0 +1,170 @@
"""
Example of auto load doc file by endpoint name(view function/method name),
parse by flask_restful.reqparse.RequestParser and validate by jsonschema
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/parse/parsed
s/validate/validated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

from flasgger import Swagger


_TEST_META_SKIP_FULL_VALIDATION = True
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this active/needed, what does it do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it should be removed

def get(self):
"""
If we set "parse" is True in Flasgger app, we will get parsed and
validate data store in "flask.request".
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/validate/validated
s/store/stored

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

If we set "parse" is True in Flasgger app, we will get parsed and
validate data store in "flask.request".

Different location`s var store in different key, eg: key "args" store
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe ` -> ' here?

@strongbugman
Copy link
Contributor Author

@javabrett Thx for your review, I fixed the errors and rebased the origin/master

@javabrett
Copy link
Collaborator

Changes look good, 🍰 . I don't think your rebase picked up the latest master but never mind - you might have forgotten to fetch or use origin/master.

@strongbugman
Copy link
Contributor Author

@javabrett Yes you are right, I forgot to fetch upstream😅, now it's rebased

@rochacbruno
Copy link
Member

@strongbugman awesome!

@javabrett first, thanks for the awesome contribution to Flasgger reviewing and helping maintaining this project ✨

@javabrett I am with little time this week to review OSS projects (due to work + FlaskConf which is this weekend). So I would like to ask if you can take care of this PR! this is awesome improvement and I do not want to hold it. As long as the code is good, working and we have some kind of doc (readme) about how to use it please decide if you want to merge it or request more changes! I'll let this one for you to review/merge!

:) thanks

@javabrett javabrett self-assigned this Aug 21, 2018
Copy link
Collaborator

@javabrett javabrett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@strongbugman I don't have large blocks of time to work on the review at the moment, so hopefully you can live with my incremental suggestions :).

Left a comment on the auth in the example below.

400:
description: Miss name or code
"""
if request.parsed_data['json']['data']['password'] != 'test':
Copy link
Collaborator

@javabrett javabrett Aug 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding this, and related auth bits - I'm just wondering whether this is an essential part of the example code, or whether is it orthogonal and potentially confusing.

With the password, I had to check the example source to find it. It also seems a bit irregular auth passing the credentials with the data payload.

If auth is integral to the example, fine let's keep it, but if it is unnecessary and orthogonal, maybe we can have auth in other examples but not here. Thoughts?

When you next rebase you'll pick up the new Dockerfile which can be useful for testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is a test about nest json body and variable route rule without converter, but the "auth" feature is unnecessary, so I`ll change the example


app = Flask(__name__)
app.config['SWAGGER'] = {
'title': 'Flasgger ParsedMethodView Example',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding ParsedMethodView (three mentions, appears in UI) - on first seeing this in camel-case, I wondered if it was going to be a subclass of Flask MethodView, but it's not, it's just a name used in the example. So maybe "Flasgger Parsed MethodView Example" might be better?

flasgger/base.py Outdated
# "static_folder": "static", # must be set by user
"swagger_ui": True,
"specs_route": "/apidocs/",
"doc_dir": "./docs/api/"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc-parsing is new and presumably optional and opt-in, but doc_dir is being set in the DEFAULT_CONFIG here. As a result all existing examples are passing through the new code in get_specs, and the doc_dir=None is not having any effect.

So I guess I'm checking whether it is really intended to set this as a default here. If not, and especially if all other options are mentioned here, it might be OK to leave it commented per static_folder.

If the intention is to make this new feature default-on with some default path to the docs, then the parse option would need review.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc auto loading is a optional feature, although it won't affect flasgger's other feature if there is no ./docs/api in local directory, for more clearly, I'll remove the doc_dir config in DEFAULT_CONFIG

@strongbugman
Copy link
Contributor Author

@javabrett Thx for your suggestions and patience, I changed the code according to your suggestions

@javabrett javabrett merged commit aaef05c into flasgger:master Aug 27, 2018
@javabrett
Copy link
Collaborator

Thanks @strongbugman for this contribution! 🍰

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants