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
+63Lines changed: 63 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -316,6 +316,65 @@ class CustomSchema(AutoSchema):
316
316
defget_operation_id(self, path, method):
317
317
pass
318
318
319
+
classMyView(APIView):
320
+
schema = AutoSchema(component_name="Ulysses")
321
+
```
322
+
323
+
### Components
324
+
325
+
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.
326
+
327
+
Using OpenAPI's components provides the following advantages:
328
+
* The schema is more readable and lightweight.
329
+
* 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.
330
+
331
+
### Handling component's schema errors
332
+
333
+
You may get the following error while generating the schema:
334
+
```
335
+
"Serializer" is an invalid class name for schema generation.
336
+
Serializer's class name should be unique and explicit. e.g. "ItemSerializer".
337
+
```
338
+
339
+
This error occurs when the Serializer name is "Serializer". You should choose a component's name unique across your schema and different than "Serializer".
340
+
341
+
You may also get the following warning:
342
+
```
343
+
Schema component "ComponentName" has been overriden with a different value.
344
+
```
345
+
346
+
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.
347
+
348
+
You have two ways to solve the previous issues:
349
+
* You can rename your serializer with a unique name and another name than "Serializer".
350
+
* You can set the `component_name` kwarg parameter of the AutoSchema constructor (see below).
351
+
* You can override the `get_component_name` method of the AutoSchema class (see below).
352
+
353
+
#### Set a custom component's name for your view
354
+
355
+
To override the component's name in your view, you can use the `component_name` parameter of the AutoSchema constructor:
356
+
357
+
```python
358
+
from rest_framework.schemas.openapi import AutoSchema
359
+
360
+
classMyView(APIView):
361
+
schema = AutoSchema(component_name="Ulysses")
362
+
```
363
+
364
+
#### Override the default implementation
365
+
366
+
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.
367
+
368
+
```python
369
+
from rest_framework.schemas.openapi import AutoSchema
370
+
371
+
classCustomSchema(AutoSchema):
372
+
defget_components(self, path, method):
373
+
# Implement your custom implementation
374
+
375
+
defget_component_name(self, serializer):
376
+
# Implement your custom implementation
377
+
319
378
classCustomView(APIView):
320
379
"""APIView subclass with custom schema introspection."""
0 commit comments