digitalbazaar / jsonld.js

A JSON-LD Processor and API implementation in JavaScript
https://json-ld.org/
Other
1.65k stars 195 forks source link

@base prepend with a path, removes last folder if no end "/" #459

Open ethieblin opened 2 years ago

ethieblin commented 2 years ago

If we expand a URI with "@base" not ending with a "/", we get a URI missing the last path fragment. For example "@base":"http://example.com/loutre" with"@id":"toto" gives http://example.com/toto when we would expect http://example.com/loutre/toto. If "@base" ends with a "/" it works fine.

Playground expansion without "/"

Playground expansion with "/"

This issue is only on URI expansion, but the behaviour is different on URI compaction : a "@base" with or without "/" will give the same URI fragment. This will be a problem when round-tripping because the input URI will change after re-expansion.

Input: "@id":"http://example.com/loutre/toto" Compaction with "@base":"http://example.com/loutre" gives

"@context": {
    "@base": "http://example.com/loutre"
  },
  "@id": "toto"

Which re-expanded gives "http://example.com/toto".

Playground compaction, "@base" without "/"

Proposed fix: https://github.com/ethieblin/jsonld.js/pull/2