UTS-eResearch / ro-crate-js

Research Object Crate (RO-Crate) utilities
GNU General Public License v3.0
2 stars 6 forks source link

Gotcha: not calling index() before modifying an ro-crate #1

Open spikelynch opened 4 years ago

spikelynch commented 4 years ago

Just spent a while tracing a bug in some code which was meant to add a namespaced identifier to an already-existing ROCrate, and I forgot to add the .index() step after initialising the object from the raw JSON-LD.

This fails silently - ie it lets you call addIdentifier, but doesn't complain or throw an error, and the JSON-LD written out by the last line is unchanged.

const roc = new rocrate.ROCrate(jsonld_raw);
roc.addIdentifier({identifier: oid, name: namespace})
const jsonld = roc.json_ld;
await fs.writeFile(jsonld_file, JSON.stringify(jsonld, null, 2));

Adding the index() makes it work:

const roc = new rocrate.ROCrate(jsonld_raw);
roc.index();
roc.addIdentifier({identifier: oid, name: namespace})
const jsonld = roc.json_ld;
await fs.writeFile(jsonld_file, JSON.stringify(jsonld, null, 2));

It would be better if either indexing were automatic - the method isn't async, so it could be in the constructor - or if methods which depended on it having been called should call it themselves (better) or throw an error if it hasn't been called (not good)