You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> A machine-readable [schema] describes what resources are available via the API, what their URLs are, how they are represented and what operations they support.
6
+
>
7
+
> — Heroku, [JSON Schema for the Heroku Platform API][cite]
8
+
9
+
API schemas are a useful tool that allow for a range of use cases, including
10
+
generating reference documentation, or driving dynamic client libraries that
11
+
can interact with your API.
12
+
13
+
Django REST Framework provides support for automatic generation of
14
+
[OpenAPI][openapi] schemas.
15
+
3
16
## Generating an OpenAPI Schema
4
17
5
-
### Static
18
+
### Install `pyyaml`
19
+
20
+
You'll need to install `pyyaml`, so that you can render your generated schema
21
+
into the commonly used YAML-based OpenAPI format.
22
+
23
+
pip install pyyaml
24
+
25
+
### Generating a static schema with the `generateschema` management command
6
26
7
27
If your schema is static, you can use the `generateschema` management command:
8
28
9
29
```bash
10
30
./manage.py generateschema > openapi-schema.yml
11
31
```
12
32
13
-
### Dynamic
33
+
Once you've generated a schema in this way you can annotate it with any
34
+
additional information that cannot be automatically inferred by the schema
35
+
generator.
36
+
37
+
You might want to check your API schema into version control and update it
38
+
with each new release, or serve the API schema from your site's static media.
39
+
40
+
### Generating a dynamic schema with `SchemaView`
14
41
15
-
If you require a dynamic schema, because foreign key choices depend on database values, for example, you can route a `SchemaView` that will generate and serve you schema on demand.
42
+
If you require a dynamic schema, because foreign key choices depend on database
43
+
values, for example, you can route a `SchemaView` that will generate and serve
44
+
your schema on demand.
45
+
46
+
To route a `SchemaView`, use the `get_schema_view()` helper.
16
47
17
48
In `urls.py`:
18
49
@@ -34,19 +65,151 @@ urlpatterns = [
34
65
35
66
#### `get_schema_view()`
36
67
37
-
* ...
68
+
The `get_schema_view()` helper takes the following keyword arguments:
69
+
70
+
*`title`: May be used to provide a descriptive title for the schema definition.
71
+
*`description`: Longer descriptive text.
72
+
*`url`: May be used to pass a canonical base URL for the schema.
73
+
74
+
schema_view = get_schema_view(
75
+
title='Server Monitoring API',
76
+
url='https://www.example.org/api/'
77
+
)
78
+
79
+
*`urlconf`: A string representing the import path to the URL conf that you want
80
+
to generate an API schema for. This defaults to the value of Django's
81
+
`ROOT_URLCONF` setting.
82
+
83
+
schema_view = get_schema_view(
84
+
title='Server Monitoring API',
85
+
url='https://www.example.org/api/',
86
+
urlconf='myproject.urls'
87
+
)
88
+
*`patterns`: List of url patterns to limit the schema introspection to. If you
89
+
only want the `myproject.api` urls to be exposed in the schema:
90
+
91
+
schema_url_patterns = [
92
+
url(r'^api/', include('myproject.api.urls')),
93
+
]
94
+
95
+
schema_view = get_schema_view(
96
+
title='Server Monitoring API',
97
+
url='https://www.example.org/api/',
98
+
patterns=schema_url_patterns,
99
+
)
100
+
101
+
*`generator_class`: May be used to specify a `SchemaGenerator` subclass to be
102
+
passed to the `SchemaView`.
103
+
*`authentication_classes`: May be used to specify the list of authentication
104
+
classes that will apply to the schema endpoint. Defaults to
105
+
`settings.DEFAULT_AUTHENTICATION_CLASSES`
106
+
*`permission_classes`: May be used to specify the list of permission classes
107
+
that will apply to the schema endpoint. Defaults to
108
+
`settings.DEFAULT_PERMISSION_CLASSES`.
109
+
*`renderer_classes`: May be used to pass the set of renderer classes that can
110
+
be used to render the API root endpoint.
38
111
39
-
* ...
40
112
41
-
* ...
113
+
## Customizing Schema Generation
42
114
115
+
You may customize schema generation at the level of the schema as a whole, or
116
+
on a per-view basis.
43
117
118
+
### Schema Level Customization
44
119
45
-
## Customizing Schema Generation
120
+
In order to customize the top-level schema sublass
121
+
`rest_framework.schemas.openapi.SchemaGenerator` and provide it as an argument
122
+
to the `generateschema` command or `get_schema_view()` helper function.
46
123
47
-
### Schema Level Customization
124
+
#### SchemaGenerator
125
+
126
+
A class that walks a list of routed URL patterns, requests the schema for each
127
+
view and collates the resulting OpenAPI schema.
128
+
129
+
Typically you'll instantiate `SchemaGenerator` with a `title` argument, like so:
0 commit comments