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

Create Schema view for endpoints #19

Closed
MissiaL opened this issue Mar 31, 2020 · 13 comments
Closed

Create Schema view for endpoints #19

MissiaL opened this issue Mar 31, 2020 · 13 comments

Comments

@MissiaL
Copy link
Contributor

MissiaL commented Mar 31, 2020

In drf_yasg I can get the schema and put it in endpoint automatically. Could you implement the same functionality?

...
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

...

schema_view = get_schema_view(
   openapi.Info(
      title="Snippets API",
      default_version='v1',
      description="Test description",
      terms_of_service="https://www.google.com/policies/terms/",
      contact=openapi.Contact(email="contact@snippets.local"),
      license=openapi.License(name="BSD License"),
   ),
   public=True,
   permission_classes=(permissions.AllowAny,),
)

urlpatterns = [
   url(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
   url(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
   url(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
   ...
]

Now I use the template and do it manually. I read the schema from the file and then use the swagger template

@tfranzel
Copy link
Owner

don't we already have that with https://github.com/tfranzel/drf-spectacular/blob/master/drf_spectacular/views.py#L16 ?

Only difference is you have to setup the settings in SPECTACULAR_SETTINGS and just load SpectacularApiView in your urlpatterns.

the caching mechanism that yasg provides is missing though.

@MissiaL
Copy link
Contributor Author

MissiaL commented Mar 31, 2020

Wow!
I did not notice this! Thanks!

@MissiaL MissiaL closed this as completed Mar 31, 2020
@aniforprez
Copy link

I suggest this be documented in the README or somewhere. There's also no documentation for the settings available in the package

@tfranzel
Copy link
Owner

tfranzel commented Apr 9, 2020

@aniforprez actually there is "documentation" for the settings: https://drf-spectacular.readthedocs.io/en/latest/settings.html

you made a good point though. the readme could be better and some feature are not obvious from the get-go. i updated https://github.com/tfranzel/drf-spectacular#take-it-for-a-spin (schema view) and usage (for the settings).

@aniforprez
Copy link

aniforprez commented Apr 10, 2020

Very nice! I also recommend at some point adding some examples with swagger or redoc or both so there's immediately something they can visually look at rather than only getting the file which is hard to parse through visually

Btw nice work!

@vladdoster
Copy link

How exactly do I get it to serve the UI? I added it to my URLS but everytimg I go to the path it just downloads the schema?

@tfranzel
Copy link
Owner

tfranzel commented May 5, 2020

@vladdoster we do not serve the UI ourselves:

swagger-ui:
$ ./manage.py spectacular --file schema.yml
$ docker run -p 80:8080 -e SWAGGER_JSON=/schema.yml -v ${PWD}/schema.yml:/schema.yml swaggerapi/swagger-ui

redoc:
serve this html with your api https://github.com/Redocly/redoc#tldr and replace the schema url in it.

@MissiaL
Copy link
Contributor Author

MissiaL commented May 5, 2020

I had to take the implementation from the drf_yasg package
It’s actually very convenient when you can display UI on your django server

I added a swagger template and give it a schema

@tfranzel
Copy link
Owner

tfranzel commented May 5, 2020

i was not aware that serving swagger-ui through a template like redoc is that simple. in that case it might be not a bad idea to add that convenience. i'll have a look

@MissiaL MissiaL reopened this May 5, 2020
@vladdoster
Copy link

Thank you so much @MissiaL && @tfranzel !

@tfranzel
Copy link
Owner

tfranzel commented May 5, 2020

added swagger&redoc and updated the README with new instructions. let me know if that works for you.

@vladdoster
Copy link

@tfranzel will give it a go tonight

@tfranzel
Copy link
Owner

tfranzel commented May 6, 2020

i used both redoc and swagger today. looks good to me.

@tfranzel tfranzel closed this as completed May 6, 2020
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

No branches or pull requests

4 participants