Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework Swagger stopped working

Just tried to rebuild a container with DRF and drf-yasg. The exact same commit was passing all tests fine but is now falling over with the following exception:

ImportError: Could not import 'rest_framework.schemas.coreapi.AutoSchema' for API setting 'DEFAULT_SCHEMA_CLASS'. ModuleNotFoundError: No module named 'rest_framework.schemas.coreapi'.

Nothing else has changed, but it seems a newer package may have been included that broke the Swagger generator.

Anyone else experience similar?

So it seems pip was pulling DRF V3.10, which has some switch from CoreAPI to OpenAPI: https://www.django-rest-framework.org/community/3.10-announcement/ . Adding the line from the release documentation:

REST_FRAMEWORK = {
  ...
  'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema'
}

did not seem to make any difference.

like image 892
Stuart Buckingham Avatar asked Jul 15 '19 20:07

Stuart Buckingham


People also ask

Is Django REST swagger deprecated?

Django REST Swagger: deprecated (2019-06-04) This project is no longer being maintained.

Can I use swagger with Django?

Improved performance. Allow multiple instances of Swagger UI in a single Django project. Allow rendering the OpenAPI JSON spec independently.

What is swagger in Django REST Framework?

drf-yasg is a Swagger generation tool implemented without using the schema generation provided by Django Rest Framework. It aims to implement as much of the OpenAPI specification as possible - nested schemas, named models, response bodies, enum/pattern/min/max validators, form parameters, etc.

What is renderers in Django REST Framework?

The rendering process takes the intermediate representation of template and context, and turns it into the final byte stream that can be served to the client. REST framework includes a number of built in Renderer classes, that allow you to return responses with various media types.


1 Answers

I would presume your dependencies in requirements.txt are not specific enough, and rebuilding the container has installed a later version of djangorestframework.

Check for a line in your pipfile like djangorestframework>=3.9, and this should be changed to either pin a specific version djangorestframework==3.9, or pin it to a specific minor release so you will still receive bug fixes and security updates djangorestframework>=3.9,<3.10.

These lines can also be used directly with pip, incase your container build uses pip directly, e.g. pip install "djangorestframework>=3.9,<3.10"

like image 130
A. J. Parr Avatar answered Oct 10 '22 20:10

A. J. Parr