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

swagger_ui: add directives that provide API Explorer and spec information #79

Merged
merged 4 commits into from
Mar 28, 2018

Conversation

ergo
Copy link
Collaborator

@ergo ergo commented Mar 25, 2018

easy way to register and serve API explorer for Swagger/OpenAPI schemas

easy way to register and serve API explorer for Swagger/OpenAPI schemas
@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 8d647a0 on ergo:master into a5182fe on Cornices:master.

3 similar comments
@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 8d647a0 on ergo:master into a5182fe on Cornices:master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 8d647a0 on ergo:master into a5182fe on Cornices:master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 8d647a0 on ergo:master into a5182fe on Cornices:master.

@coveralls
Copy link

coveralls commented Mar 25, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling 41e4886 on ergo:master into a5182fe on Cornices:master.

Copy link
Contributor

@leplatrem leplatrem left a 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">
Copy link
Contributor

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?

Copy link
Collaborator Author

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.


Then you will be able to access Swagger UI API explorer on url:

http://localhost:8000/api-explorer (in case of example below)
Copy link
Contributor

Choose a reason for hiding this comment

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

above :)

Copy link
Collaborator Author

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.

Copy link
Collaborator

@gabisurita gabisurita Mar 27, 2018

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").

Copy link
Collaborator Author

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" :)



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.
Copy link
Contributor

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

Copy link
Collaborator Author

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.

Copy link
Collaborator

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks! Will fix that.

Custom Swagger UI <script> bootstrap
====================================

By default standard Swagger UI config is used, but you can customize the
Copy link
Contributor

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?


The default one is:

`cornice_swagger.swagger_ui_script_generator = cornice_swagger.views:swagger_ui_script_template`
Copy link
Contributor

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


`cornice_swagger.swagger_ui_script_generator = cornice_swagger.views:swagger_ui_script_template`

It points to following callable that accepts request object:
Copy link
Contributor

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

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')
Copy link
Contributor

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

Copy link
Collaborator Author

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.

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'
Copy link
Contributor

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
Copy link
Contributor

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 :)

https://github.com/Kinto/kinto/blob/8db533939f47d33c44deb66e0d1285258d99d68b/kinto/core/views/openapi.py#L22-L32

Copy link
Collaborator Author

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.

Copy link
Collaborator

@gabisurita gabisurita left a 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...



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.
Copy link
Collaborator

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
Copy link
Collaborator

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?

Copy link
Collaborator Author

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.

config.add_directive('register_swagger_ui', register_swagger_ui)


def register_swagger_ui(
Copy link
Collaborator

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?

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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?

@ergo
Copy link
Collaborator Author

ergo commented Mar 27, 2018

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.

@ergo ergo changed the title swagger_ui: add a register_swagger_ui directive that provides swagger_ui: add directives that provide API Explorer and spec information Mar 27, 2018
@leplatrem
Copy link
Contributor

Thanks it looks great :)

I have a little doubt left about naming being a little bit too vague. I feel that I'd prefer cornice_enable_openapi_view() and cornice_enable_openapi_explorer().
What do you think? Otherwise I'm +1 on landing this :)

@ergo
Copy link
Collaborator Author

ergo commented Mar 27, 2018

Sure, let me correct that.

@ergo
Copy link
Collaborator Author

ergo commented Mar 27, 2018

@leplatrem Done, the PR now has updated function names.

@gabisurita
Copy link
Collaborator

gabisurita commented Mar 27, 2018

@ergo Just one nipick in the docs and we can GTM.

@ergo
Copy link
Collaborator Author

ergo commented Mar 27, 2018

@gabisurita I've fixed the docs.

@gabisurita gabisurita merged commit 5b2e383 into Cornices:master Mar 28, 2018
@gabisurita
Copy link
Collaborator

@ergo Thanks! Just merged and released 0.6.0.

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.

4 participants