question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make the library compatible with drf-yasg library

See original GitHub issue

https://github.com/axnsan12/drf-yasg is the best swagger generator out there by far.

Unfortunately, it is not possible to integrate with wagtail due to the current API design.

The error when generating swagger schema:

Traceback (most recent call last):
  File "/home/code/swehq/newgifiter/gifiter/blog/venv/lib/python3.6/site-packages/drf_yasg/inspectors/view.py", line 94, in get_view_serializer
    return self.view.get_serializer()
  File "/home/code/swehq/newgifiter/gifiter/blog/venv/lib/python3.6/site-packages/rest_framework/generics.py", line 110, in get_serializer
    serializer_class = self.get_serializer_class()
  File "/home/code/swehq/newgifiter/gifiter/blog/venv/lib/python3.6/site-packages/wagtail/api/v2/endpoints.py", line 300, in get_serializer_class
    model = type(self.get_object())
  File "/home/code/swehq/newgifiter/gifiter/blog/venv/lib/python3.6/site-packages/wagtail/api/v2/endpoints.py", line 437, in get_object
    base = super().get_object()
  File "/home/code/swehq/newgifiter/gifiter/blog/venv/lib/python3.6/site-packages/rest_framework/generics.py", line 94, in get_object
    (self.__class__.__name__, lookup_url_kwarg)
AssertionError: Expected view PagesAdminAPIEndpoint to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly.
Internal Server Error: /swagger/
Traceback (most recent call last):
  File "/home/code/swehq/newgifiter/gifiter/blog/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/home/code/swehq/newgifiter/gifiter/blog/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 158, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/code/swehq/newgifiter/gifiter/blog/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 156, in _get_response
    response = response.render()
  File "/home/code/swehq/newgifiter/gifiter/blog/venv/lib/python3.6/site-packages/django/template/response.py", line 106, in render
    self.content = self.rendered_content
  File "/home/code/swehq/newgifiter/gifiter/blog/venv/lib/python3.6/site-packages/rest_framework/response.py", line 72, in rendered_content
    ret = renderer.render(self.data, accepted_media_type, context)
  File "/home/code/swehq/newgifiter/gifiter/blog/venv/lib/python3.6/site-packages/drf_yasg/renderers.py", line 34, in render
    return codec.encode(data)
  File "/home/code/swehq/newgifiter/gifiter/blog/venv/lib/python3.6/site-packages/drf_yasg/codecs.py", line 73, in encode
    raise SwaggerValidationError("spec validation failed", errors, spec, self)
drf_yasg.errors.SwaggerValidationError: spec validation failed

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
thclarkcommented, May 23, 2019

This is a bit finicky to achieve, but can be done with a polymorphic serialiser, and generally bypasses wagtail’s API to use DRF out of the box, relying on Wagtail serializers and fields. Example here, showing different page schema on the /pages_read endpoint (see the different response samples for HomePage, AboutPage, etc).

I’m still getting StreamField to work, though.

@jasonHav If folks are interested and can raise money to sponsor for a couple of weeks’ work for me (I have a couple of weeks spare at end of april / early may) I could tidy what I’ve done as a package on pypi (drf-wagtail, maybe) with a couple of examples.

0reactions
lb-commented, May 13, 2020

Based on validation from @lingster (below), we will close this issue as it appears to be resolved. If this still is a problem for some specific scenario it would be good for a new bug to be raised with exact steps to reproduce.

The generation of the swagger schema works correctly for wagtail >=2.8 version including the current 2.10rc. I have tested as well with drf_yasg == 1.17.1 and django >2.2 and 3.0.x There is an AssertionError raised when loading the swagger schema in 2.8 but this does not appear to impact the loading of the swagger page. The error is: AssertionError: Expected view PagesAPIViewSet to be called with a URL keyword argument named "pk". Fix your URL conf, or set the .lookup_fieldattribute on the view correctly. https://github.com/wagtail/wagtail/pull/4922#issuecomment-626399255

Read more comments on GitHub >

github_iconTop Results From Across the Web

drf-yasg - PyPI
Generate real Swagger/OpenAPI 2.0 specifications from a Django Rest Framework API. Compatible with. Django Rest Framework: 3.10, 3.11, 3.12.
Read more >
drf-yasg Documentation - Read the Docs
Generate real Swagger/OpenAPI 2.0 specifications from a Django Rest Framework API. Compatible with. • Django Rest Framework: 3.10, 3.11, ...
Read more >
Django - ImportError: Could not import 'drf_yasg.generators ...
I'm trying to add https://github.com/axnsan12/drf-yasg this library to our django ...
Read more >
How to Create More Understandable APIs in Python
The first step to create APIs in Python is to set up the environment to explore how to use drf-yasg: 1. Install the...
Read more >
Documenting an API implemented with Django Rest Framework
We followed the instruction to get started with drf-yasg and browsed ... by redoc or swagger-ui, both Javascript libraries, in the browser ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found