Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixes all suggestions from comment #1020 (review)
  • Loading branch information
jokiefer committed Dec 3, 2021
commit e07834783b7e3a62ccbaf80922bf33f90e0aacf5
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ Tim Selman <timcbaoth@gmail.com>
Tom Glowka <glowka.tom@gmail.com>
Ulrich Schuster <ulrich.schuster@mailworks.org>
Yaniv Peer <yanivpeer@gmail.com>
Jonas Kiefer <https://github.com/jokiefer>
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 error message if AttributeError is raised by `get_resource_type_from_serializer()`

### Changed

Expand Down
5 changes: 2 additions & 3 deletions rest_framework_json_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,9 @@ def get_resource_type_from_serializer(serializer):
return meta.resource_name
elif hasattr(meta, "model"):
return get_resource_type_from_model(meta.model)
# inspect.currentframe().f_code.co_firstlineno
raise AttributeError(
f"can not detect 'resource_name' on serializer '{serializer.__class__.__name__}'"
f" in module '{serializer.__class__.__module__}:{inspect.getsourcelines(serializer.__class__)[-1]}'")
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
12 changes: 5 additions & 7 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
import inspect
from django.db import models
from rest_framework import status
from rest_framework.fields import Field
Expand Down Expand Up @@ -381,15 +380,14 @@ class Meta:

assert included_serializers == expected_included_serializers


def test_get_resource_type_from_serializer_error_message():

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

serializer = SerializerWithoutResourceName()
try:

with pytest.raises(AttributeError) as excinfo:
get_resource_type_from_serializer(serializer=serializer)
raise AssertionError('no AttributeError was raised')
except AttributeError as ex:
assert str(ex) == f"can not detect 'resource_name' on serializer 'SerializerWithoutResourceName' in module 'tests.test_utils:{inspect.getsourcelines(serializer.__class__)[-1]}'"
assert str(excinfo.value) == "can not detect 'resource_name' on serializer 'SerializerWithoutResourceName' in module 'tests.test_utils'"