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 469eb96..1138d5a 100644 --- a/include/ro-crate.hpp +++ b/include/ro-crate.hpp @@ -7,7 +7,6 @@ #include #include #include -#include namespace rocrate { @@ -254,15 +253,7 @@ namespace rocrate { EntityRegister getEntities() const; /** - * 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. @@ -321,9 +312,6 @@ namespace rocrate { ); EntityRegister entities_; - std::vector remoteContext_ = { - "https://w3id.org/ro/crate/1.3/context", - }; std::map context_; }; @@ -396,6 +384,7 @@ 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."); } @@ -418,22 +407,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}}); } @@ -478,10 +459,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 029cdfc..a43d5f4 100644 --- a/tests/unit.cpp +++ b/tests/unit.cpp @@ -200,7 +200,7 @@ 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); } @@ -244,10 +244,11 @@ 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_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("getContext returns the correct combined context", "[unit]") {