8000 Added support to overwrite serializer methods in schema class by sliverc · Pull Request #1098 · django-json-api/django-rest-framework-json-api · GitHub
[go: up one dir, main page]

Skip to content

Added support to overwrite serializer methods in schema class #1098

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

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file < 8000 /span>
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 22 additions & 12 deletions rest_framework_json_api/schemas/openapi.py
674E
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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

Expand All @@ -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"],
Expand Down Expand Up @@ -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). "
Expand All @@ -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)

Expand All @@ -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)
Expand Down
0