Skip to content

OpenAPI: Allow separate serializers for request/response/components #7425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions rest_framework/schemas/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def get_components(self, path, method):
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 {}
Expand Down Expand Up @@ -615,6 +615,18 @@ def get_serializer(self, path, method):
.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))}

Expand All @@ -624,7 +636,7 @@ def get_request_body(self, path, method):

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 = {}
Expand All @@ -648,7 +660,7 @@ def get_responses(self, path, method):

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 = {}
Expand Down