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
+64Lines changed: 64 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -288,8 +288,72 @@ class MyView(APIView):
288
288
...
289
289
```
290
290
291
+
=======
292
+
### Components
293
+
294
+
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.
295
+
296
+
Using OpenAPI's components provides the following advantages:
297
+
* The schema is more readable and lightweight.
298
+
* 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.
299
+
300
+
### Handling component's schema errors
301
+
302
+
You may get the following error while generating the schema:
303
+
```
304
+
"Serializer" is an invalid class name for schema generation.
305
+
Serializer's class name should be unique and explicit. e.g. "ItemSerializer".
306
+
```
307
+
308
+
This error occurs when the Serializer name is "Serializer". You should choose a component's name unique across your schema and different than "Serializer".
309
+
310
+
You may also get the following warning:
311
+
```
312
+
Schema component "ComponentName" has been overriden with a different value.
313
+
```
314
+
315
+
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.
316
+
317
+
You have two ways to solve the previous issues:
318
+
* You can rename your serializer with a unique name and another name than "Serializer".
319
+
* You can set the `component_name` kwarg parameter of the AutoSchema constructor (see below).
320
+
* You can override the `get_component_name` method of the AutoSchema class (see below).
321
+
322
+
#### Set a custom component's name for your view
323
+
324
+
To override the component's name in your view, you can use the `component_name` parameter of the AutoSchema constructor:
325
+
326
+
```python
327
+
from rest_framework.schemas.openapi import AutoSchema
328
+
329
+
classMyView(APIView):
330
+
schema = AutoSchema(component_name="Ulysses")
331
+
```
332
+
333
+
#### Override the default implementation
334
+
335
+
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.
336
+
337
+
```python
338
+
from rest_framework.schemas.openapi import AutoSchema
339
+
340
+
classCustomSchema(AutoSchema):
341
+
defget_components(self, path, method):
342
+
# Implement your custom implementation
343
+
344
+
defget_component_name(self, serializer):
345
+
# Implement your custom implementation
346
+
347
+
classCustomView(APIView):
348
+
"""APIView subclass with custom schema introspection."""
0 commit comments