|
1 | | - |
| 1 | +` |
2 | 2 | # Usage |
3 | 3 |
|
4 | 4 | The DJA package implements a custom renderer, parser, exception handler, query filter backends, and |
@@ -876,3 +876,48 @@ The `prefetch_related` case will issue 4 queries, but they will be small and fas |
876 | 876 | ### Relationships |
877 | 877 | ### Errors |
878 | 878 | --> |
| 879 | + |
| 880 | +## Generating an OpenAPI Specification (OAS) 3.0 schema document |
| 881 | + |
| 882 | +DRF 3.10 added a new management command: `generateschema` which can generate an |
| 883 | +[OAS 3.0 schema](https://www.openapis.org/) as a YAML or JSON file. |
| 884 | + |
| 885 | +In order to produce an OAS schema that properly represents the JSON:API structure, |
| 886 | +DJA has this same command as an override of DRF's. In order to make sure the DJA |
| 887 | +version of the command is used, you must add DJA **ahead of** DRF in the |
| 888 | +`INSTALLED_APPS` settings as in this example: |
| 889 | +```python |
| 890 | +INSTALLED_APPS = [ |
| 891 | + 'django.contrib.contenttypes', |
| 892 | + 'django.contrib.staticfiles', |
| 893 | + 'django.contrib.sites', |
| 894 | + 'django.contrib.sessions', |
| 895 | + 'django.contrib.auth', |
| 896 | + 'rest_framework_json_api', |
| 897 | + 'rest_framework', |
| 898 | + 'polymorphic', |
| 899 | + 'example', |
| 900 | + 'debug_toolbar', |
| 901 | + 'django_filters', |
| 902 | +] |
| 903 | +``` |
| 904 | + |
| 905 | +To generate an OAS schema document, use something like: |
| 906 | + |
| 907 | +```text |
| 908 | +$ django-admin generateschema --settings=example.settings >myschema.yaml |
| 909 | +``` |
| 910 | + |
| 911 | +You can then use any number of OAS tools such as |
| 912 | +[swagger-ui-watcher](https://www.npmjs.com/package/swagger-ui-watcher) |
| 913 | +to render the schema: |
| 914 | +```text |
| 915 | +$ swagger-ui-watcher myschema.yaml |
| 916 | +``` |
| 917 | + |
| 918 | +Note: Swagger-ui-watcher will complain that "DELETE operations cannot have a requestBody" |
| 919 | +but it will still work. This |
| 920 | +[error](https://github.com/OAI/OpenAPI-Specification/pull/1937) |
| 921 | +in the OAS specification is expected to be fixed soon. |
| 922 | +([swagger-ui](https://www.npmjs.com/package/swagger-ui) will work silently.) |
| 923 | + |
0 commit comments