Fixed AttributeError raised by data property being silently ignored (#9455)

Signed-off-by: James Riley McHugh <mchugh_james@bah.com>
Co-authored-by: James Riley McHugh <mchugh_james@bah.com>
This commit is contained in:
James McHugh 2024-07-17 12:51:39 -04:00 committed by GitHub
parent b6ea11028f
commit 8e304e1adb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 4 deletions

View File

@ -217,6 +217,7 @@ class Request:
@property
def data(self):
if not _hasattr(self, '_full_data'):
with wrap_attributeerrors():
self._load_data_and_files()
return self._full_data
@ -420,12 +421,13 @@ class Request:
_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'):
with wrap_attributeerrors():
self._load_data_and_files()
if is_form_media_type(self.content_type):
return self._data
@ -437,6 +439,7 @@ class Request:
# Different from the other two cases, which are not valid property
# names on the WSGIRequest class.
if not _hasattr(self, '_files'):
with wrap_attributeerrors():
self._load_data_and_files()
return self._files

View File

@ -126,6 +126,25 @@ class TestContentParsing(TestCase):
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,)