Skip to content

Commit

Permalink
Fixed AttributeError raised by data property being silently ignored (#…
Browse files Browse the repository at this point in the history
…9455)

Signed-off-by: James Riley McHugh <[email protected]>
Co-authored-by: James Riley McHugh <[email protected]>
  • Loading branch information
james-mchugh and James Riley McHugh authored Jul 17, 2024
1 parent b6ea110 commit 8e304e1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
11 changes: 7 additions & 4 deletions rest_framework/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ def query_params(self):
@property
def data(self):
if not _hasattr(self, '_full_data'):
self._load_data_and_files()
with wrap_attributeerrors():
self._load_data_and_files()
return self._full_data

@property
Expand Down Expand Up @@ -420,13 +421,14 @@ def __getattr__(self, attr):
_request = self.__getattribute__("_request")
return getattr(_request, attr)
except AttributeError:
return self.__getattribute__(attr)
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{attr}'")

@property
def POST(self):
# Ensure that request.POST uses our request parsing.
if not _hasattr(self, '_data'):
self._load_data_and_files()
with wrap_attributeerrors():
self._load_data_and_files()
if is_form_media_type(self.content_type):
return self._data
return QueryDict('', encoding=self._request._encoding)
Expand All @@ -437,7 +439,8 @@ def FILES(self):
# Different from the other two cases, which are not valid property
# names on the WSGIRequest class.
if not _hasattr(self, '_files'):
self._load_data_and_files()
with wrap_attributeerrors():
self._load_data_and_files()
return self._files

def force_plaintext_errors(self, value):
Expand Down
19 changes: 19 additions & 0 deletions tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ def test_standard_behaviour_determines_non_form_content_PUT(self):
request.parsers = (PlainTextParser(), )
assert request.data == content

def test_calling_data_fails_when_attribute_error_is_raised(self):
"""
Ensure attribute errors raised when parsing are properly re-raised.
"""
expected_message = "Internal error"

class BrokenParser:
media_type = "application/json"

def parse(self, *args, **kwargs):
raise AttributeError(expected_message)

http_request = factory.post('/', data={}, format="json")
request = Request(http_request)
request.parsers = (BrokenParser,)

with self.assertRaisesMessage(WrappedAttributeError, expected_message):
request.data


class MockView(APIView):
authentication_classes = (SessionAuthentication,)
Expand Down

0 comments on commit 8e304e1

Please sign in to comment.