eclipse-rdf4j / rdf4j

Eclipse RDF4J: scalable RDF for Java
https://rdf4j.org/
BSD 3-Clause "New" or "Revised" License
361 stars 164 forks source link

JSON-LD Parsing - missing exception with invalid @id #3658

Open pajoma opened 2 years ago

pajoma commented 2 years ago

Current Behavior

Parsing invalid "@id" values in a JSON-LD document like in the following example just resulted in no statements. (via RIO and ContextStatementCollector)

{
  "@context": {
    "ical": "http://www.w3.org/2002/12/cal/ical#",
    "my": "http://example.org#"
  },
  "@type": "ical:Vevent",
  "@id": "1234",
  "ical:summary": "Lady Gaga Concert"
}

Result: {statements=[], namespaces={}} (from ContextStatementCollector)

The following example is valid and works

{
  "@context": {
    "ical": "http://www.w3.org/2002/12/cal/ical#",
    "my": "http://example.org#"
  },
  "@type": "ical:Vevent",
  "@id": "my:1234",
  "ical:summary": "Lady Gaga Concert"
}

Result {statements=[ (http://example.org#cal124121323, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2002/12/cal/ical#Vevent), (http://example.org#cal124121323, http://www.w3.org/2002/12/cal/ical#summary, "Lady Gaga Concert")], namespaces={}}

The following example is also valid and works

{
  "@context": {
    "@base": "http://www.example.org",
    "ical": "http://www.w3.org/2002/12/cal/ical#"
  },
  "@type": "ical:Vevent",
  "@id": "1234",
  "ical:summary": "Lady Gaga Concert"
}

Expected Behavior

I am not exactly sure about the expected behaviour. According to the spec, the "@id" has to be an IRI. Leaving the prefix should require at least "@base" definition in the "@context" (even though the json-ld playground considers it to be valid and injects its own "@base")

{
  "@context": {
    "ical": "http://www.w3.org/2002/12/cal/ical#"
  },
  "@id": "1234",
  "ical:summary": "Lady Gaga Concert"
}

results in <https://json-ld.org/playground/1234> <http://www.w3.org/2002/12/cal/ical#summary> "Lady Gaga Concert" .

This might work (e.g. have a default namespace set when a base namespace is missing), but I think an exception makes most sense here.

Steps To Reproduce

Here's my code


...
RDFParser parser = .. from ParserFactory for JSON-LD
ContextStatementCollector collector = new ContextStatementCollector(simpleValueFactory);
try (InputStream is = dataBuffer.asInputStream(true)) {
   parser.setRDFHandler(collector);
   parser.parse(is);
...

``

### Version

4.0.0-M1

### Are you interested in contributing a solution yourself?

Perhaps?

### Anything else?

Here's the source code where the issue is happening: https://github.com/bechtleav360/eagl-service-graph/blob/main/src/main/java/com/bechtle/eagl/graph/config/converter/RdfDecoder.java
abrokenjester commented 2 years ago

I'm rusty on my JSON-LD but is it possible that this is because you're using a JDON-LD 1.1 feature? I think our current support (which relies on jsonld-java) only covers 1.0.

pajoma commented 2 years ago

The relevant part in the spec about node identifiers didn't change, as far as I can see. The @ID must be a valid URI/IRI, either fully qualified or resolvable through the default namespace (base).