From b420c930df99bb8922ede4c1e5034bc72429a852 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Thu, 23 Jul 2020 14:31:25 +0300 Subject: [PATCH] OpenAPI: Allow separate serializers for request/response/components --- rest_framework/schemas/openapi.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index 9774a94c7..ea6995692 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -192,7 +192,7 @@ class AutoSchema(ViewInspector): if method.lower() == 'delete': return {} - serializer = self.get_serializer(path, method) + serializer = self.get_components_serializer(path, method) if not isinstance(serializer, serializers.Serializer): return {} @@ -615,6 +615,18 @@ class AutoSchema(ViewInspector): .format(view.__class__.__name__, method, path)) return None + def get_components_serializer(self, path, method): + # Override this function in a subclass if necessary. + return self.get_serializer(path, method) + + def get_body_serializer(self, path, method): + # Override this function in a subclass if necessary. + return self.get_serializer(path, method) + + def get_response_serializer(self, path, method): + # Override this function in a subclass if necessary. + return self.get_serializer(path, method) + def _get_reference(self, serializer): return {'$ref': '#/components/schemas/{}'.format(self.get_component_name(serializer))} @@ -624,7 +636,7 @@ class AutoSchema(ViewInspector): self.request_media_types = self.map_parsers(path, method) - serializer = self.get_serializer(path, method) + serializer = self.get_body_serializer(path, method) if not isinstance(serializer, serializers.Serializer): item_schema = {} @@ -648,7 +660,7 @@ class AutoSchema(ViewInspector): self.response_media_types = self.map_renderers(path, method) - serializer = self.get_serializer(path, method) + serializer = self.get_response_serializer(path, method) if not isinstance(serializer, serializers.Serializer): item_schema = {}