Skip to content
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

Fix the format_errors bug when response data is None #1229

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions example/tests/unit/test_default_drf_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ def test_blog_create(client):
assert resp.json() == expected


@pytest.mark.django_db
def test_rest_action_request(client):
url = reverse("drf-entry-blog-list") + "/actions/custom_response"
name = "Dummy Name"
request_data = {
"data": {"attributes": {"name": name}, "type": "blogs"},
}

resp = client.post(url, request_data)

assert resp.status_code == 400


@pytest.mark.django_db
def test_get_object_gives_correct_blog(client, blog, entry):
url = reverse("drf-entry-blog-detail", kwargs={"entry_pk": entry.id})
Expand Down
18 changes: 15 additions & 3 deletions example/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import logging
import rest_framework.exceptions as exceptions
import rest_framework.parsers
import rest_framework.renderers
from django_filters import rest_framework as filters
from rest_framework.filters import SearchFilter

from rest_framework.response import Response
from rest_framework.decorators import action
import rest_framework_json_api.metadata
import rest_framework_json_api.parsers
import rest_framework_json_api.renderers
Expand Down Expand Up @@ -74,6 +76,18 @@ def get_object(self):

return super().get_object()

@action(url_path="actions/custom_response", detail=False, methods=["post"])
def custom_response(self, request):
data = request.data
serializer = self.get_serializer(data=data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
try:
test = 1/0
except Exception:
return Response(status=400)

return Response(status=201)

class JsonApiViewSet(ModelViewSet):
"""
Expand Down Expand Up @@ -257,7 +271,6 @@ def get_queryset(self, *args, **kwargs):

return queryset


class CompanyViewset(ModelViewSet):
queryset = Company.objects.all()
serializer_class = CompanySerializer
Expand All @@ -276,7 +289,6 @@ class ProjectTypeViewset(ModelViewSet):
class EntryRelationshipView(RelationshipView):
queryset = Entry.objects.all()


class BlogRelationshipView(RelationshipView):
queryset = Blog.objects.all()

Expand Down
2 changes: 1 addition & 1 deletion rest_framework_json_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,6 @@ def format_error_object(message, pointer, response):


def format_errors(data):
if len(data) > 1 and isinstance(data, list):
if isinstance(data, list) and len(data) > 1:
data.sort(key=lambda x: x.get("source", {}).get("pointer", ""))
return {"errors": data}
Loading