Skip to content

Commit 6085218

Browse files
committed
Accept @type as a list containing a single IRI
as is found is JSON-LD documents from the EBRAINS Knowledge Graph, for example
1 parent 776dec3 commit 6085218

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

pipeline/src/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ def from_jsonld(cls, data):
119119
data_copy = data.copy()
120120
context = data_copy.pop("@context", None)
121121
type_ = data_copy.pop("@type")
122+
if isinstance(type_, list) and len(type_) == 1:
123+
type_ = type_[0]
122124
if type_ and type_ != cls.type_:
123125
raise TypeError(f"Mismatched types. Data has '{type_}' but trying to create '{cls.type_}'")
124126
deserialized_data = {}

pipeline/src/properties.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,21 @@ def deserialize_item(item):
206206
elif all(issubclass(t, Node) for t in self.types):
207207
# use data["@type"] to figure out class to use
208208
if "@type" in item:
209+
if isinstance(item["@type"], list) and len(item["@type"]) == 1:
210+
item_type = item["@type"][0]
211+
else:
212+
item_type = item["@type"]
209213
for cls in self.types:
210-
if cls.type_ == item["@type"]:
214+
if cls.type_ == item_type:
211215
if set(item.keys()) == link_keys:
212216
# if we only have @id and @type, it's a Link
213217
return Link(item["@id"], allowed_types=[cls])
214218
else:
215219
# otherwise it's a Node
216220
return cls.from_jsonld(item)
217221
raise TypeError(
218-
f"Mismatched types. Data has '{item['@type']}' "
219-
f"but property only allows {[cls.type_ for cls in self.types]}"
222+
f"Mismatched types. Data has '{item_type}' "
223+
f"but property only allows one of {[cls.type_ for cls in self.types]}"
220224
)
221225
else:
222226
return Link(item["@id"], allowed_types=self.types)

0 commit comments

Comments
 (0)