-
Notifications
You must be signed in to change notification settings - Fork 14
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
swagger_ui: add directives that provide API Explorer and spec information #79
Conversation
easy way to register and serve API explorer for Swagger/OpenAPI schemas
3 similar comments
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.
Thanks for your contribution!
Will the old method still work? I guess so right? If I don't call the new config directive, I can still do it the old way...
I think it's cool to have this, as long as it remains optional, but I'd rather let Gabi give a final review :)
<head> | ||
<meta charset="UTF-8"> | ||
<title>Swagger UI</title> | ||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Source+Code+Pro:300,600|Titillium+Web:400,600,700" rel="stylesheet"> |
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.
nitpick: Ship the font here maybe instead of leaking to google?
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.
The old method will still work.
We would need to ship statics and update them in the package - the fonts link exists also in the official Swagger UI package, its not something I've made - I did point to swagger UI JS files on CDN to avoid shipping big statics.
docs/source/quickstart.rst
Outdated
|
||
Then you will be able to access Swagger UI API explorer on url: | ||
|
||
http://localhost:8000/api-explorer (in case of example below) |
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.
above :)
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.
Yup, I've just added some defaults for this to work, no functionality of existing package is removed or altered.
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 think what @leplatrem said is the the example is literally above (not below). Also you can only use "in the example above" (remove "case of").
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.
Ah, got confused because I was thinking about the "minimalist example below" :)
docs/source/tutorial.rst
Outdated
|
||
|
||
If you don't know what this is about or need more information, please check the | ||
`Pyramid documentation <http://docs.pylonsproject.org/projects/pyramid>`_ | ||
|
||
By default API explorer will be served under `/api-explorer` path in your | ||
application. You can easily configure the paths, required pyramid view | ||
permissions or route factory. |
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 think it feels more natural to read configure the paths, required permissions and Pyramid route factory
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.
Thanks, not a native speaker will correct that.
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 think it's worth mention that additional kwargs should match the the generate()
arguments.
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.
Thanks! Will fix that.
docs/source/tutorial.rst
Outdated
Custom Swagger UI <script> bootstrap | ||
==================================== | ||
|
||
By default standard Swagger UI config is used, but you can customize the |
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.
link to Swagger UI project?
docs/source/tutorial.rst
Outdated
|
||
The default one is: | ||
|
||
`cornice_swagger.swagger_ui_script_generator = cornice_swagger.views:swagger_ui_script_template` |
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.
In RST you have to double back quotes
docs/source/tutorial.rst
Outdated
|
||
`cornice_swagger.swagger_ui_script_generator = cornice_swagger.views:swagger_ui_script_template` | ||
|
||
It points to following callable that accepts request object: |
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.
to the following
a request object
cornice_swagger/__init__.py
Outdated
route_name='cornice_swagger.swagger_ui_path') | ||
config.add_view('cornice_swagger.views.swagger_json_view', | ||
renderer='json', permission=permission, | ||
route_name='cornice_swagger.swagger_api_path') |
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.
My feedback would be :
- I like the idea of using a config directive
- I would find a better name, not using the deprecated swagger name, and not mentioning UI (
enable_openapi()
?) - I would set
swagger_ui_path
to None by default - I would would not register the UI views if the
swagger_ui_path
is None
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.
Sure, makes sense, let me correct that.
cornice_swagger/views.py
Outdated
ui_js_bundle_url = 'https://cdnjs.cloudflare.com/ajax/libs/' \ | ||
'swagger-ui/3.12.9/swagger-ui-bundle.js' | ||
ui_js_standalone_url = 'https://cdnjs.cloudflare.com/ajax/libs/' \ | ||
'swagger-ui/3.12.9/swagger-ui-standalone-preset.js' |
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.
nitpick: move as module constants?
doc = cornice_swagger.CorniceSwagger(cornice.service.get_services()) | ||
kwargs = request.registry.settings['cornice_swagger.spec_kwargs'] | ||
my_spec = doc.generate(**kwargs) | ||
return my_spec |
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 think there should be an example on how to replace this with a custom view.
For example, in Kinto we inherit the CorniceSwagger
class and instantiate our own stuff the the 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.
I think if you want to replace this with a custom view - then you don't use the directive, because it basicly just adds 2 views so you don't have to override them. If someone wants to override they'd have to redo all the work directive does basicly.
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.
Looking good! I like the idea of using a directive instead of manually adding a service to serve the documentation. I've got a few minor suggestions...
docs/source/tutorial.rst
Outdated
|
||
|
||
If you don't know what this is about or need more information, please check the | ||
`Pyramid documentation <http://docs.pylonsproject.org/projects/pyramid>`_ | ||
|
||
By default API explorer will be served under `/api-explorer` path in your | ||
application. You can easily configure the paths, required pyramid view | ||
permissions or route factory. |
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 think it's worth mention that additional kwargs should match the the generate()
arguments.
def openAPI_spec(request): | ||
doc = CorniceSwagger(get_services()) | ||
my_spec = doc.generate('MyAPI', '1.0.0') | ||
return my_spec |
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.
Maybe move this to another example?
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 think the minimalist example should show the easiest way to archieve that?
How to generate the docs is mentioned above so I'm not sure if it makes sense to duplicate the example just show that.
cornice_swagger/__init__.py
Outdated
config.add_directive('register_swagger_ui', register_swagger_ui) | ||
|
||
|
||
def register_swagger_ui( |
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.
maybe registering theapi_path
could be a separate directive, thus allowing one to register the api docs without using the api_explorer. What do you think?
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.
Sure, sounds like a sane change.
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.
@gabisurita any suggestions of what we want to name the other directive?
Hi @gabisurita, @leplatrem I've tried to do everything according to your feedback. We have now two directives, one just renders simple spec json one renders explorer. |
register_swagger_ui
directive that provides
Thanks it looks great :) I have a little doubt left about naming being a little bit too vague. I feel that I'd prefer |
Sure, let me correct that. |
@leplatrem Done, the PR now has updated function names. |
@ergo Just one nipick in the docs and we can GTM. |
@gabisurita I've fixed the docs. |
@ergo Thanks! Just merged and released 0.6.0. |
easy way to register and serve API explorer for Swagger/OpenAPI schemas