Skip to content

Commit 5b8341d

Browse files
committed
initial implementation of OAS 3.0 generateschema
1 parent 7c6cceb commit 5b8341d

File tree

12 files changed

+1928
-3
lines changed

12 files changed

+1928
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This release is not backwards compatible. For easy migration best upgrade first
1616
### Added
1717

1818
* Add support for Django REST framework 3.10.
19+
* Add support for `generateschema` management command.
1920

2021
### Removed
2122

docs/usage.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
`
22
# Usage
33

44
The DJA package implements a custom renderer, parser, exception handler, query filter backends, and
@@ -876,3 +876,48 @@ The `prefetch_related` case will issue 4 queries, but they will be small and fas
876876
### Relationships
877877
### Errors
878878
-->
879+
880+
## Generating an OpenAPI Specification (OAS) 3.0 schema document
881+
882+
DRF 3.10 added a new management command: `generateschema` which can generate an
883+
[OAS 3.0 schema](https://www.openapis.org/) as a YAML or JSON file.
884+
885+
In order to produce an OAS schema that properly represents the JSON:API structure,
886+
DJA has this same command as an override of DRF's. In order to make sure the DJA
887+
version of the command is used, you must add DJA **ahead of** DRF in the
888+
`INSTALLED_APPS` settings as in this example:
889+
```python
890+
INSTALLED_APPS = [
891+
'django.contrib.contenttypes',
892+
'django.contrib.staticfiles',
893+
'django.contrib.sites',
894+
'django.contrib.sessions',
895+
'django.contrib.auth',
896+
'rest_framework_json_api',
897+
'rest_framework',
898+
'polymorphic',
899+
'example',
900+
'debug_toolbar',
901+
'django_filters',
902+
]
903+
```
904+
905+
To generate an OAS schema document, use something like:
906+
907+
```text
908+
$ django-admin generateschema --settings=example.settings >myschema.yaml
909+
```
910+
911+
You can then use any number of OAS tools such as
912+
[swagger-ui-watcher](https://www.npmjs.com/package/swagger-ui-watcher)
913+
to render the schema:
914+
```text
915+
$ swagger-ui-watcher myschema.yaml
916+
```
917+
918+
Note: Swagger-ui-watcher will complain that "DELETE operations cannot have a requestBody"
919+
but it will still work. This
920+
[error](https://github.com/OAI/OpenAPI-Specification/pull/1937)
921+
in the OAS specification is expected to be fixed soon.
922+
([swagger-ui](https://www.npmjs.com/package/swagger-ui) will work silently.)
923+

example/settings/dev.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import os
22

3+
from rest_framework import VERSION as DRFVERSION
4+
5+
drf_version = tuple(int(x) for x in DRFVERSION.split('.'))
6+
37
SITE_ID = 1
48
DEBUG = True
59

@@ -21,6 +25,7 @@
2125
'django.contrib.sites',
2226
'django.contrib.sessions',
2327
'django.contrib.auth',
28+
'rest_framework_json_api',
2429
'rest_framework',
2530
'polymorphic',
2631
'example',
@@ -99,3 +104,6 @@
99104
),
100105
'TEST_REQUEST_DEFAULT_FORMAT': 'vnd.api+json'
101106
}
107+
108+
if drf_version >= (3, 10):
109+
REST_FRAMEWORK['DEFAULT_SCHEMA_CLASS'] = 'rest_framework_json_api.schemas.openapi.AutoSchema'

0 commit comments

Comments
 (0)