Allow customization in determining if a serializer produces a component schema

This commit is contained in:
phillipdupuis 2021-01-28 22:09:38 -05:00
parent 19655edbf7
commit e97047954b
2 changed files with 17 additions and 3 deletions

View File

@ -375,6 +375,16 @@ operationIds.
In order to work around this, you can override `get_operation_id_base()` to In order to work around this, you can override `get_operation_id_base()` to
provide a different base for name part of the ID. provide a different base for name part of the ID.
#### `is_valid_serializer()`
Returns true if a serializer instance will produce a valid component schema
for describing request and response bodies. The default behavior is to return
'True' for any Serializer instance.
If you have a custom subclass of BaseSerializer which is capable of producing
a component schema, you can use that component schema by overrideing this method
(along with `map_serializer()`).
### `AutoSchema.__init__()` kwargs ### `AutoSchema.__init__()` kwargs
`AutoSchema` provides a number of `__init__()` kwargs that can be used for `AutoSchema` provides a number of `__init__()` kwargs that can be used for

View File

@ -194,7 +194,7 @@ class AutoSchema(ViewInspector):
serializer = self.get_serializer(path, method) serializer = self.get_serializer(path, method)
if not isinstance(serializer, serializers.Serializer): if not self.is_valid_serializer(serializer):
return {} return {}
component_name = self.get_component_name(serializer) component_name = self.get_component_name(serializer)
@ -626,7 +626,7 @@ class AutoSchema(ViewInspector):
serializer = self.get_serializer(path, method) serializer = self.get_serializer(path, method)
if not isinstance(serializer, serializers.Serializer): if not self.is_valid_serializer(serializer):
item_schema = {} item_schema = {}
else: else:
item_schema = self._get_reference(serializer) item_schema = self._get_reference(serializer)
@ -650,7 +650,7 @@ class AutoSchema(ViewInspector):
serializer = self.get_serializer(path, method) serializer = self.get_serializer(path, method)
if not isinstance(serializer, serializers.Serializer): if not self.is_valid_serializer(serializer):
item_schema = {} item_schema = {}
else: else:
item_schema = self._get_reference(serializer) item_schema = self._get_reference(serializer)
@ -692,6 +692,10 @@ class AutoSchema(ViewInspector):
return [path.split('/')[0].replace('_', '-')] return [path.split('/')[0].replace('_', '-')]
def is_valid_serializer(self, serializer):
# return True if the serializer produces a valid component schema
return isinstance(serializer, serializers.Serializer)
def _get_path_parameters(self, path, method): def _get_path_parameters(self, path, method):
warnings.warn( warnings.warn(
"Method `_get_path_parameters()` has been renamed to `get_path_parameters()`. " "Method `_get_path_parameters()` has been renamed to `get_path_parameters()`. "