2019-07-01 05:04:36 +03:00
---
source:
2019-09-27 19:47:51 +03:00
- schemas
2019-07-01 05:04:36 +03:00
---
2016-07-04 18:38:17 +03:00
2019-07-08 15:09:05 +03:00
# Schema
2016-07-04 18:38:17 +03:00
> 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.
>
> — Heroku, [JSON Schema for the Heroku Platform API][cite]
API schemas are a useful tool that allow for a range of use cases, including
generating reference documentation, or driving dynamic client libraries that
can interact with your API.
2019-07-08 15:09:05 +03:00
Django REST Framework provides support for automatic generation of
[OpenAPI][openapi] schemas.
2017-09-14 11:46:34 +03:00
2019-07-08 15:09:05 +03:00
## Generating an OpenAPI Schema
2017-09-14 11:46:34 +03:00
2020-02-19 14:56:12 +03:00
### Install dependencies
2018-10-04 16:05:55 +03:00
2020-02-19 14:56:12 +03:00
pip install pyyaml uritemplate
2018-10-04 16:05:55 +03:00
2020-02-19 14:56:12 +03:00
* `pyyaml` is used to generate schema into YAML-based OpenAPI format.
* `uritemplate` is used internally to get parameters in path.
2018-10-04 16:05:55 +03:00
2019-07-08 15:09:05 +03:00
### Generating a static schema with the `generateschema` management command
2018-10-04 16:05:55 +03:00
2019-07-08 15:09:05 +03:00
If your schema is static, you can use the `generateschema` management command:
2018-10-04 16:05:55 +03:00
2019-07-08 15:09:05 +03:00
```bash
2020-02-12 22:35:54 +03:00
./manage.py generateschema --file openapi-schema.yml
2018-10-04 16:05:55 +03:00
```
Once you've generated a schema in this way you can annotate it with any
additional information that cannot be automatically inferred by the schema
generator.
You might want to check your API schema into version control and update it
with each new release, or serve the API schema from your site's static media.
2019-07-08 15:09:05 +03:00
### Generating a dynamic schema with `SchemaView`
2018-10-04 16:05:55 +03:00
2019-07-08 15:09:05 +03:00
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
your schema on demand.
2018-10-04 16:05:55 +03:00
2019-07-08 15:09:05 +03:00
To route a `SchemaView` , use the `get_schema_view()` helper.
In `urls.py` :
2018-10-04 16:05:55 +03:00
2019-07-08 15:09:05 +03:00
```python
2019-07-16 23:32:19 +03:00
from rest_framework.schemas import get_schema_view
2018-10-04 16:05:55 +03:00
urlpatterns = [
2019-07-08 15:09:05 +03:00
# ...
# Use the `get_schema_view()` helper to add a `SchemaView` to project URLs.
# * `title` and `description` parameters are passed to `SchemaGenerator` .
# * Provide view name for use with `reverse()` .
path('openapi', get_schema_view(
title="Your Project",
2019-09-03 17:07:30 +03:00
description="API for all things …",
version="1.0.0"
2019-07-08 15:09:05 +03:00
), name='openapi-schema'),
# ...
2018-10-04 16:05:55 +03:00
]
```
2019-07-08 15:09:05 +03:00
#### `get_schema_view()`
2016-07-04 18:38:17 +03:00
2019-07-08 15:09:05 +03:00
The `get_schema_view()` helper takes the following keyword arguments:
2017-09-14 11:46:34 +03:00
2019-07-08 15:09:05 +03:00
* `title` : May be used to provide a descriptive title for the schema definition.
* `description` : Longer descriptive text.
2019-11-06 23:54:12 +03:00
* `version` : The version of the API.
2019-07-08 15:09:05 +03:00
* `url` : May be used to pass a canonical base URL for the schema.
2016-07-04 18:38:17 +03:00
2019-07-08 15:09:05 +03:00
schema_view = get_schema_view(
title='Server Monitoring API',
url='https://www.example.org/api/'
)
2016-07-04 18:38:17 +03:00
2019-07-08 15:09:05 +03:00
* `urlconf` : A string representing the import path to the URL conf that you want
to generate an API schema for. This defaults to the value of Django's
`ROOT_URLCONF` setting.
2016-07-04 18:38:17 +03:00
2019-07-08 15:09:05 +03:00
schema_view = get_schema_view(
title='Server Monitoring API',
url='https://www.example.org/api/',
urlconf='myproject.urls'
)
2019-10-22 21:10:15 +03:00
2019-07-08 15:09:05 +03:00
* `patterns` : List of url patterns to limit the schema introspection to. If you
only want the `myproject.api` urls to be exposed in the schema:
2016-07-04 18:38:17 +03:00
2019-07-08 15:09:05 +03:00
schema_url_patterns = [
url(r'^api/', include('myproject.api.urls')),
]
schema_view = get_schema_view(
title='Server Monitoring API',
url='https://www.example.org/api/',
patterns=schema_url_patterns,
)
* `generator_class` : May be used to specify a `SchemaGenerator` subclass to be
passed to the `SchemaView` .
* `authentication_classes` : May be used to specify the list of authentication
classes that will apply to the schema endpoint. Defaults to
`settings.DEFAULT_AUTHENTICATION_CLASSES`
* `permission_classes` : May be used to specify the list of permission classes
that will apply to the schema endpoint. Defaults to
`settings.DEFAULT_PERMISSION_CLASSES` .
* `renderer_classes` : May be used to pass the set of renderer classes that can
be used to render the API root endpoint.
2017-09-14 11:46:34 +03:00
2019-07-08 15:09:05 +03:00
## Customizing Schema Generation
2017-09-14 11:46:34 +03:00
2019-07-08 15:09:05 +03:00
You may customize schema generation at the level of the schema as a whole, or
on a per-view basis.
2017-09-14 11:46:34 +03:00
2019-07-08 15:09:05 +03:00
### Schema Level Customization
2020-02-19 22:16:42 +03:00
In order to customize the top-level schema subclass
2019-07-08 15:09:05 +03:00
`rest_framework.schemas.openapi.SchemaGenerator` and provide it as an argument
to the `generateschema` command or `get_schema_view()` helper function.
#### SchemaGenerator
A class that walks a list of routed URL patterns, requests the schema for each
view and collates the resulting OpenAPI schema.
Typically you'll instantiate `SchemaGenerator` with a `title` argument, like so:
generator = SchemaGenerator(title='Stock Prices API')
Arguments:
2017-09-14 11:46:34 +03:00
2019-07-08 15:09:05 +03:00
* `title` **required** : The name of the API.
* `description` : Longer descriptive text.
2019-09-03 17:07:30 +03:00
* `version` : The version of the API. Defaults to `0.1.0` .
2019-07-08 15:09:05 +03:00
* `url` : The root URL of the API schema. This option is not required unless the schema is included under path prefix.
* `patterns` : A list of URLs to inspect when generating the schema. Defaults to the project's URL conf.
* `urlconf` : A URL conf module name to use when generating the schema. Defaults to `settings.ROOT_URLCONF` .
2017-09-14 11:46:34 +03:00
2019-07-08 15:09:05 +03:00
##### get_schema(self, request)
2017-09-14 11:46:34 +03:00
2019-07-08 15:09:05 +03:00
Returns a dictionary that represents the OpenAPI schema:
generator = SchemaGenerator(title='Stock Prices API')
2017-09-14 11:46:34 +03:00
schema = generator.get_schema()
2019-07-08 15:09:05 +03:00
The `request` argument is optional, and may be used if you want to apply
per-user permissions to the resulting schema generation.
2019-10-24 09:54:37 +03:00
This is a good point to override if you want to customize the generated
2019-07-08 15:09:05 +03:00
dictionary, for example to add custom
[specification extensions][openapi-specification-extensions].
### Per-View Customization
2017-09-14 11:46:34 +03:00
By default, view introspection is performed by an `AutoSchema` instance
accessible via the `schema` attribute on `APIView` . This provides the
2019-07-08 15:09:05 +03:00
appropriate [Open API operation object][openapi-operation] for the view,
request method and path:
2017-09-14 11:46:34 +03:00
auto_schema = view.schema
2019-07-08 15:09:05 +03:00
operation = auto_schema.get_operation(...)
2017-09-14 11:46:34 +03:00
2019-07-08 15:09:05 +03:00
In compiling the schema, `SchemaGenerator` calls `view.schema.get_operation()`
for each view, allowed method, and path.
2017-09-14 11:46:34 +03:00
2017-12-04 12:52:59 +03:00
---
**Note**: For basic `APIView` subclasses, default introspection is essentially
limited to the URL kwarg path parameters. For `GenericAPIView`
subclasses, which includes all the provided class based views, `AutoSchema` will
2019-10-24 21:31:12 +03:00
attempt to introspect serializer, pagination and filter fields, as well as
2017-12-04 12:52:59 +03:00
provide richer path field descriptions. (The key hooks here are the relevant
`GenericAPIView` attributes and methods: `get_serializer` , `pagination_class` ,
`filter_backends` and so on.)
---
2019-10-24 09:54:37 +03:00
In order to customize the operation generation, you should provide an `AutoSchema` subclass, overriding `get_operation()` as you need:
2017-09-14 11:46:34 +03:00
from rest_framework.views import APIView
2019-07-08 15:09:05 +03:00
from rest_framework.schemas.openapi import AutoSchema
2017-09-14 11:46:34 +03:00
class CustomSchema(AutoSchema):
2019-10-24 09:54:37 +03:00
def get_operation(...):
2017-10-06 14:18:31 +03:00
# Implement custom introspection here (or in other sub-methods)
2017-09-14 11:46:34 +03:00
class CustomView(APIView):
2019-07-08 15:09:05 +03:00
"""APIView subclass with custom schema introspection."""
2017-09-14 11:46:34 +03:00
schema = CustomSchema()
2019-07-08 15:09:05 +03:00
This provides complete control over view introspection.
2017-09-14 11:46:34 +03:00
2017-09-20 12:29:47 +03:00
You may disable schema generation for a view by setting `schema` to `None` :
2017-09-14 11:46:34 +03:00
class CustomView(APIView):
2016-07-04 18:38:17 +03:00
...
2019-07-08 15:09:05 +03:00
schema = None # Will not appear in schema
2016-07-04 18:38:17 +03:00
2019-07-08 15:09:05 +03:00
This also applies to extra actions for `ViewSet` s:
2017-10-02 12:16:33 +03:00
2019-07-08 15:09:05 +03:00
class CustomViewSet(viewsets.ModelViewSet):
2017-10-02 12:16:33 +03:00
2019-07-08 15:09:05 +03:00
@action (detail=True, schema=None)
def extra_action(self, request, pk=None):
...
2018-01-02 16:51:54 +03:00
2019-07-08 15:09:05 +03:00
If you wish to provide a base `AutoSchema` subclass to be used throughout your
project you may adjust `settings.DEFAULT_SCHEMA_CLASS` appropriately.
2018-01-02 16:51:54 +03:00
2020-02-28 14:06:03 +03:00
### Grouping Operations With Tags
Tags can be used to group logical operations. Each tag name in the list MUST be unique.
---
#### Django REST Framework generates tags automatically with the following logic:
Tag name will be first element from the path. Also, any `_` in path name will be replaced by a `-` .
Consider below examples.
Example 1: Consider a user management system. The following table will illustrate the tag generation logic.
Here first element from the paths is: `users` . Hence tag wil be `users`
Http Method | Path | Tags
-------------------------------------|-------------------|-------------
PUT, PATCH, GET(Retrieve), DELETE | /users/{id}/ | ['users']
POST, GET(List) | /users/ | ['users']
Example 2: Consider a restaurant management system. The System has restaurants. Each restaurant has branches.
Consider REST APIs to deal with a branch of a particular restaurant.
Here first element from the paths is: `restaurants` . Hence tag wil be `restaurants` .
Http Method | Path | Tags
-------------------------------------|----------------------------------------------------|-------------------
PUT, PATCH, GET(Retrieve), DELETE: | /restaurants/{restaurant_id}/branches/{branch_id} | ['restaurants']
POST, GET(List): | /restaurants/{restaurant_id}/branches/ | ['restaurants']
Example 3: Consider Order items for an e commerce company.
Http Method | Path | Tags
-------------------------------------|-------------------------|-------------
PUT, PATCH, GET(Retrieve), DELETE | /order_items/{id}/ | ['order-items']
POST, GET(List) | /order_items/ | ['order-items']
---
#### Overriding auto generated tags:
You can override auto-generated tags by passing `tags` argument to the constructor of `AutoSchema` . `tags` argument must be a list or tuple of string.
```python
from rest_framework.schemas.openapi import AutoSchema
from rest_framework.views import APIView
class MyView(APIView):
schema = AutoSchema(tags=['tag1', 'tag2'])
...
```
If you need more customization, you can override the `get_tags` method of `AutoSchema` class. Consider the following example:
```python
from rest_framework.schemas.openapi import AutoSchema
from rest_framework.views import APIView
class MySchema(AutoSchema):
...
def get_tags(self, path, method):
if method == 'POST':
tags = ['tag1', 'tag2']
elif method == 'GET':
tags = ['tag2', 'tag3']
elif path == '/example/path/':
tags = ['tag3', 'tag4']
else:
tags = ['tag5', 'tag6', 'tag7']
return tags
class MyView(APIView):
schema = MySchema()
...
```
2020-03-02 18:40:18 +03:00
### OperationId
2020-03-03 19:51:51 +03:00
The schema generator generates an [operationid ](openapi-operationid ) for each operation. This `operationId` is deduced from the model name, serializer name or view name. The operationId may looks like "listItems", "retrieveItem", "updateItem", etc..
The `operationId` is camelCase by convention.
2020-03-02 18:40:18 +03:00
If you have several views with the same model, the generator may generate duplicate operationId.
In order to work around this, you can override the second part of the operationId: operation name.
```python
from rest_framework.schemas.openapi import AutoSchema
class ExampleView(APIView):
"""APIView subclass with custom schema introspection."""
schema = AutoSchema(operation_id_base="Custom")
```
2020-03-03 19:51:51 +03:00
The previous example will generate the following operationId: "listCustoms", "retrieveCustom", "updateCustom", "partialUpdateCustom", "destroyCustom".
2020-03-02 18:40:18 +03:00
You need to provide the singular form of he operation name. For the list operation, a "s" will be appended at the end of the operation.
If you need more configuration over the `operationId` field, you can override the `get_operation_id_base` and `get_operation_id` methods from the `AutoSchema` class:
```python
class CustomSchema(AutoSchema):
def get_operation_id_base(self, path, method, action):
pass
def get_operation_id(self, path, method):
pass
2020-03-02 21:35:27 +03:00
class MyView(APIView):
schema = AutoSchema(component_name="Ulysses")
```
### Components
Since DRF 3.12, Schema uses the [OpenAPI Components ](openapi-components ). This method defines components in the schema and [references them ](openapi-reference ) inside request and response objects. By default, the component's name is deduced from the Serializer's name.
Using OpenAPI's components provides the following advantages:
* The schema is more readable and lightweight.
* If you use the schema to generate an SDK (using [openapi-generator ](openapi-generator ) or [swagger-codegen ](swagger-codegen )). The generator can name your SDK's models.
### Handling component's schema errors
You may get the following error while generating the schema:
```
"Serializer" is an invalid class name for schema generation.
Serializer's class name should be unique and explicit. e.g. "ItemSerializer".
```
This error occurs when the Serializer name is "Serializer". You should choose a component's name unique across your schema and different than "Serializer".
You may also get the following warning:
```
Schema component "ComponentName" has been overriden with a different value.
```
This warning occurs when different components have the same name in one schema. Your component name should be unique across your project. This is likely an error that may lead to an invalid schema.
You have two ways to solve the previous issues:
* You can rename your serializer with a unique name and another name than "Serializer".
* You can set the `component_name` kwarg parameter of the AutoSchema constructor (see below).
* You can override the `get_component_name` method of the AutoSchema class (see below).
#### Set a custom component's name for your view
To override the component's name in your view, you can use the `component_name` parameter of the AutoSchema constructor:
```python
from rest_framework.schemas.openapi import AutoSchema
class MyView(APIView):
schema = AutoSchema(component_name="Ulysses")
```
#### Override the default implementation
If you want to have more control and customization about how the schema's components are generated, you can override the `get_component_name` and `get_components` method from the AutoSchema class.
```python
from rest_framework.schemas.openapi import AutoSchema
class CustomSchema(AutoSchema):
def get_components(self, path, method):
# Implement your custom implementation
def get_component_name(self, serializer):
# Implement your custom implementation
2020-03-02 18:40:18 +03:00
class CustomView(APIView):
"""APIView subclass with custom schema introspection."""
schema = CustomSchema()
```
2020-02-28 14:06:03 +03:00
2019-07-08 15:09:05 +03:00
[openapi]: https://github.com/OAI/OpenAPI-Specification
[openapi-specification-extensions]: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#specification-extensions
2019-10-24 09:54:37 +03:00
[openapi-operation]: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#operationObject
2020-02-28 14:06:03 +03:00
[openapi-tags]: https://swagger.io/specification/#tagObject
2020-03-02 18:40:18 +03:00
[openapi-operationid]: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#fixed-fields-17
2020-03-02 21:35:27 +03:00
[openapi-components]: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#componentsObject
[openapi-reference]: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#referenceObject
[openapi-generator]: https://github.com/OpenAPITools/openapi-generator
[swagger-codegen]: https://github.com/swagger-api/swagger-codegen