mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-01-25 16:38:09 +00:00
Fix serialization of JSON-LD subarrays
Properties like "tag" could be lists containing multiple subclasses of ActivityObject. Make sure to serialize them recursively instead of outputting them as they are, because otherwise we could get a bunch of nulls in the resulting JSON and that wouldn't necessarily be a valid JSON-LD object. Fix: #2451
This commit is contained in:
parent
8ec984c3ff
commit
cdc833657f
2 changed files with 25 additions and 18 deletions
|
@ -194,6 +194,11 @@ class ActivityObject:
|
||||||
try:
|
try:
|
||||||
if issubclass(type(v), ActivityObject):
|
if issubclass(type(v), ActivityObject):
|
||||||
data[k] = v.serialize()
|
data[k] = v.serialize()
|
||||||
|
elif isinstance(v, list):
|
||||||
|
data[k] = [
|
||||||
|
e.serialize() if issubclass(type(e), ActivityObject) else e
|
||||||
|
for e in v
|
||||||
|
]
|
||||||
except TypeError:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
data = {k: v for (k, v) in data.items() if v is not None and k not in omit}
|
data = {k: v for (k, v) in data.items() if v is not None and k not in omit}
|
||||||
|
@ -306,7 +311,9 @@ class Link(ActivityObject):
|
||||||
|
|
||||||
def serialize(self, **kwargs):
|
def serialize(self, **kwargs):
|
||||||
"""remove fields"""
|
"""remove fields"""
|
||||||
omit = ("id", "type", "@context")
|
omit = ("id", "@context")
|
||||||
|
if self.type == "Link":
|
||||||
|
omit += ("type",)
|
||||||
return super().serialize(omit=omit)
|
return super().serialize(omit=omit)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -191,14 +191,14 @@ class Status(TestCase):
|
||||||
self.assertEqual(activity["type"], "Note")
|
self.assertEqual(activity["type"], "Note")
|
||||||
self.assertEqual(activity["sensitive"], False)
|
self.assertEqual(activity["sensitive"], False)
|
||||||
self.assertIsInstance(activity["attachment"], list)
|
self.assertIsInstance(activity["attachment"], list)
|
||||||
self.assertEqual(activity["attachment"][0].type, "Document")
|
self.assertEqual(activity["attachment"][0]["type"], "Document")
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
re.match(
|
re.match(
|
||||||
r"https:\/\/your.domain.here\/images\/covers\/test_[A-z0-9]+.jpg",
|
r"https:\/\/your.domain.here\/images\/covers\/test_[A-z0-9]+.jpg",
|
||||||
activity["attachment"][0].url,
|
activity["attachment"][0]["url"],
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.assertEqual(activity["attachment"][0].name, "Test Edition")
|
self.assertEqual(activity["attachment"][0]["name"], "Test Edition")
|
||||||
|
|
||||||
def test_comment_to_activity(self, *_):
|
def test_comment_to_activity(self, *_):
|
||||||
"""subclass of the base model version with a "pure" serializer"""
|
"""subclass of the base model version with a "pure" serializer"""
|
||||||
|
@ -223,14 +223,14 @@ class Status(TestCase):
|
||||||
activity["content"],
|
activity["content"],
|
||||||
f'test content<p>(comment on <a href="{self.book.remote_id}">"Test Edition"</a>)</p>',
|
f'test content<p>(comment on <a href="{self.book.remote_id}">"Test Edition"</a>)</p>',
|
||||||
)
|
)
|
||||||
self.assertEqual(activity["attachment"][0].type, "Document")
|
self.assertEqual(activity["attachment"][0]["type"], "Document")
|
||||||
# self.assertTrue(
|
# self.assertTrue(
|
||||||
# re.match(
|
# re.match(
|
||||||
# r"https:\/\/your.domain.here\/images\/covers\/test_[A-z0-9]+.jpg",
|
# r"https:\/\/your.domain.here\/images\/covers\/test_[A-z0-9]+.jpg",
|
||||||
# activity["attachment"][0].url,
|
# activity["attachment"][0].url,
|
||||||
# )
|
# )
|
||||||
# )
|
# )
|
||||||
self.assertEqual(activity["attachment"][0].name, "Test Edition")
|
self.assertEqual(activity["attachment"][0]["name"], "Test Edition")
|
||||||
|
|
||||||
def test_quotation_to_activity(self, *_):
|
def test_quotation_to_activity(self, *_):
|
||||||
"""subclass of the base model version with a "pure" serializer"""
|
"""subclass of the base model version with a "pure" serializer"""
|
||||||
|
@ -262,14 +262,14 @@ class Status(TestCase):
|
||||||
activity["content"],
|
activity["content"],
|
||||||
f'a sickening sense <p>-- <a href="{self.book.remote_id}">"Test Edition"</a></p>test content',
|
f'a sickening sense <p>-- <a href="{self.book.remote_id}">"Test Edition"</a></p>test content',
|
||||||
)
|
)
|
||||||
self.assertEqual(activity["attachment"][0].type, "Document")
|
self.assertEqual(activity["attachment"][0]["type"], "Document")
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
re.match(
|
re.match(
|
||||||
r"https:\/\/your.domain.here\/images\/covers\/test_[A-z0-9]+.jpg",
|
r"https:\/\/your.domain.here\/images\/covers\/test_[A-z0-9]+.jpg",
|
||||||
activity["attachment"][0].url,
|
activity["attachment"][0]["url"],
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.assertEqual(activity["attachment"][0].name, "Test Edition")
|
self.assertEqual(activity["attachment"][0]["name"], "Test Edition")
|
||||||
|
|
||||||
def test_review_to_activity(self, *_):
|
def test_review_to_activity(self, *_):
|
||||||
"""subclass of the base model version with a "pure" serializer"""
|
"""subclass of the base model version with a "pure" serializer"""
|
||||||
|
@ -305,14 +305,14 @@ class Status(TestCase):
|
||||||
f'Review of "{self.book.title}" (3 stars): Review\'s name',
|
f'Review of "{self.book.title}" (3 stars): Review\'s name',
|
||||||
)
|
)
|
||||||
self.assertEqual(activity["content"], "test content")
|
self.assertEqual(activity["content"], "test content")
|
||||||
self.assertEqual(activity["attachment"][0].type, "Document")
|
self.assertEqual(activity["attachment"][0]["type"], "Document")
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
re.match(
|
re.match(
|
||||||
r"https:\/\/your.domain.here\/images\/covers\/test_[A-z0-9]+.jpg",
|
r"https:\/\/your.domain.here\/images\/covers\/test_[A-z0-9]+.jpg",
|
||||||
activity["attachment"][0].url,
|
activity["attachment"][0]["url"],
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.assertEqual(activity["attachment"][0].name, "Test Edition")
|
self.assertEqual(activity["attachment"][0]["name"], "Test Edition")
|
||||||
|
|
||||||
def test_review_to_pure_activity_no_rating(self, *_):
|
def test_review_to_pure_activity_no_rating(self, *_):
|
||||||
"""subclass of the base model version with a "pure" serializer"""
|
"""subclass of the base model version with a "pure" serializer"""
|
||||||
|
@ -330,14 +330,14 @@ class Status(TestCase):
|
||||||
f'Review of "{self.book.title}": Review name',
|
f'Review of "{self.book.title}": Review name',
|
||||||
)
|
)
|
||||||
self.assertEqual(activity["content"], "test content")
|
self.assertEqual(activity["content"], "test content")
|
||||||
self.assertEqual(activity["attachment"][0].type, "Document")
|
self.assertEqual(activity["attachment"][0]["type"], "Document")
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
re.match(
|
re.match(
|
||||||
r"https:\/\/your.domain.here\/images\/covers\/test_[A-z0-9]+.jpg",
|
r"https:\/\/your.domain.here\/images\/covers\/test_[A-z0-9]+.jpg",
|
||||||
activity["attachment"][0].url,
|
activity["attachment"][0]["url"],
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.assertEqual(activity["attachment"][0].name, "Test Edition")
|
self.assertEqual(activity["attachment"][0]["name"], "Test Edition")
|
||||||
|
|
||||||
def test_reviewrating_to_pure_activity(self, *_):
|
def test_reviewrating_to_pure_activity(self, *_):
|
||||||
"""subclass of the base model version with a "pure" serializer"""
|
"""subclass of the base model version with a "pure" serializer"""
|
||||||
|
@ -353,14 +353,14 @@ class Status(TestCase):
|
||||||
activity["content"],
|
activity["content"],
|
||||||
f'rated <em><a href="{self.book.remote_id}">{self.book.title}</a></em>: 3 stars',
|
f'rated <em><a href="{self.book.remote_id}">{self.book.title}</a></em>: 3 stars',
|
||||||
)
|
)
|
||||||
self.assertEqual(activity["attachment"][0].type, "Document")
|
self.assertEqual(activity["attachment"][0]["type"], "Document")
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
re.match(
|
re.match(
|
||||||
r"https:\/\/your.domain.here\/images\/covers\/test_[A-z0-9]+.jpg",
|
r"https:\/\/your.domain.here\/images\/covers\/test_[A-z0-9]+.jpg",
|
||||||
activity["attachment"][0].url,
|
activity["attachment"][0]["url"],
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.assertEqual(activity["attachment"][0].name, "Test Edition")
|
self.assertEqual(activity["attachment"][0]["name"], "Test Edition")
|
||||||
|
|
||||||
def test_favorite(self, *_):
|
def test_favorite(self, *_):
|
||||||
"""fav a status"""
|
"""fav a status"""
|
||||||
|
|
Loading…
Reference in a new issue