diff --git a/pipeline/src/base.py b/pipeline/src/base.py index 47d4d703..5bb2f275 100644 --- a/pipeline/src/base.py +++ b/pipeline/src/base.py @@ -92,6 +92,17 @@ def to_jsonld(self, include_empty_properties=True, embed_linked_nodes=True, with ) for item in value ] + elif isinstance(value, (tuple, list)): + # if property.multiple is False, then this means the node does not validate, + # but we should try to serialize it anyway + data[property.path] = [ + value_to_jsonld( + item, + include_empty_properties=include_empty_properties, + embed_linked_nodes=embed_linked_nodes, + ) + for item in value + ] else: data[property.path] = value_to_jsonld( value, diff --git a/pipeline/src/properties.py b/pipeline/src/properties.py index ba432353..ad730392 100644 --- a/pipeline/src/properties.py +++ b/pipeline/src/properties.py @@ -194,7 +194,7 @@ def deserialize_item(item): else: raise NotImplementedError() - if self.multiple and isinstance(data, (tuple, list)): + if isinstance(data, (tuple, list)): return [deserialize_item(item) for item in data] else: return deserialize_item(data) diff --git a/pipeline/tests/test_regressions.py b/pipeline/tests/test_regressions.py index 2c750a94..595b70b2 100644 --- a/pipeline/tests/test_regressions.py +++ b/pipeline/tests/test_regressions.py @@ -235,3 +235,22 @@ def test_issue0023(): assert len(dv_again.custodians[0].affiliations) == 2 assert dv_again.custodians[0].affiliations[0].member_of.full_name == "University of This Place" assert dv_again.custodians[0].affiliations[1].member_of.full_name == "University of That Place" + + +def test_issue0056(): + # https://github.com/openMetadataInitiative/openMINDS_Python/issues/56 + # Since we are permissive on object creation, serialization to JSON-LD should work + # even if the object gives validation failures. + # However, under some circumstances, to_jsonld() produces a data structure + # that cannot be saved as a JSON string. + dataset = omcore.Dataset( + digital_identifier=[ + omcore.DOI(identifier="abc"), + omcore.DOI(identifier="def") + ] + ) + failures = dataset.validate(ignore=["required"]) + assert len(failures) == 1 + assert failures["multiplicity"] == ['digital_identifier does not accept multiple values, but contains 2'] + data = dataset.to_jsonld() + json.dumps(data) # this should not raise an Exception