Skip to content

Commit 7ad59e8

Browse files
committed
initial implementation of OAS 3.0 generateschema
1 parent bd8b535 commit 7ad59e8

File tree

15 files changed

+2074
-1
lines changed

15 files changed

+2074
-1
lines changed

CHANGELOG.md

+1
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

+96-1
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
@@ -32,6 +32,7 @@ REST_FRAMEWORK = {
3232
'rest_framework.renderers.BrowsableAPIRenderer'
3333
),
3434
'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata',
35+
'DEFAULT_SCHEMA_CLASS': 'rest_framework_json_api.schemas.openapi.AutoSchema',
3536
'DEFAULT_FILTER_BACKENDS': (
3637
'rest_framework_json_api.filters.QueryParameterValidationFilter',
3738
'rest_framework_json_api.filters.OrderingFilter',
@@ -876,3 +877,97 @@ The `prefetch_related` case will issue 4 queries, but they will be small and fas
876877
### Relationships
877878
### Errors
878879
-->
880+
881+
## Generating an OpenAPI Specification (OAS) 3.0 schema document
882+
883+
DRF 3.10 added a new management command: `generateschema` which can generate an
884+
[OAS 3.0 schema](https://www.openapis.org/) as a YAML or JSON file.
885+
886+
### Settings needed
887+
888+
In order to produce an OAS schema that properly represents the JSON:API structure,
889+
DJA has this same command as an override of DRF's. In order to make sure the DJA
890+
version of the command is used, you must add DJA **ahead of** DRF in the
891+
`INSTALLED_APPS` settings as in this example:
892+
```python
893+
INSTALLED_APPS = [
894+
'django.contrib.contenttypes',
895+
'django.contrib.staticfiles',
896+
'django.contrib.sites',
897+
'django.contrib.sessions',
898+
'django.contrib.auth',
899+
'rest_framework_json_api',
900+
'rest_framework',
901+
'polymorphic',
902+
'example',
903+
'debug_toolbar',
904+
'django_filters',
905+
]
906+
```
907+
908+
You'll also need to make sure you are using the DJA AutoSchema class, either as the default schema class or
909+
explicitly as a view's `schema`:
910+
911+
### Default schema class
912+
913+
```python
914+
REST_FRAMEWORK = {
915+
# ...
916+
'DEFAULT_SCHEMA_CLASS': 'rest_framework_json_api.schemas.openapi.AutoSchema',
917+
}
918+
```
919+
920+
### View-based
921+
922+
You can explicitly use DJA's AutoSchema in your view definition, optionally including an OAS schema document
923+
initializer:
924+
925+
```python
926+
from rest_framework_json_api.schemas.openapi import AutoSchema
927+
928+
openapi_schema = {
929+
'info': {
930+
'version': '1.0',
931+
'title': 'my demo API',
932+
'description': 'A demonstration of [OAS 3.0](https://www.openapis.org) AutoSchema',
933+
'contact': {
934+
'name': 'my name'
935+
},
936+
'license': {
937+
'name': 'BSD 2 clause',
938+
'url': 'https://github.com/django-json-api/django-rest-framework-json-api/blob/master/LICENSE',
939+
}
940+
},
941+
'servers': [
942+
{'url': 'https://localhost/v1', 'description': 'local docker'},
943+
{'url': 'http://localhost:8000/v1', 'description': 'local dev'},
944+
{'url': 'https://api.example.com/v1', 'description': 'demo server'},
945+
{'url': '{serverURL}', 'description': 'provide your server URL',
946+
'variables': {'serverURL': {'default': 'http://localhost:8000/v1'}}}
947+
]
948+
}
949+
950+
951+
class MyViewSet(ModelViewSet):
952+
schema = AutoSchema(openapi_schema=openapi_schema)
953+
```
954+
955+
To generate an OAS schema document, use something like:
956+
957+
```text
958+
$ django-admin generateschema --settings=example.settings >myschema.yaml
959+
```
960+
961+
You can then use any number of OAS tools such as
962+
[swagger-ui-watcher](https://www.npmjs.com/package/swagger-ui-watcher)
963+
to render the schema:
964+
```text
965+
$ swagger-ui-watcher myschema.yaml
966+
```
967+
968+
Note: Swagger-ui-watcher will complain that "DELETE operations cannot have a requestBody"
969+
but it will still work. This
970+
[error](https://github.com/OAI/OpenAPI-Specification/pull/1937)
971+
in the OAS specification is expected to be fixed soon.
972+
([swagger-ui](https://www.npmjs.com/package/swagger-ui) will work silently.)
973+

example/settings/dev.py

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
'django.contrib.sites',
2222
'django.contrib.sessions',
2323
'django.contrib.auth',
24+
'rest_framework_json_api',
2425
'rest_framework',
2526
'polymorphic',
2627
'example',
@@ -88,6 +89,7 @@
8889
'rest_framework.renderers.BrowsableAPIRenderer',
8990
),
9091
'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata',
92+
'DEFAULT_SCHEMA_CLASS': 'rest_framework_json_api.schemas.openapi.AutoSchema',
9193
'DEFAULT_FILTER_BACKENDS': (
9294
'rest_framework_json_api.filters.OrderingFilter',
9395
'rest_framework_json_api.django_filters.DjangoFilterBackend',

example/tests/snapshots/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)