From 14d7ec4ea06b646b3275b4ed21cc9da96218757c Mon Sep 17 00:00:00 2001 From: Oliver Woolland Date: Wed, 18 Nov 2026 16:47:57 +0000 Subject: [PATCH] Allow reading of multiple types --- entity-with-multiple-types.out.json | 32 +++++++++++++++++++ include/ro-crate.hpp | 9 ++++-- .../fixtures/entity-with-multiple-types.json | 22 +++++++++++++ tests/unit.cpp | 21 ++++++++++++ 4 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 entity-with-multiple-types.out.json create mode 100644 tests/fixtures/entity-with-multiple-types.json diff --git a/entity-with-multiple-types.out.json b/entity-with-multiple-types.out.json new file mode 100644 index 0000000..6b9df27 --- /dev/null +++ b/entity-with-multiple-types.out.json @@ -0,0 +1,32 @@ +{ + "@context": "https://w3id.org/ro/crate/1.3/context", + "@graph": [ + { + "@id": "./", + "@type": "Dataset" + }, + { + "@id": "https://w3id.org/workflowhub/workflow-ro-crate/1.0", + "@type": [ + "CreativeWork", + "Profile" + ], + "name": "Process Run Crate" + }, + { + "@id": "ro-crate-metadata.json", + "@type": "CreativeWork", + "about": { + "@id": "./" + }, + "conformsTo": [ + { + "@id": "https://w3id.org/ro/crate/1.3" + }, + { + "@id": "https://w3id.org/workflowhub/workflow-ro-crate/1.0" + } + ] + } + ] +} \ No newline at end of file diff --git a/include/ro-crate.hpp b/include/ro-crate.hpp index 2d6f5d0..538b8f4 100644 --- a/include/ro-crate.hpp +++ b/include/ro-crate.hpp @@ -364,11 +364,14 @@ namespace rocrate { } std::string entityId = entityJson["@id"].get(); - // Get the entity type + // Get the entity's types if (!entityJson.contains("@type")) { throw std::runtime_error("Entity in RO-Crate JSON is missing '@type' field."); } - std::string entityType = entityJson["@type"].get(); + std::vector entityTypes; + for (const auto& type : entityJson["@type"]) { + entityTypes.push_back(type.get()); + } // Try getting the entity from the register, if it exists, otherwise create a new one Entity entity({"Thing"}); // Default type, will be overridden if entity exists @@ -378,7 +381,7 @@ namespace rocrate { updateProperties(entity, entityJson); } catch (const std::runtime_error&) { // Entity does not exist, create a new one - entity = Entity({entityType}); + entity = Entity(entityTypes); updateProperties(entity, entityJson); addEntity(entityId, entity); } diff --git a/tests/fixtures/entity-with-multiple-types.json b/tests/fixtures/entity-with-multiple-types.json new file mode 100644 index 0000000..3510fa0 --- /dev/null +++ b/tests/fixtures/entity-with-multiple-types.json @@ -0,0 +1,22 @@ +{ "@context": "https://w3id.org/ro/crate/1.3/context", + "@graph": [ + { + "@id": "ro-crate-metadata.json", + "@type": "CreativeWork", + "about": {"@id": "./"}, + "conformsTo": [ + {"@id": "https://w3id.org/ro/crate/1.3"}, + {"@id": "https://w3id.org/workflowhub/workflow-ro-crate/1.0"} + ] + }, + { + "@id": "./", + "@type": "Dataset" + }, + { + "@id": "https://w3id.org/workflowhub/workflow-ro-crate/1.0", + "@type": ["CreativeWork", "Profile"], + "name": "Process Run Crate" + } + ] +} diff --git a/tests/unit.cpp b/tests/unit.cpp index 01fff55..7e8e1ff 100644 --- a/tests/unit.cpp +++ b/tests/unit.cpp @@ -4,6 +4,8 @@ #include +#include "test_helpers.hpp" + using rocrate::Entity; // ------------------------------------------------------------------------------ @@ -185,3 +187,22 @@ TEST_CASE("Read in RO-Crate from JSON-LD without throwing", "[unit]") { ) ); } + +TEST_CASE("Read in RO-Crate with an entity typed with an array of types", "[unit]") { + rocrate::ROCrate crate; + + REQUIRE_NOTHROW( + crate.readIn( + std::string(TEST_SOURCE_DIR) + "/tests/fixtures/entity-with-multiple-types.json" + ) + ); + + const std::string outputPath = + std::string(TEST_SOURCE_DIR) + "/entity-with-multiple-types.out.json"; + crate.writeOut(outputPath); + + REQUIRE_RO_CRATE_FILE_EQUAL_BY_ID( + std::string(TEST_SOURCE_DIR) + "/tests/fixtures/entity-with-multiple-types.json", + outputPath + ); +}