From 47bf0f579d4d5785042611f6ef89d6870f57ee90 Mon Sep 17 00:00:00 2001 From: Alain St-Denis Date: Sun, 11 Feb 2024 10:23:52 -0500 Subject: [PATCH] Nested fields: handle unknown json-ld types more gracefully. --- federation/entities/activitypub/models.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/federation/entities/activitypub/models.py b/federation/entities/activitypub/models.py index 082f5e7..cb95320 100644 --- a/federation/entities/activitypub/models.py +++ b/federation/entities/activitypub/models.py @@ -209,7 +209,11 @@ class MixedField(fields.Nested): ret = [] for item in value: if item.get('@type'): - res = super()._deserialize(item, attr, data, **kwargs) + try: + res = super()._deserialize(item, attr, data, **kwargs) + except KeyError as ex: + logger.warning("nested field: undefined JSON-LD type %s", ex) + continue ret.append(res if not isinstance(res, list) else res[0]) else: ret.append(self.iri._deserialize(item, attr, data, **kwargs)) @@ -247,7 +251,7 @@ class Object(BaseEntity, metaclass=JsonLDAnnotation): icon = MixedField(as2.icon, nested='ImageSchema') image = MixedField(as2.image, nested='ImageSchema') tag_objects = MixedField(as2.tag, nested=['NoteSchema', 'HashtagSchema','MentionSchema','PropertyValueSchema','EmojiSchema'], many=True) - attachment = fields.Nested(as2.attachment, nested=['LinkSchema', 'NoteSchema', 'ImageSchema', 'AudioSchema', 'DocumentSchema','PropertyValueSchema','IdentityProofSchema'], + attachment = MixedField(as2.attachment, nested=['LinkSchema', 'NoteSchema', 'ImageSchema', 'AudioSchema', 'DocumentSchema','PropertyValueSchema','IdentityProofSchema'], many=True, default=[]) content_map = LanguageMap(as2.content) # language maps are not implemented in calamus context = fields.RawJsonLD(as2.context)