|
1 |
| - |
2 | 1 | # Usage
|
3 | 2 |
|
4 | 3 | The DJA package implements a custom renderer, parser, exception handler, query filter backends, and
|
@@ -32,6 +31,7 @@ REST_FRAMEWORK = {
|
32 | 31 | 'rest_framework.renderers.BrowsableAPIRenderer'
|
33 | 32 | ),
|
34 | 33 | 'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata',
|
| 34 | + 'DEFAULT_SCHEMA_CLASS': 'rest_framework_json_api.schemas.openapi.AutoSchema', |
35 | 35 | 'DEFAULT_FILTER_BACKENDS': (
|
36 | 36 | 'rest_framework_json_api.filters.QueryParameterValidationFilter',
|
37 | 37 | 'rest_framework_json_api.filters.OrderingFilter',
|
@@ -944,3 +944,124 @@ The `prefetch_related` case will issue 4 queries, but they will be small and fas
|
944 | 944 | ### Relationships
|
945 | 945 | ### Errors
|
946 | 946 | -->
|
| 947 | + |
| 948 | +## Generating an OpenAPI Specification (OAS) 3.0 schema document |
| 949 | + |
| 950 | +DRF >= 3.12 has a [new OAS schema functionality](https://www.django-rest-framework.org/api-guide/schemas/) to generate an |
| 951 | +[OAS 3.0 schema](https://www.openapis.org/) as a YAML or JSON file. |
| 952 | + |
| 953 | +DJA extends DRF's schema support to generate an OAS schema in the JSON:API format. |
| 954 | + |
| 955 | +### AutoSchema Settings |
| 956 | + |
| 957 | +In order to produce an OAS schema that properly represents the JSON:API structure |
| 958 | +you have to either add a `schema` attribute to each view class or set the `REST_FRAMEWORK['DEFAULT_SCHEMA_CLASS']` |
| 959 | +to DJA's version of AutoSchema. |
| 960 | + |
| 961 | +#### View-based |
| 962 | + |
| 963 | +```python |
| 964 | +from rest_framework_json_api.schemas.openapi import AutoSchema |
| 965 | + |
| 966 | +class MyViewset(ModelViewSet): |
| 967 | + schema = AutoSchema |
| 968 | + ... |
| 969 | +``` |
| 970 | + |
| 971 | +#### Default schema class |
| 972 | + |
| 973 | +```python |
| 974 | +REST_FRAMEWORK = { |
| 975 | + # ... |
| 976 | + 'DEFAULT_SCHEMA_CLASS': 'rest_framework_json_api.schemas.openapi.AutoSchema', |
| 977 | +} |
| 978 | +``` |
| 979 | + |
| 980 | +### Adding additional OAS schema content |
| 981 | + |
| 982 | +You can extend the OAS schema document by subclassing |
| 983 | +[`SchemaGenerator`](https://www.django-rest-framework.org/api-guide/schemas/#schemagenerator) |
| 984 | +and extending `get_schema`. |
| 985 | + |
| 986 | + |
| 987 | +Here's an example that adds OAS `info` and `servers` objects. |
| 988 | + |
| 989 | +```python |
| 990 | +from rest_framework_json_api.schemas.openapi import SchemaGenerator as JSONAPISchemaGenerator |
| 991 | + |
| 992 | + |
| 993 | +class MySchemaGenerator(JSONAPISchemaGenerator): |
| 994 | + """ |
| 995 | + Describe my OAS schema info in detail (overriding what DRF put in) and list the servers where it can be found. |
| 996 | + """ |
| 997 | + def get_schema(self, request, public): |
| 998 | + schema = super().get_schema(request, public) |
| 999 | + schema['info'] = { |
| 1000 | + 'version': '1.0', |
| 1001 | + 'title': 'my demo API', |
| 1002 | + 'description': 'A demonstration of [OAS 3.0](https://www.openapis.org)', |
| 1003 | + 'contact': { |
| 1004 | + 'name': 'my name' |
| 1005 | + }, |
| 1006 | + 'license': { |
| 1007 | + 'name': 'BSD 2 clause', |
| 1008 | + 'url': 'https://github.com/django-json-api/django-rest-framework-json-api/blob/master/LICENSE', |
| 1009 | + } |
| 1010 | + } |
| 1011 | + schema['servers'] = [ |
| 1012 | + {'url': 'https://localhost/v1', 'description': 'local docker'}, |
| 1013 | + {'url': 'http://localhost:8000/v1', 'description': 'local dev'}, |
| 1014 | + {'url': 'https://api.example.com/v1', 'description': 'demo server'}, |
| 1015 | + {'url': '{serverURL}', 'description': 'provide your server URL', |
| 1016 | + 'variables': {'serverURL': {'default': 'http://localhost:8000/v1'}}} |
| 1017 | + ] |
| 1018 | + return schema |
| 1019 | +``` |
| 1020 | + |
| 1021 | +### Generate a Static Schema on Command Line |
| 1022 | + |
| 1023 | +See [DRF documentation for generateschema](https://www.django-rest-framework.org/api-guide/schemas/#generating-a-static-schema-with-the-generateschema-management-command) |
| 1024 | +To generate an OAS schema document, use something like: |
| 1025 | + |
| 1026 | +```text |
| 1027 | +$ django-admin generateschema --settings=example.settings \ |
| 1028 | + --generator_class myapp.views.MySchemaGenerator >myschema.yaml |
| 1029 | +``` |
| 1030 | + |
| 1031 | +You can then use any number of OAS tools such as |
| 1032 | +[swagger-ui-watcher](https://www.npmjs.com/package/swagger-ui-watcher) |
| 1033 | +to render the schema: |
| 1034 | +```text |
| 1035 | +$ swagger-ui-watcher myschema.yaml |
| 1036 | +``` |
| 1037 | + |
| 1038 | +Note: Swagger-ui-watcher will complain that "DELETE operations cannot have a requestBody" |
| 1039 | +but it will still work. This [error](https://github.com/OAI/OpenAPI-Specification/pull/2117) |
| 1040 | +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) |
| 1041 | +is published. |
| 1042 | + |
| 1043 | +([swagger-ui](https://www.npmjs.com/package/swagger-ui) will work silently.) |
| 1044 | + |
| 1045 | +### Generate a Dynamic Schema in a View |
| 1046 | + |
| 1047 | +See [DRF documentation for a Dynamic Schema](https://www.django-rest-framework.org/api-guide/schemas/#generating-a-dynamic-schema-with-schemaview). |
| 1048 | + |
| 1049 | +```python |
| 1050 | +from rest_framework.schemas import get_schema_view |
| 1051 | + |
| 1052 | +urlpatterns = [ |
| 1053 | + ... |
| 1054 | + path('openapi', get_schema_view( |
| 1055 | + title="Example API", |
| 1056 | + description="API for all things …", |
| 1057 | + version="1.0.0", |
| 1058 | + generator_class=MySchemaGenerator, |
| 1059 | + ), name='openapi-schema'), |
| 1060 | + path('swagger-ui/', TemplateView.as_view( |
| 1061 | + template_name='swagger-ui.html', |
| 1062 | + extra_context={'schema_url': 'openapi-schema'} |
| 1063 | + ), name='swagger-ui'), |
| 1064 | + ... |
| 1065 | +] |
| 1066 | +``` |
| 1067 | + |
0 commit comments