django-rest-framework/docs/api-guide/schemas.md

441 lines
15 KiB
Markdown
Raw Normal View History

2019-07-01 05:04:36 +03:00
---
source:
- schemas
2019-07-01 05:04:36 +03:00
---
# Schema
> 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.
Django REST Framework provides support for automatic generation of
[OpenAPI][openapi] schemas.
2020-06-16 14:33:48 +03:00
## Overview
Schema generation has several moving parts. It's worth having an overview:
* `SchemaGenerator` is a top-level class that is responsible for walking your
configured URL patterns, finding `APIView` subclasses, enquiring for their
schema representation, and compiling the final schema object.
* `AutoSchema` encapsulates all the details necessary for per-view schema
introspection. Is attached to each view via the `schema` attribute. You
subclass `AutoSchema` in order to customize your schema.
* The `generateschema` management command allows you to generate a static schema
offline.
* Alternatively, you can route `SchemaView` to dynamically generate and serve
your schema.
* `settings.DEFAULT_SCHEMA_CLASS` allows you to specify an `AutoSchema`
subclass to serve as your project's default.
The following sections explain more.
## Generating an OpenAPI Schema
### Install dependencies
pip install pyyaml uritemplate
* `pyyaml` is used to generate schema into YAML-based OpenAPI format.
* `uritemplate` is used internally to get parameters in path.
### Generating a static schema with the `generateschema` management command
If your schema is static, you can use the `generateschema` management command:
```bash
./manage.py generateschema --file openapi-schema.yml
```
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.
### Generating a dynamic schema with `SchemaView`
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.
To route a `SchemaView`, use the `get_schema_view()` helper.
In `urls.py`:
```python
from rest_framework.schemas import get_schema_view
urlpatterns = [
# ...
# 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",
description="API for all things …",
version="1.0.0"
), name='openapi-schema'),
# ...
]
```
#### `get_schema_view()`
The `get_schema_view()` helper takes the following keyword arguments:
* `title`: May be used to provide a descriptive title for the schema definition.
* `description`: Longer descriptive text.
* `version`: The version of the API.
* `url`: May be used to pass a canonical base URL for the schema.
schema_view = get_schema_view(
title='Server Monitoring API',
url='https://www.example.org/api/'
)
* `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.
schema_view = get_schema_view(
title='Server Monitoring API',
url='https://www.example.org/api/',
urlconf='myproject.urls'
)
* `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:
schema_url_patterns = [
path('api/', include('myproject.api.urls')),
]
schema_view = get_schema_view(
title='Server Monitoring API',
url='https://www.example.org/api/',
patterns=schema_url_patterns,
)
2022-10-17 11:54:48 +03:00
* `public`: May be used to specify if schema should bypass views permissions. Default to False
* `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.
2020-06-16 14:33:48 +03:00
## SchemaGenerator
2020-06-16 14:33:48 +03:00
**Schema-level customization**
2020-06-16 14:33:48 +03:00
```python
from rest_framework.schemas.openapi import SchemaGenerator
```
2020-06-16 14:33:48 +03:00
`SchemaGenerator` is a class that walks a list of routed URL patterns, requests
the schema for each view and collates the resulting OpenAPI schema.
2020-06-16 14:33:48 +03:00
Typically you won't need to instantiate `SchemaGenerator` yourself, but you can
do so like so:
generator = SchemaGenerator(title='Stock Prices API')
Arguments:
* `title` **required**: The name of the API.
* `description`: Longer descriptive text.
* `version`: The version of the API. Defaults to `0.1.0`.
* `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`.
2020-06-16 14:33:48 +03:00
In order to customize the top-level schema, subclass
`rest_framework.schemas.openapi.SchemaGenerator` and provide your subclass
as an argument to the `generateschema` command or `get_schema_view()` helper
function.
2022-06-06 15:31:00 +03:00
### get_schema(self, request=None, public=False)
Returns a dictionary that represents the OpenAPI schema:
generator = SchemaGenerator(title='Stock Prices API')
schema = generator.get_schema()
The `request` argument is optional, and may be used if you want to apply
per-user permissions to the resulting schema generation.
This is a good point to override if you want to customize the generated
2020-06-16 14:33:48 +03:00
dictionary For example you might wish to add terms of service to the [top-level
`info` object][info-object]:
2020-06-16 14:33:48 +03:00
```
class TOSSchemaGenerator(SchemaGenerator):
def get_schema(self, *args, **kwargs):
schema = super().get_schema(*args, **kwargs)
2020-06-16 14:33:48 +03:00
schema["info"]["termsOfService"] = "https://example.com/tos.html"
return schema
```
2020-06-16 14:33:48 +03:00
## AutoSchema
2020-06-16 14:33:48 +03:00
**Per-View Customization**
2020-06-16 14:33:48 +03:00
```python
from rest_framework.schemas.openapi import AutoSchema
```
2020-06-16 14:33:48 +03:00
By default, view introspection is performed by an `AutoSchema` instance
accessible via the `schema` attribute on `APIView`.
2020-06-16 14:33:48 +03:00
auto_schema = some_view.schema
2020-06-16 14:33:48 +03:00
`AutoSchema` provides the OpenAPI elements needed for each view, request method
and path:
2020-06-16 14:33:48 +03:00
* A list of [OpenAPI components][openapi-components]. In DRF terms these are
mappings of serializers that describe request and response bodies.
* The appropriate [OpenAPI operation object][openapi-operation] that describes
the endpoint, including path and query parameters for pagination, filtering,
and so on.
2020-06-16 14:33:48 +03:00
```python
components = auto_schema.get_components(...)
operation = auto_schema.get_operation(...)
```
2020-06-16 14:33:48 +03:00
In compiling the schema, `SchemaGenerator` calls `get_components()` and
`get_operation()` for each view, allowed method, and path.
2020-06-16 14:33:48 +03:00
----
2020-06-16 14:33:48 +03:00
**Note**: The automatic introspection of components, and many operation
parameters relies on the relevant attributes and methods of
`GenericAPIView`: `get_serializer()`, `pagination_class`, `filter_backends`,
etc. For basic `APIView` subclasses, default introspection is essentially limited to
the URL kwarg path parameters for this reason.
2020-06-16 14:33:48 +03:00
----
2020-06-16 14:33:48 +03:00
`AutoSchema` encapsulates the view introspection needed for schema generation.
Because of this all the schema generation logic is kept in a single place,
rather than being spread around the already extensive view, serializer and
field APIs.
2020-06-16 14:33:48 +03:00
Keeping with this pattern, try not to let schema logic leak into your own
views, serializers, or fields when customizing the schema generation. You might
be tempted to do something like this:
2020-06-16 14:33:48 +03:00
```python
class CustomSchema(AutoSchema):
"""
AutoSchema subclass using schema_extra_info on the view.
"""
...
2020-06-16 14:33:48 +03:00
class CustomView(APIView):
schema = CustomSchema()
schema_extra_info = ... some extra info ...
```
2020-06-16 14:33:48 +03:00
Here, the `AutoSchema` subclass goes looking for `schema_extra_info` on the
view. This is _OK_ (it doesn't actually hurt) but it means you'll end up with
your schema logic spread out in a number of different places.
2020-06-16 14:33:48 +03:00
Instead try to subclass `AutoSchema` such that the `extra_info` doesn't leak
out into the view:
2020-02-28 14:06:03 +03:00
2020-06-16 14:33:48 +03:00
```python
class BaseSchema(AutoSchema):
"""
AutoSchema subclass that knows how to use extra_info.
"""
...
2020-02-28 14:06:03 +03:00
2020-06-16 14:33:48 +03:00
class CustomSchema(BaseSchema):
extra_info = ... some extra info ...
2020-02-28 14:06:03 +03:00
2020-06-16 14:33:48 +03:00
class CustomView(APIView):
schema = CustomSchema()
```
2020-02-28 14:06:03 +03:00
2020-06-16 14:33:48 +03:00
This style is slightly more verbose but maintains the encapsulation of the
schema related code. It's more _cohesive_ in the _parlance_. It'll keep the
rest of your API code more tidy.
2020-02-28 14:06:03 +03:00
2020-06-16 14:33:48 +03:00
If an option applies to many view classes, rather than creating a specific
subclass per-view, you may find it more convenient to allow specifying the
option as an `__init__()` kwarg to your base `AutoSchema` subclass:
2020-02-28 14:06:03 +03:00
2020-06-16 14:33:48 +03:00
```python
class CustomSchema(BaseSchema):
def __init__(self, **kwargs):
# store extra_info for later
self.extra_info = kwargs.pop("extra_info")
super().__init__(**kwargs)
2020-02-28 14:06:03 +03:00
2020-06-16 14:33:48 +03:00
class CustomView(APIView):
schema = CustomSchema(
extra_info=... some extra info ...
)
```
2020-02-28 14:06:03 +03:00
2020-06-16 14:33:48 +03:00
This saves you having to create a custom subclass per-view for a commonly used option.
2020-02-28 14:06:03 +03:00
Not all `AutoSchema` methods expose related `__init__()` kwargs, but those for
2020-06-16 14:33:48 +03:00
the more commonly needed options do.
2020-02-28 14:06:03 +03:00
2020-06-16 14:33:48 +03:00
### `AutoSchema` methods
2020-02-28 14:06:03 +03:00
2020-06-16 14:33:48 +03:00
#### `get_components()`
2020-02-28 14:06:03 +03:00
2020-06-16 14:33:48 +03:00
Generates the OpenAPI components that describe request and response bodies,
deriving their properties from the serializer.
2020-02-28 14:06:03 +03:00
2020-06-16 14:33:48 +03:00
Returns a dictionary mapping the component name to the generated
representation. By default this has just a single pair but you may override
`get_components()` to return multiple pairs if your view uses multiple
serializers.
2020-02-28 14:06:03 +03:00
2020-06-16 14:33:48 +03:00
#### `get_component_name()`
2020-02-28 14:06:03 +03:00
2020-06-16 14:33:48 +03:00
Computes the component's name from the serializer.
2020-02-28 14:06:03 +03:00
2020-06-16 14:33:48 +03:00
You may see warnings if your API has duplicate component names. If so you can override `get_component_name()` or pass the `component_name` `__init__()` kwarg (see below) to provide different names.
2022-06-06 15:44:02 +03:00
#### `get_reference()`
Returns a reference to the serializer component. This may be useful if you override `get_schema()`.
2020-06-16 14:33:48 +03:00
#### `map_serializer()`
2020-06-16 14:33:48 +03:00
Maps serializers to their OpenAPI representations.
2020-06-16 14:33:48 +03:00
Most serializers should conform to the standard OpenAPI `object` type, but you may
wish to override `map_serializer()` in order to customize this or other
serializer-level fields.
2020-06-16 14:33:48 +03:00
#### `map_field()`
2020-06-16 14:33:48 +03:00
Maps individual serializer fields to their schema representation. The base implementation
will handle the default fields that Django REST Framework provides.
2020-06-16 14:33:48 +03:00
For `SerializerMethodField` instances, for which the schema is unknown, or custom field subclasses you should override `map_field()` to generate the correct schema:
```python
class CustomSchema(AutoSchema):
2020-06-16 14:33:48 +03:00
"""Extension of ``AutoSchema`` to add support for custom field schemas."""
2020-06-16 14:33:48 +03:00
def map_field(self, field):
# Handle SerializerMethodFields or custom fields here...
# ...
return super().map_field(field)
2020-03-02 21:35:27 +03:00
```
2020-06-16 14:33:48 +03:00
Authors of third-party packages should aim to provide an `AutoSchema` subclass,
and a mixin, overriding `map_field()` so that users can easily generate schemas
for their custom fields.
2020-03-02 21:35:27 +03:00
2020-06-16 14:33:48 +03:00
#### `get_tags()`
2020-04-24 17:17:35 +03:00
2020-06-16 14:33:48 +03:00
OpenAPI groups operations by tags. By default tags taken from the first path
segment of the routed URL. For example, a URL like `/users/{id}/` will generate
the tag `users`.
2020-03-02 21:35:27 +03:00
2020-06-16 14:33:48 +03:00
You can pass an `__init__()` kwarg to manually specify tags (see below), or
override `get_tags()` to provide custom logic.
2020-03-02 21:35:27 +03:00
2020-06-16 14:33:48 +03:00
#### `get_operation()`
2020-03-02 21:35:27 +03:00
2020-06-16 14:33:48 +03:00
Returns the [OpenAPI operation object][openapi-operation] that describes the
endpoint, including path and query parameters for pagination, filtering, and so
on.
2020-03-02 21:35:27 +03:00
2020-06-16 14:33:48 +03:00
Together with `get_components()`, this is the main entry point to the view
introspection.
2020-03-02 21:35:27 +03:00
2020-06-16 14:33:48 +03:00
#### `get_operation_id()`
2020-03-02 21:35:27 +03:00
2020-06-16 14:33:48 +03:00
There must be a unique [operationid](openapi-operationid) for each operation.
By default the `operationId` is deduced from the model name, serializer name or
view name. The operationId looks like "listItems", "retrieveItem",
"updateItem", etc. The `operationId` is camelCase by convention.
2020-04-24 17:17:35 +03:00
2020-06-16 14:33:48 +03:00
#### `get_operation_id_base()`
2020-03-02 21:35:27 +03:00
2020-06-16 14:33:48 +03:00
If you have several views with the same model name, you may see duplicate
operationIds.
2020-03-02 21:35:27 +03:00
2020-06-16 14:33:48 +03:00
In order to work around this, you can override `get_operation_id_base()` to
provide a different base for name part of the ID.
2020-03-02 21:35:27 +03:00
#### `get_serializer()`
If the view has implemented `get_serializer()`, returns the result.
#### `get_request_serializer()`
By default returns `get_serializer()` but can be overridden to
differentiate between request and response objects.
#### `get_response_serializer()`
By default returns `get_serializer()` but can be overridden to
differentiate between request and response objects.
2020-06-16 14:33:48 +03:00
### `AutoSchema.__init__()` kwargs
2020-03-02 21:35:27 +03:00
2020-06-16 14:33:48 +03:00
`AutoSchema` provides a number of `__init__()` kwargs that can be used for
common customizations, if the default generated values are not appropriate.
2020-03-02 21:35:27 +03:00
2020-06-16 14:33:48 +03:00
The available kwargs are:
2020-03-02 21:35:27 +03:00
2020-06-16 14:33:48 +03:00
* `tags`: Specify a list of tags.
* `component_name`: Specify the component name.
* `operation_id_base`: Specify the resource-name part of operation IDs.
2020-03-02 21:35:27 +03:00
2020-06-16 14:33:48 +03:00
You pass the kwargs when declaring the `AutoSchema` instance on your view:
2020-03-02 21:35:27 +03:00
2020-06-16 14:33:48 +03:00
```
class PetDetailView(generics.RetrieveUpdateDestroyAPIView):
schema = AutoSchema(
tags=['Pets'],
component_name='Pet',
operation_id_base='Pet',
)
...
```
2020-03-02 21:35:27 +03:00
2020-06-16 14:33:48 +03:00
Assuming a `Pet` model and `PetSerializer` serializer, the kwargs in this
example are probably not needed. Often, though, you'll need to pass the kwargs
if you have multiple view targeting the same model, or have multiple views with
identically named serializers.
2020-03-02 21:35:27 +03:00
2020-06-16 14:33:48 +03:00
If your views have related customizations that are needed frequently, you can
create a base `AutoSchema` subclass for your project that takes additional
`__init__()` kwargs to save subclassing `AutoSchema` for each view.
2020-02-28 14:06:03 +03:00
2021-04-26 11:30:41 +03:00
[cite]: https://blog.heroku.com/archives/2014/1/8/json_schema_for_heroku_platform_api
[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
[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
[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
2020-06-16 14:33:48 +03:00
[info-object]: https://swagger.io/specification/#infoObject