Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions entity-with-multiple-types.out.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
]
}
9 changes: 6 additions & 3 deletions include/ro-crate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,14 @@ namespace rocrate {
}
std::string entityId = entityJson["@id"].get<std::string>();

// 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::string>();
std::vector<std::string> entityTypes;
for (const auto& type : entityJson["@type"]) {
entityTypes.push_back(type.get<std::string>());
}

// 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
Expand All @@ -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);
}
Expand Down
22 changes: 22 additions & 0 deletions tests/fixtures/entity-with-multiple-types.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
21 changes: 21 additions & 0 deletions tests/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <stdexcept>

#include "test_helpers.hpp"

using rocrate::Entity;

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -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
);
}
Loading