Skip to content
This repository has been archived by the owner on Jan 11, 2021. It is now read-only.

URLs by router are not detected by swagger #479

Closed
humitos opened this issue Jul 15, 2016 · 7 comments
Closed

URLs by router are not detected by swagger #479

humitos opened this issue Jul 15, 2016 · 7 comments

Comments

@humitos
Copy link

humitos commented Jul 15, 2016

Hi, after upgrading to django-rest-swagger 2.0 my url defined by the router are not detected

# urls.py

router = MyRouter()

router.register(r'search', search_views.SearchViewSet, base_name='search')
router.register(r'reservations', reservations_views.ReservationsViewSet, base_name='reservation')

urlpatterns = router.urls
urlpatterns += url(r'^admin/', admin.site.urls),
urlpatterns += url(r'^docs/', schema_view),

Then, when I hit /docs I only see the documentation for /docs endpoint.

What I am doing wrong?

I was working properly with the previous version of django-rest-swagger.

@marcgibbons
Copy link
Owner

Checked this out. Quick suggestion is to write a schema view as shown in the quick start example. It is possible to get this working with a router, but DRF does not yet fully support the SwaggerUIRenderer as the root view renderer when used on the DefaultRouter. We should open an issue there

However, you can get it "almost" working with the default router if you do the following:

router = DefaultRouter(
    schema_title='Pastebin API',
    schema_renderers=[OpenAPIRenderer, SwaggerUIRenderer]
)

@marcgibbons
Copy link
Owner

Then, from the browser, you will need to add ?format=swagger to your URL. Or else, you'll get the yellow screen of death (since in this example the CoreJSON renderer is omitted)

Ideally, we'd want to be able to set the SwaggerUIRenderer as the root view's renderer, not just the schema, but that currently is not possible

https://github.com/tomchristie/django-restramework/blob/master/rest_framework/routers.py#L292

@humitos
Copy link
Author

humitos commented Jul 15, 2016

@marcgibbons thanks for your comments

I've created the schema view as the quick start example and mapped to /docs in my urls.py file.

I'm using a custom router (inherit from SimpleRouter) so the schema_renderers is not a valid attribute. On the other hand, I try just changing the my own router to the DefaultRouter using the parameters you has told me but I have the same problem.

I'm not sure what to try now.

The link you pointed me on django-restframework doesn't work.

Taking advance on this discussion I want to ask you: how are we going to customize the documentation if YAML is not supported anymore? I didn't found anything in the new documentation.

@marcgibbons
Copy link
Owner

@humitos If you are using the DefaultRouter as the schema source, then you will not need the schema_view.

Have you tried running the example_app included in this project?

The urls.py can be modified to use the DefaultRouter as the serving point for the docs.

router = DefaultRouter(
    schema_title='Pastebin API',
    schema_renderers=[OpenAPIRenderer, SwaggerUIRenderer]
)

router.register(r'snippets', views.SnippetViewSet)
router.register(r'users', views.UserViewSet)

urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

Then, to view, you would open http://localhost:8000/?format=swagger

As for customization, the idea is that we are pushing this responsibility up to DRF. I do plan on implementing a feature which would allow the inclusion of OpenAPI $ref either in YAML or JSON

@arielpontes
Copy link

I have the same problem. I followed the instructions in this discussion but it still doesn't work. I get these other errors now.

@Zowie
Copy link

Zowie commented Aug 3, 2016

I just stumbled upon this issue. Using djangorestframework==3.4.1 and django-rest-swagger==2.0.3, it seems this works now :) I'm using the schema_view as shown in the docs.

@angvp
Copy link

angvp commented Oct 21, 2016

@humitos @marcgibbons this seems to be addressed as well, can we close it?

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

No branches or pull requests

5 participants