Skip to content

Commit 4e18292

Browse files
authored
Added support to overwrite serializer methods in schema class (#1098)
1 parent 5e6872b commit 4e18292

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ any parts of the framework not mentioned in the documentation should generally b
1414

1515
* Added support for Python 3.11.
1616

17+
### Changed
18+
19+
* Added support to overwrite serializer methods in customized schema class
20+
1721
## [6.0.0] - 2022-09-24
1822

1923
### Fixed

rest_framework_json_api/schemas/openapi.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,9 @@ def get_operation(self, path, method):
427427
# get request and response code schemas
428428
if method == "GET":
429429
if is_list_view(path, method, self.view):
430-
self._add_get_collection_response(operation)
430+
self._add_get_collection_response(operation, path)
431431
else:
432-
self._add_get_item_response(operation)
432+
self._add_get_item_response(operation, path)
433433
elif method == "POST":
434434
self._add_post_item_response(operation, path)
435435
elif method == "PATCH":
@@ -487,25 +487,29 @@ def _get_sort_parameters(self, path, method):
487487
"""
488488
return [{"$ref": "#/components/parameters/sort"}]
489489

490-
def _add_get_collection_response(self, operation):
490+
def _add_get_collection_response(self, operation, path):
491491
"""
492492
Add GET 200 response for a collection to operation
493493
"""
494494
operation["responses"] = {
495-
"200": self._get_toplevel_200_response(operation, collection=True)
495+
"200": self._get_toplevel_200_response(
496+
operation, path, "GET", collection=True
497+
)
496498
}
497499
self._add_get_4xx_responses(operation)
498500

499-
def _add_get_item_response(self, operation):
501+
def _add_get_item_response(self, operation, path):
500502
"""
501503
add GET 200 response for an item to operation
502504
"""
503505
operation["responses"] = {
504-
"200": self._get_toplevel_200_response(operation, collection=False)
506+
"200": self._get_toplevel_200_response(
507+
operation, path, "GET", collection=False
508+
)
505509
}
506510
self._add_get_4xx_responses(operation)
507511

508-
def _get_toplevel_200_response(self, operation, collection=True):
512+
def _get_toplevel_200_response(self, operation, path, method, collection=True):
509513
"""
510514
return top-level JSON:API GET 200 response
511515
@@ -516,10 +520,12 @@ def _get_toplevel_200_response(self, operation, collection=True):
516520
if collection:
517521
data = {
518522
"type": "array",
519-
"items": get_reference(self, self.view.get_serializer()),
523+
"items": get_reference(
524+
self, self.get_response_serializer(path, method)
525+
),
520526
}
521527
else:
522-
data = get_reference(self, self.view.get_serializer())
528+
data = get_reference(self, self.get_response_serializer(path, method))
523529

524530
return {
525531
"description": operation["operationId"],
@@ -555,7 +561,9 @@ def _add_post_item_response(self, operation, path):
555561
"""
556562
operation["requestBody"] = self.get_request_body(path, "POST")
557563
operation["responses"] = {
558-
"201": self._get_toplevel_200_response(operation, collection=False)
564+
"201": self._get_toplevel_200_response(
565+
operation, path, "POST", collection=False
566+
)
559567
}
560568
operation["responses"]["201"]["description"] = (
561569
"[Created](https://jsonapi.org/format/#crud-creating-responses-201). "
@@ -574,7 +582,9 @@ def _add_patch_item_response(self, operation, path):
574582
"""
575583
operation["requestBody"] = self.get_request_body(path, "PATCH")
576584
operation["responses"] = {
577-
"200": self._get_toplevel_200_response(operation, collection=False)
585+
"200": self._get_toplevel_200_response(
586+
operation, path, "PATCH", collection=False
587+
)
578588
}
579589
self._add_patch_4xx_responses(operation)
580590

@@ -591,7 +601,7 @@ def get_request_body(self, path, method):
591601
"""
592602
A request body is required by JSON:API for POST, PATCH, and DELETE methods.
593603
"""
594-
serializer = self.get_serializer(path, method)
604+
serializer = self.get_request_serializer(path, method)
595605
if not isinstance(serializer, (serializers.BaseSerializer,)):
596606
return {}
597607
is_relationship = isinstance(self.view, views.RelationshipView)

0 commit comments

Comments
 (0)