Skip to content

Commit d562a95

Browse files
committed
missing documentation of AutoSchema
1 parent 7178c49 commit d562a95

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

docs/usage.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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',
@@ -882,6 +883,8 @@ The `prefetch_related` case will issue 4 queries, but they will be small and fas
882883
DRF 3.10 added a new management command: `generateschema` which can generate an
883884
[OAS 3.0 schema](https://www.openapis.org/) as a YAML or JSON file.
884885

886+
### Settings needed
887+
885888
In order to produce an OAS schema that properly represents the JSON:API structure,
886889
DJA has this same command as an override of DRF's. In order to make sure the DJA
887890
version of the command is used, you must add DJA **ahead of** DRF in the
@@ -902,12 +905,58 @@ INSTALLED_APPS = [
902905
]
903906
```
904907

908+
You'll also need to make sure you are using the DJA AutoSchema class, either as the default schema class:
909+
910+
```python
911+
REST_FRAMEWORK = {
912+
# ...
913+
'DEFAULT_SCHEMA_CLASS': 'rest_framework_json_api.schemas.openapi.AutoSchema',
914+
}
915+
```
916+
917+
### View-based
918+
919+
Or you can explicit use DJA's AutoSchema it in your view definition, possible including an OAS schema document
920+
initializer:
921+
922+
```python
923+
from rest_framework_json_api.schemas.openapi import AutoSchema
924+
925+
openapi_schema = {
926+
'info': {
927+
'version': '1.0',
928+
'title': 'my demo API',
929+
'description': 'A demonstration of [OAS 3.0](https://www.openapis.org) AutoSchema',
930+
'contact': {
931+
'name': 'my name'
932+
},
933+
'license': {
934+
'name': 'BSD 2 clause',
935+
'url': 'https://github.com/django-json-api/django-rest-framework-json-api/blob/master/LICENSE',
936+
}
937+
},
938+
'servers': [
939+
{'url': 'https://localhost/v1', 'description': 'local docker'},
940+
{'url': 'http://localhost:8000/v1', 'description': 'local dev'},
941+
{'url': 'https://api.example.com/v1', 'description': 'demo server'},
942+
{'url': '{serverURL}', 'description': 'provide your server URL',
943+
'variables': {'serverURL': {'default': 'http://localhost:8000/v1'}}}
944+
]
945+
}
946+
947+
948+
class MyViewSet(ModelViewSet):
949+
schema = AutoSchema(openapi_schema=openapi_schema)
950+
```
951+
905952
To generate an OAS schema document, use something like:
906953

907954
```text
908955
$ django-admin generateschema --settings=example.settings >myschema.yaml
909956
```
910957

958+
959+
911960
You can then use any number of OAS tools such as
912961
[swagger-ui-watcher](https://www.npmjs.com/package/swagger-ui-watcher)
913962
to render the schema:

0 commit comments

Comments
 (0)