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
Copy file name to clipboardExpand all lines: docs/api-guide/schemas.md
+65Lines changed: 65 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -215,6 +215,7 @@ This also applies to extra actions for `ViewSet`s:
215
215
If you wish to provide a base `AutoSchema` subclass to be used throughout your
216
216
project you may adjust `settings.DEFAULT_SCHEMA_CLASS` appropriately.
217
217
218
+
<<<<<<< HEAD
218
219
219
220
### Grouping Operations With Tags
220
221
@@ -288,8 +289,72 @@ class MyView(APIView):
288
289
...
289
290
```
290
291
292
+
=======
293
+
### Components
294
+
295
+
Since DRF 3.12, Schema uses the [OpenAPI Components](openapi-components). This method define components in the schema and [referenced them](openapi-reference) inside request and response objects. The component's name is deduced from the Serializer's name.
296
+
297
+
Using OpenAPI's components provides the following advantages:
298
+
* The schema is more readable and lightweight.
299
+
* If you use the schema to generate a SDK (using [openapi-generator](openapi-generator) or [swagger-codegen](swagger-codegen)). The generator can name your SDK's models.
300
+
301
+
### Handling component's schema errors
302
+
303
+
You may get the following error while generating the schema:
304
+
```
305
+
"Serializer" is an invalid class name for schema generation.
306
+
Serializer's class name should be unique and explicit. e.g. "ItemSerializer".
307
+
```
308
+
309
+
This error occurs when the Serializer name is "Serializer". You should choose a component's name unique across your schema and different than "Serializer".
310
+
311
+
You may also get the following warning:
312
+
```
313
+
Schema component "ComponentName" has been overriden with a different value.
314
+
```
315
+
316
+
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.
317
+
318
+
You have two ways to solve the previous issues:
319
+
* You can rename your serializer with a unique name and another name than "Serializer".
320
+
* You can set the `component_name` kwarg parameter of the AutoSchema constructor (see below).
321
+
* You can override the `get_component_name` method of the AutoSchema class (see below).
322
+
323
+
#### Set a custom component's name for your view
324
+
325
+
To override the component's name in your view, you can use the `component_name` parameter of the AutoSchema constructor:
326
+
327
+
```python
328
+
from rest_framework.schemas.openapi import AutoSchema
329
+
330
+
classMyView(APIView):
331
+
schema = AutoSchema(component_name="Ulysses")
332
+
```
333
+
334
+
#### Override the default implementation
335
+
336
+
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.
337
+
338
+
```python
339
+
from rest_framework.schemas.openapi import AutoSchema
340
+
341
+
classCustomSchema(AutoSchema):
342
+
defget_components(self, path, method):
343
+
# Implement your custom implementation
344
+
345
+
defget_component_name(self, serializer):
346
+
# Implement your custom implementation
347
+
348
+
classCustomView(APIView):
349
+
"""APIView subclass with custom schema introspection."""
0 commit comments