From a740c31d6eea171a562a73cd654ac0197808d88f Mon Sep 17 00:00:00 2001 From: Oliver Woolland <86293426+OliverWoolland@users.noreply.github.com> Date: Mon, 23 Nov 2026 12:44:15 +0000 Subject: [PATCH 1/2] Revert "Array context" --- .gitignore | 1 - include/ro-crate.hpp | 49 ++++----------------------------- tests/fixtures/add-context.json | 25 ----------------- tests/unit.cpp | 35 ++++------------------- 4 files changed, 11 insertions(+), 99 deletions(-) delete mode 100644 tests/fixtures/add-context.json diff --git a/.gitignore b/.gitignore index 1985da3..c5f1728 100644 --- a/.gitignore +++ b/.gitignore @@ -68,4 +68,3 @@ vcpkg_installed/ # test output & cache Testing/ .cache/ -*out* \ No newline at end of file diff --git a/include/ro-crate.hpp b/include/ro-crate.hpp index 9ba4094..2f9216f 100644 --- a/include/ro-crate.hpp +++ b/include/ro-crate.hpp @@ -7,7 +7,6 @@ #include #include #include -#include namespace rocrate { @@ -194,15 +193,7 @@ namespace rocrate { Entity& getEntity(const std::string& id); /** - * Adds a context entry to the RO-Crate's root metadata entity, without a prefix. - * - * @param value The context value associated with the key. - * @throw std::invalid_argument if the value is empty. - */ - void addContext(const std::string& value); - - /** - * Adds a context entry to the RO-Crate's root metadata entity, with a prefix. + * Adds a context entry to the RO-Crate's root metadata entity. * * @param key The context key to add. * @param value The context value associated with the key. @@ -247,9 +238,6 @@ namespace rocrate { ); EntityRegister entities_; - std::vector remoteContext_ = { - "https://w3id.org/ro/crate/1.3/context", - }; std::map context_; }; @@ -299,21 +287,8 @@ namespace rocrate { return it->second; } - inline void ROCrate::addContext(const std::string& value) { - if (value.empty()) { - throw std::invalid_argument("Context value cannot be empty."); - } - - // Ignore if duplicate context value is already present - if (std::find(remoteContext_.begin(), remoteContext_.end(), value) != remoteContext_.end()) { - return; // Context value already exists, do not add it again - } - - // Add the context entry to the RO-Crate's root metadata entity - remoteContext_.push_back(value); - } - inline void ROCrate::addContext(const std::string& key, const std::string& value) { + // Validate the context if (key.empty()) { throw std::invalid_argument("Context key cannot be empty."); } @@ -328,22 +303,14 @@ namespace rocrate { inline void ROCrate::writeOut(const std::string& path) { // Create the JSON representation of the RO-Crate nlohmann::json outCrate = { + {"@context", "https://w3id.org/ro/crate/1.3/context"}, {"@graph", nlohmann::json::array()} }; // Add the context entries to the JSON representation - if (!remoteContext_.empty() || !context_.empty()) { - // Remote contexts: bare string if only one, array if multiple - if (remoteContext_.size() == 1 && context_.empty()) { - outCrate["@context"] = remoteContext_[0]; - } else { - outCrate["@context"] = nlohmann::json::array(); - for (const auto& remote : remoteContext_) { - outCrate["@context"].push_back(remote); - } - } - - // Local contexts + if (!context_.empty()) { + outCrate["@context"] = nlohmann::json::array(); + outCrate["@context"].push_back("https://w3id.org/ro/crate/1.3/context"); for (const auto& [key, value] : context_) { outCrate["@context"].push_back({{key, value}}); } @@ -388,10 +355,6 @@ namespace rocrate { for (const auto& [key, value] : contextEntry.items()) { addContext(key, value.get()); } - } else if (contextEntry.is_string()) { - addContext(contextEntry.get()); - } else { - throw std::runtime_error("Invalid context entry in RO-Crate JSON: " + contextEntry.dump()); } } } diff --git a/tests/fixtures/add-context.json b/tests/fixtures/add-context.json deleted file mode 100644 index 2cd60e1..0000000 --- a/tests/fixtures/add-context.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "@context": [ - "https://w3id.org/ro/crate/1.3/context", - "https://example.com/context2.jsonld", - { - "example": "https://example.com/context.jsonld" - } - ], - "@graph": [ - { - "@id": "./", - "@type": "Dataset" - }, - { - "@id": "ro-crate-metadata.json", - "@type": "CreativeWork", - "about": { - "@id": "./" - }, - "conformsTo": { - "@id": "https://w3id.org/ro/crate/1.3" - } - } - ] -} diff --git a/tests/unit.cpp b/tests/unit.cpp index d9fde6e..b915e50 100644 --- a/tests/unit.cpp +++ b/tests/unit.cpp @@ -129,39 +129,14 @@ TEST_CASE("Duplicate context doesn't throw", "[unit]") { } -TEST_CASE("Add context throws if context value is empty", "[unit]") { +TEST_CASE("Add context throws if context is empty", "[unit]") { rocrate::ROCrate crate; - REQUIRE_THROWS_AS(crate.addContext("example", ""), std::invalid_argument); -} - -TEST_CASE("Add context with and without prefix (key) yields correct JSON-LD output", "[unit]") { - rocrate::ROCrate crate; - - // Add context with prefix - crate.addContext("example", "https://example.com/context.jsonld"); - - // Add context without prefix - crate.addContext("https://example.com/context2.jsonld"); - - // Roundtrip the crate to JSON-LD and back to ensure the contexts are preserved - const std::string outputPath = - std::string(TEST_SOURCE_DIR) + "/add-context.out.json"; - crate.writeOut(outputPath); - - REQUIRE_RO_CRATE_FILE_EQUAL_BY_ID( - std::string(TEST_SOURCE_DIR) + "/tests/fixtures/add-context.json", - outputPath - ); - - // Read the crate back in and check that the contexts are still present - rocrate::ROCrate crate2; - REQUIRE_NOTHROW(crate2.readIn(outputPath)); - crate2.writeOut(outputPath + ".roundtrip.json"); - REQUIRE_RO_CRATE_FILE_EQUAL_BY_ID( - std::string(TEST_SOURCE_DIR) + "/tests/fixtures/add-context.json", - outputPath + ".roundtrip.json" + REQUIRE_THROWS_AS( + crate.addContext("", "https://example.com/context.jsonld"), + std::invalid_argument ); + REQUIRE_THROWS_AS(crate.addContext("example", ""), std::invalid_argument); } TEST_CASE("Add entity throws if ID is empty", "[unit]") { From 717b013274d27e514411c073086a809c38612b2e Mon Sep 17 00:00:00 2001 From: OliverWoolland <86293426+OliverWoolland@users.noreply.github.com> Date: Mon, 30 Nov 2026 14:10:16 +0000 Subject: [PATCH 2/2] Restore array context after merge Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- .gitignore | 1 + include/ro-crate.hpp | 35 +++++++++++++++++++++++++++------ tests/fixtures/add-context.json | 25 +++++++++++++++++++++++ tests/unit.cpp | 14 ++++++++----- 4 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 tests/fixtures/add-context.json diff --git a/.gitignore b/.gitignore index c5f1728..1985da3 100644 --- a/.gitignore +++ b/.gitignore @@ -68,3 +68,4 @@ vcpkg_installed/ # test output & cache Testing/ .cache/ +*out* \ No newline at end of file diff --git a/include/ro-crate.hpp b/include/ro-crate.hpp index 1138d5a..469eb96 100644 --- a/include/ro-crate.hpp +++ b/include/ro-crate.hpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace rocrate { @@ -253,7 +254,15 @@ namespace rocrate { EntityRegister getEntities() const; /** - * Adds a context entry to the RO-Crate's root metadata entity. + * Adds a context entry to the RO-Crate's root metadata entity, without a prefix. + * + * @param value The context value associated with the key. + * @throw std::invalid_argument if the value is empty. + */ + void addContext(const std::string& value); + + /** + * Adds a context entry to the RO-Crate's root metadata entity, with a prefix. * * @param key The context key to add. * @param value The context value associated with the key. @@ -312,6 +321,9 @@ namespace rocrate { ); EntityRegister entities_; + std::vector remoteContext_ = { + "https://w3id.org/ro/crate/1.3/context", + }; std::map context_; }; @@ -384,7 +396,6 @@ namespace rocrate { } inline void ROCrate::addContext(const std::string& key, const std::string& value) { - // Validate the context if (key.empty()) { throw std::invalid_argument("Context key cannot be empty."); } @@ -407,14 +418,22 @@ namespace rocrate { inline void ROCrate::writeOut(const std::string& path) { // Create the JSON representation of the RO-Crate nlohmann::json outCrate = { - {"@context", "https://w3id.org/ro/crate/1.3/context"}, {"@graph", nlohmann::json::array()} }; // Add the context entries to the JSON representation - if (!context_.empty()) { - outCrate["@context"] = nlohmann::json::array(); - outCrate["@context"].push_back("https://w3id.org/ro/crate/1.3/context"); + if (!remoteContext_.empty() || !context_.empty()) { + // Remote contexts: bare string if only one, array if multiple + if (remoteContext_.size() == 1 && context_.empty()) { + outCrate["@context"] = remoteContext_[0]; + } else { + outCrate["@context"] = nlohmann::json::array(); + for (const auto& remote : remoteContext_) { + outCrate["@context"].push_back(remote); + } + } + + // Local contexts for (const auto& [key, value] : context_) { outCrate["@context"].push_back({{key, value}}); } @@ -459,6 +478,10 @@ namespace rocrate { for (const auto& [key, value] : contextEntry.items()) { addContext(key, value.get()); } + } else if (contextEntry.is_string()) { + addContext(contextEntry.get()); + } else { + throw std::runtime_error("Invalid context entry in RO-Crate JSON: " + contextEntry.dump()); } } } diff --git a/tests/fixtures/add-context.json b/tests/fixtures/add-context.json new file mode 100644 index 0000000..2cd60e1 --- /dev/null +++ b/tests/fixtures/add-context.json @@ -0,0 +1,25 @@ +{ + "@context": [ + "https://w3id.org/ro/crate/1.3/context", + "https://example.com/context2.jsonld", + { + "example": "https://example.com/context.jsonld" + } + ], + "@graph": [ + { + "@id": "./", + "@type": "Dataset" + }, + { + "@id": "ro-crate-metadata.json", + "@type": "CreativeWork", + "about": { + "@id": "./" + }, + "conformsTo": { + "@id": "https://w3id.org/ro/crate/1.3" + } + } + ] +} diff --git a/tests/unit.cpp b/tests/unit.cpp index a43d5f4..dd64c97 100644 --- a/tests/unit.cpp +++ b/tests/unit.cpp @@ -200,11 +200,16 @@ TEST_CASE("Duplicate context doesn't throw", "[unit]") { } -TEST_CASE("Add context throws if context is empty", "[unit]") { +TEST_CASE("Add context throws if context value is empty", "[unit]") { rocrate::ROCrate crate; REQUIRE_THROWS_AS(crate.addContext("example", ""), std::invalid_argument); } +TEST_CASE("Add context throws if context key is empty", "[unit]") { + rocrate::ROCrate crate; + REQUIRE_THROWS_AS(crate.addContext("", "https://example.com/context.jsonld"), std::invalid_argument); +} + TEST_CASE("Add context with and without prefix (key) yields correct JSON-LD output", "[unit]") { rocrate::ROCrate crate; @@ -244,11 +249,10 @@ TEST_CASE("Add context with and without prefix (key) yields correct JSON-LD outp REQUIRE_NOTHROW(crate2.readIn(outputPath)); crate2.writeOut(outputPath + ".roundtrip.json"); - REQUIRE_THROWS_AS( - crate.addContext("", "https://example.com/context.jsonld"), - std::invalid_argument + REQUIRE_RO_CRATE_FILE_EQUAL_BY_ID( + std::string(TEST_SOURCE_DIR) + "/tests/fixtures/add-context.json", + outputPath + ".roundtrip.json" ); - REQUIRE_THROWS_AS(crate.addContext("example", ""), std::invalid_argument); } TEST_CASE("getContext returns the correct combined context", "[unit]") {