@@ -1054,139 +1054,6 @@ The `prefetch_related` case will issue 4 queries, but they will be small and fas
10541054### Errors
10551055-->
10561056
1057- ## Generating an OpenAPI Specification (OAS) 3.0 schema document
1058-
1059- DRF has a [ OAS schema functionality] ( https://www.django-rest-framework.org/api-guide/schemas/ ) to generate an
1060- [ OAS 3.0 schema] ( https://www.openapis.org/ ) as a YAML or JSON file.
1061-
1062- DJA extends DRF's schema support to generate an OAS schema in the JSON: API format.
1063-
1064- ---
1065-
1066- ** Deprecation notice:**
1067-
1068- REST framework's built-in support for generating OpenAPI schemas is
1069- ** deprecated** in favor of 3rd party packages that can provide this
1070- functionality instead. Therefore we have also deprecated the schema support in
1071- Django REST framework JSON: API . The built-in support will be retired over the
1072- next releases.
1073-
1074- As a full-fledged replacement, we recommend the [ drf-spectacular-json-api] package.
1075-
1076- ---
1077-
1078- ### AutoSchema Settings
1079-
1080- In order to produce an OAS schema that properly represents the JSON: API structure
1081- you have to either add a ` schema ` attribute to each view class or set the ` REST_FRAMEWORK['DEFAULT_SCHEMA_CLASS'] `
1082- to DJA's version of AutoSchema.
1083-
1084- #### View-based
1085-
1086- ``` python
1087- from rest_framework_json_api.schemas.openapi import AutoSchema
1088-
1089- class MyViewset (ModelViewSet ):
1090- schema = AutoSchema
1091- ...
1092- ```
1093-
1094- #### Default schema class
1095-
1096- ``` python
1097- REST_FRAMEWORK = {
1098- # ...
1099- ' DEFAULT_SCHEMA_CLASS' : ' rest_framework_json_api.schemas.openapi.AutoSchema' ,
1100- }
1101- ```
1102-
1103- ### Adding additional OAS schema content
1104-
1105- You can extend the OAS schema document by subclassing
1106- [ ` SchemaGenerator ` ] ( https://www.django-rest-framework.org/api-guide/schemas/#schemagenerator )
1107- and extending ` get_schema ` .
1108-
1109-
1110- Here's an example that adds OAS ` info ` and ` servers ` objects.
1111-
1112- ``` python
1113- from rest_framework_json_api.schemas.openapi import SchemaGenerator as JSONAPISchemaGenerator
1114-
1115-
1116- class MySchemaGenerator (JSONAPISchemaGenerator ):
1117- """
1118- Describe my OAS schema info in detail (overriding what DRF put in) and list the servers where it can be found.
1119- """
1120- def get_schema (self , request , public ):
1121- schema = super ().get_schema(request, public)
1122- schema[' info' ] = {
1123- ' version' : ' 1.0' ,
1124- ' title' : ' my demo API' ,
1125- ' description' : ' A demonstration of [OAS 3.0](https://www.openapis.org)' ,
1126- ' contact' : {
1127- ' name' : ' my name'
1128- },
1129- ' license' : {
1130- ' name' : ' BSD 2 clause' ,
1131- ' url' : ' https://github.com/django-json-api/django-rest-framework-json-api/blob/main/LICENSE' ,
1132- }
1133- }
1134- schema[' servers' ] = [
1135- {' url' : ' http://localhost/v1' , ' description' : ' local docker' },
1136- {' url' : ' http://localhost:8000/v1' , ' description' : ' local dev' },
1137- {' url' : ' https://api.example.com/v1' , ' description' : ' demo server' },
1138- {' url' : ' {serverURL} ' , ' description' : ' provide your server URL' ,
1139- ' variables' : {' serverURL' : {' default' : ' http://localhost:8000/v1' }}}
1140- ]
1141- return schema
1142- ```
1143-
1144- ### Generate a Static Schema on Command Line
1145-
1146- See [ DRF documentation for generateschema] ( https://www.django-rest-framework.org/api-guide/schemas/#generating-a-static-schema-with-the-generateschema-management-command )
1147- To generate a static OAS schema document, using the ` generateschema ` management command, you ** must override DRF's default** ` generator_class ` with the DJA-specific version:
1148-
1149- ``` text
1150- $ ./manage.py generateschema --generator_class rest_framework_json_api.schemas.openapi.SchemaGenerator
1151- ```
1152-
1153- You can then use any number of OAS tools such as
1154- [ swagger-ui-watcher] ( https://www.npmjs.com/package/swagger-ui-watcher )
1155- to render the schema:
1156- ``` text
1157- $ swagger-ui-watcher myschema.yaml
1158- ```
1159-
1160- Note: Swagger-ui-watcher will complain that "DELETE operations cannot have a requestBody"
1161- but it will still work. This [ error] ( https://github.com/OAI/OpenAPI-Specification/pull/2117 )
1162- in the OAS specification will be fixed when [ OAS 3.1.0] ( https://www.openapis.org/blog/2020/06/18/openapi-3-1-0-rc0-its-here )
1163- is published.
1164-
1165- ([ swagger-ui] ( https://www.npmjs.com/package/swagger-ui ) will work silently.)
1166-
1167- ### Generate a Dynamic Schema in a View
1168-
1169- See [ DRF documentation for a Dynamic Schema] ( https://www.django-rest-framework.org/api-guide/schemas/#generating-a-dynamic-schema-with-schemaview ) .
1170-
1171- ``` python
1172- from rest_framework.schemas import get_schema_view
1173-
1174- urlpatterns = [
1175- ...
1176- path(' openapi' , get_schema_view(
1177- title = " Example API" ,
1178- description = " API for all things …" ,
1179- version = " 1.0.0" ,
1180- generator_class = MySchemaGenerator,
1181- ), name = ' openapi-schema' ),
1182- path(' swagger-ui/' , TemplateView.as_view(
1183- template_name = ' swagger-ui.html' ,
1184- extra_context = {' schema_url' : ' openapi-schema' }
1185- ), name = ' swagger-ui' ),
1186- ...
1187- ]
1188- ```
1189-
11901057## Third Party Packages
11911058
11921059### About Third Party Packages
0 commit comments