DRF - Yet another Swagger generator 2
Automated generation of real Swagger/OpenAPI 2.0 schemas from Django REST Framework code.
Notice
The original drf-yasg repository is now maintained again. This repo will be left as is to serve those still using it.
Please migrate back to the using the origional repository and for submitting contributions.
Status
| Source | Shields |
|---|---|
| Project | |
| Publishers | |
| Downloads | |
| Raised |
Compatibility
| Release | Python | Django | Django REST Framework |
|---|---|---|---|
| 1.18.x | 2.7 | 1.11 | 3.8 - 3.9 |
| 1.18.x | 3.6 - 3.8 | 2.2 - 3.0 | 3.8 - 3.12 |
| 1.19.x | 3.6 - 3.9 | 2.2 - 3.1 | 3.8 - 3.12 |
Features
-
Full support for nested Serializers and Schemas
-
Response schemas and descriptions
-
Model definitions compatible with codegen tools
-
Customization hooks at all points in the spec generation process
-
JSON and YAML format for spec
-
Bundles latest version of swagger-ui and redoc for viewing the generated documentation
-
Schema view is cacheable out of the box
-
Generated Swagger schema can be automatically validated by swagger-spec-validator
-
Supports Django REST Framework API versioning with
URLPathVersioningandNamespaceVersioning; other DRF or custom versioning schemes are not currently supported
Screenshots
Fully nested request and response schemas
Choose between redoc and swagger-ui
Model definitions
Installing
Installing the package from pypi:
pip install drf-yasg2
Given the numerous methods to manually customize the generated schema, it makes sense to validate the result to ensure it still conforms to OpenAPI 2.0. To this end, validation is provided at the generation point using Python Swagger libraries, and can be activated by passing validators=['ssv'] to get_schema_view; if the generated schema is not valid, a SwaggerValidationError is raised by the handling codec.
To provide the built-in validation mechanisms you can install the extra requirements:
pip install drf-yasg2[validation]
Usage
Checkout the live demo!
Add the package to INSTALLED_APPS:
INSTALLED_APPS = [
...
'drf_yasg2',
...
]
Add the endpoints to urlpatterns:
from django.urls import path, re_path
from rest_framework import permissions
from drf_yasg2.views import get_schema_view
from drf_yasg2 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 = [
re_path(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
...
]
This exposes 4 endpoints:
- A JSON view of your API specification at
/swagger.json - A YAML view of your API specification at
/swagger.yaml - A swagger-ui view of your API specification at
/swagger/ - A ReDoc view of your API specification at
/redoc/
Docs
Additional details are available in the full documentation and the changelog.
To generate the documentation locally:
scripts/docs.sh
Tests
To run tests:
tox
Contributing
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests.
Versioning
SemVer is used for versioning. For a list of versions available, see the tags on this repository.
Authors
This project forked from drf-yasg. Credit is given to Cristi Vîjdea and the original contributors.
-
Cristi Vîjdea - Initial work - Cristi Vîjdea
-
Joel Lefkowitz - This fork's maintainer - Joel Lefkowitz
Huge thank you to the contributors who've participated in this project. Have a look at the contributions!
License
This project is licensed under the BSD 3-Clause License - see the LICENSE.md file for details
Metadata
Release files for drf-yasg2 1.19.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| drf_yasg2-1.19.4.tar.gz | 1.5 MB | Without provenance | Details for drf_yasg2-1.19.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| drf_yasg2-1.19.4-py3-none-any.whl | Python 3 | none | any | Details for drf_yasg2-1.19.4-py3-none-any.whl |
Total release size: 3.1 MB
Release files / drf_yasg2-1.19.4.tar.gz
| Download URL | drf_yasg2-1.19.4.tar.gz |
|---|---|
| Size | 1.5 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7037a8041eb5d1073fa504a284fc889685f93d0bfd008a963db1b366db786734
|
|
BLAKE2b-256 checksum How to use checksums |
1b67d5e9cf08a315615e477934812025338e5637f8ce33514bb5712fd8d667f0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.6.7
|
Release files / drf_yasg2-1.19.4-py3-none-any.whl
| Download URL | drf_yasg2-1.19.4-py3-none-any.whl |
|---|---|
| Size | 1.6 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
75e661ca5cf15eb44fcfab408c7b864f87c20794f564aa08b3a31817a857f19d
|
|
BLAKE2b-256 checksum How to use checksums |
d4975b067df5722aee9e3ceec2d0de136b01c54d1311ab379e43bcf93f9f5c18
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.6.7
|
Security for drf-yasg2 1.19.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.