diff --git a/CHANGELOG.md b/CHANGELOG.md index ef228d1d..5919fae3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,10 @@ any parts of the framework not mentioned in the documentation should generally b * Added support for Python 3.11. +### Changed + +* Added support to overwrite serializer methods in customized schema class + ## [6.0.0] - 2022-09-24 ### Fixed diff --git a/rest_framework_json_api/schemas/openapi.py b/rest_framework_json_api/schemas/openapi.py index 59ed3a3e..99fc2462 100644 --- a/rest_framework_json_api/schemas/openapi.py +++ b/rest_framework_json_api/schemas/openapi.py @@ -427,9 +427,9 @@ def get_operation(self, path, method): # get request and response code schemas if method == "GET": if is_list_view(path, method, self.view): - self._add_get_collection_response(operation) + self._add_get_collection_response(operation, path) else: - self._add_get_item_response(operation) + self._add_get_item_response(operation, path) elif method == "POST": self._add_post_item_response(operation, path) elif method == "PATCH": @@ -487,25 +487,29 @@ def _get_sort_parameters(self, path, method): """ return [{"$ref": "#/components/parameters/sort"}] - def _add_get_collection_response(self, operation): + def _add_get_collection_response(self, operation, path): """ Add GET 200 response for a collection to operation """ operation["responses"] = { - "200": self._get_toplevel_200_response(operation, collection=True) + "200": self._get_toplevel_200_response( + operation, path, "GET", collection=True + ) } self._add_get_4xx_responses(operation) - def _add_get_item_response(self, operation): + def _add_get_item_response(self, operation, path): """ add GET 200 response for an item to operation """ operation["responses"] = { - "200": self._get_toplevel_200_response(operation, collection=False) + "200": self._get_toplevel_200_response( + operation, path, "GET", collection=False + ) } self._add_get_4xx_responses(operation) - def _get_toplevel_200_response(self, operation, collection=True): + def _get_toplevel_200_response(self, operation, path, method, collection=True): """ return top-level JSON:API GET 200 response @@ -516,10 +520,12 @@ def _get_toplevel_200_response(self, operation, collection=True): if collection: data = { "type": "array", - "items": get_reference(self, self.view.get_serializer()), + "items": get_reference( + self, self.get_response_serializer(path, method) + ), } else: - data = get_reference(self, self.view.get_serializer()) + data = get_reference(self, self.get_response_serializer(path, method)) return { "description": operation["operationId"], @@ -555,7 +561,9 @@ def _add_post_item_response(self, operation, path): """ operation["requestBody"] = self.get_request_body(path, "POST") operation["responses"] = { - "201": self._get_toplevel_200_response(operation, collection=False) + "201": self._get_toplevel_200_response( + operation, path, "POST", collection=False + ) } operation["responses"]["201"]["description"] = ( "[Created](https://jsonapi.org/format/#crud-creating-responses-201). " @@ -574,7 +582,9 @@ def _add_patch_item_response(self, operation, path): """ operation["requestBody"] = self.get_request_body(path, "PATCH") operation["responses"] = { - "200": self._get_toplevel_200_response(operation, collection=False) + "200": self._get_toplevel_200_response( + operation, path, "PATCH", collection=False + ) } self._add_patch_4xx_responses(operation) @@ -591,7 +601,7 @@ def get_request_body(self, path, method): """ A request body is required by JSON:API for POST, PATCH, and DELETE methods. """ - serializer = self.get_serializer(path, method) + serializer = self.get_request_serializer(path, method) if not isinstance(serializer, (serializers.BaseSerializer,)): return {} is_relationship = isinstance(self.view, views.RelationshipView)