|
1 |
| - |
| 1 | +` |
2 | 2 | # Usage
|
3 | 3 |
|
4 | 4 | The DJA package implements a custom renderer, parser, exception handler, query filter backends, and
|
@@ -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',
|
@@ -876,3 +877,97 @@ The `prefetch_related` case will issue 4 queries, but they will be small and fas
|
876 | 877 | ### Relationships
|
877 | 878 | ### Errors
|
878 | 879 | -->
|
| 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 | + |
0 commit comments