Skip to content

Added missing error message when no resource name is configured on serializer #1020

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 6 commits into from
Dec 5, 2021
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
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Jamie Bliss <[email protected]>
Jason Housley <[email protected]>
Jeppe Fihl-Pearson <[email protected]>
Jerel Unruh <[email protected]>
Jonas Kiefer <https://github.com/jokiefer>
Jonas Metzener <[email protected]>
Jonathan Senecal <[email protected]>
Joseba Mendivil <[email protected]>
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ any parts of the framework not mentioned in the documentation should generally b
* Raise comprehensible error when reserved field names `meta` and `results` are used.
* Use `relationships` in the error object `pointer` when the field is actually a relationship.
* Added missing inflection to the generated OpenAPI schema.
* Added missing error message when `resource_name` is not properly configured.

### Changed

Expand Down
5 changes: 4 additions & 1 deletion rest_framework_json_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,10 @@ def get_resource_type_from_serializer(serializer):
return meta.resource_name
elif hasattr(meta, "model"):
return get_resource_type_from_model(meta.model)
raise AttributeError()
raise AttributeError(
f"can not detect 'resource_name' on serializer '{serializer.__class__.__name__}'"
f" in module '{serializer.__class__.__module__}'"
)


def get_included_resources(request, serializer=None):
Expand Down
16 changes: 16 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from django.db import models
from rest_framework import status
from rest_framework.fields import Field
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
Expand All @@ -15,6 +16,7 @@
get_included_serializers,
get_related_resource_type,
get_resource_name,
get_resource_type_from_serializer,
undo_format_field_name,
undo_format_field_names,
undo_format_link_segment,
Expand Down Expand Up @@ -377,3 +379,17 @@ class Meta:
}

assert included_serializers == expected_included_serializers


def test_get_resource_type_from_serializer_without_resource_name_raises_error():
class SerializerWithoutResourceName(serializers.Serializer):
something = Field()

serializer = SerializerWithoutResourceName()

with pytest.raises(AttributeError) as excinfo:
get_resource_type_from_serializer(serializer=serializer)
assert str(excinfo.value) == (
"can not detect 'resource_name' on serializer "
"'SerializerWithoutResourceName' in module 'tests.test_utils'"
)