@@ -32,6 +32,7 @@ REST_FRAMEWORK = {
32
32
' rest_framework.renderers.BrowsableAPIRenderer'
33
33
),
34
34
' DEFAULT_METADATA_CLASS' : ' rest_framework_json_api.metadata.JSONAPIMetadata' ,
35
+ ' DEFAULT_SCHEMA_CLASS' : ' rest_framework_json_api.schemas.openapi.AutoSchema' ,
35
36
' DEFAULT_FILTER_BACKENDS' : (
36
37
' rest_framework_json_api.filters.QueryParameterValidationFilter' ,
37
38
' 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
882
883
DRF 3.10 added a new management command: ` generateschema ` which can generate an
883
884
[ OAS 3.0 schema] ( https://www.openapis.org/ ) as a YAML or JSON file.
884
885
886
+ ### Settings needed
887
+
885
888
In order to produce an OAS schema that properly represents the JSON: API structure,
886
889
DJA has this same command as an override of DRF's. In order to make sure the DJA
887
890
version of the command is used, you must add DJA ** ahead of** DRF in the
@@ -902,12 +905,58 @@ INSTALLED_APPS = [
902
905
]
903
906
```
904
907
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
+
905
952
To generate an OAS schema document, use something like:
906
953
907
954
``` text
908
955
$ django-admin generateschema --settings=example.settings >myschema.yaml
909
956
```
910
957
958
+
959
+
911
960
You can then use any number of OAS tools such as
912
961
[ swagger-ui-watcher] ( https://www.npmjs.com/package/swagger-ui-watcher )
913
962
to render the schema:
0 commit comments