Fixed #33458 -- Fixed encoding of messages with empty string as extra_tags.

This commit is contained in:
Timothy McCurrach 2022-01-22 02:06:17 +00:00 committed by Mariusz Felisiak
parent 7c4f396509
commit efb4478e48
2 changed files with 26 additions and 9 deletions

View File

@ -19,7 +19,7 @@ class MessageEncoder(json.JSONEncoder):
# Using 0/1 here instead of False/True to produce more compact json # Using 0/1 here instead of False/True to produce more compact json
is_safedata = 1 if isinstance(obj.message, SafeData) else 0 is_safedata = 1 if isinstance(obj.message, SafeData) else 0
message = [self.message_key, is_safedata, obj.level, obj.message] message = [self.message_key, is_safedata, obj.level, obj.message]
if obj.extra_tags: if obj.extra_tags is not None:
message.append(obj.extra_tags) message.append(obj.extra_tags)
return message return message
return super().default(obj) return super().default(obj)

View File

@ -52,6 +52,12 @@ class CookieTests(BaseTests, SimpleTestCase):
def stored_messages_count(self, storage, response): def stored_messages_count(self, storage, response):
return stored_cookie_messages_count(storage, response) return stored_cookie_messages_count(storage, response)
def encode_decode(self, *args, **kwargs):
storage = self.get_storage()
message = Message(constants.DEBUG, *args, **kwargs)
encoded = storage._encode(message)
return storage._decode(encoded)
def test_get(self): def test_get(self):
storage = self.storage_class(self.get_request()) storage = self.storage_class(self.get_request())
# Set initial data. # Set initial data.
@ -168,12 +174,23 @@ class CookieTests(BaseTests, SimpleTestCase):
A message containing SafeData is keeping its safe status when A message containing SafeData is keeping its safe status when
retrieved from the message storage. retrieved from the message storage.
""" """
def encode_decode(data): self.assertIsInstance(
message = Message(constants.DEBUG, data) self.encode_decode(mark_safe('<b>Hello Django!</b>')).message,
encoded = storage._encode(message) SafeData,
decoded = storage._decode(encoded) )
return decoded.message self.assertNotIsInstance(
self.encode_decode('<b>Hello Django!</b>').message,
SafeData,
)
storage = self.get_storage() def test_extra_tags(self):
self.assertIsInstance(encode_decode(mark_safe("<b>Hello Django!</b>")), SafeData) """
self.assertNotIsInstance(encode_decode("<b>Hello Django!</b>"), SafeData) A message's extra_tags attribute is correctly preserved when retrieved
from the message storage.
"""
for extra_tags in ['', None, 'some tags']:
with self.subTest(extra_tags=extra_tags):
self.assertEqual(
self.encode_decode('message', extra_tags=extra_tags).extra_tags,
extra_tags,
)