digitalbazaar / jsonld.js

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

@type in context not expanding @base #276

Open rubensworks opened 5 years ago

rubensworks commented 5 years ago

Parsing the following throws an error:

{
  "@context": {
    "@base": "http://example.org/",
    "@vocab": null,
    "p": { "@id": "http://ex.org/p", "@type": "bla" }
  },
  "@id": "",
  "p": "bla"
}

Error:

jsonld.SyntaxError: Invalid JSON-LD syntax; an @context @type value must be an absolute IRI.

As far as I understand, this should however not throw an error, and instead expand the @type based on the @base value.

If I'm correct, this document is equivalent to the following: (which does not produce an error)

{
  "@context": {
    "@base": "http://example.org/",
    "@vocab": null
  },
  "@id": "",
  "http://ex.org/p": { "@value": "val", "@type": "bla" }
}

Output:

<http://example.org/> <http://ex.org/p> "val"^^<http://example.org/bla> .
gkellogg commented 5 years ago

From Context Definitions:

If the expanded term definition contains the @type keyword, its value MUST be an absolute IRI, a compact IRI, a term, null, or one of the keywords @id or @vocab.

In the first case "bla" does not expand to any of those, thus the error is raised.

rubensworks commented 5 years ago

If I understand that definition correctly, then the following should also produce an error, right? (As it includes a relative IRI, but this time with a valid vocabulary)

{
  "@context": {
    "@base": "http://base.org/",
    "@vocab": "http://vocab.org/"
  },
  "@id": "",
  "http://ex.org/p": { "@value": "val", "@type": "bla" }
}

Instead, it outputs:

<http://base.org/> <http://ex.org/p> "val"^^<http://vocab.org/bla> .

From a developers-perspective, this feels like an inconsistency in the spec. So I'm wondering if it would make sense to discuss this over at https://github.com/w3c/json-ld-syntax/ and move the issue?

gkellogg commented 5 years ago

Ecause of @vocab, “bla” does expand to an absolute IRI, so is correct. In any case, it’s not an error, but the resulting triple would be dropped if it contains a relative IRI.