w3c / json-ld-api

JSON-LD 1.1 Processing Algorithms and API Specification
https://w3c.github.io/json-ld-api/
Other
73 stars 29 forks source link

The algorithm for processing a reverse term returns too early #565

Closed niklasl closed 11 months ago

niklasl commented 1 year ago

In section 4.2 Create Term Definition of the Context Processing Algorithms, step 13, processing a reverse term ends with:

Set the term definition of term in active context to definition and the value associated with defined's entry term to true and return.

But with that early return, the algorithm stops processing a reverse term definition here, and thus reverse terms will not use any compaction features captured in the subsequent steps (step 19 and onward); e.g. indexed containers).

Thus this example:

{
  "@context": {
    "@base": "https://example.org/",
    "@vocab": "https://example.net/ns#",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "statement": {"@reverse": "rdf:subject", "@container": "@index", "@index": "predicate"},
    "predicate": {"@id": "rdf:predicate", "@type": "@vocab"},
    "term": {"@id": "rdf:object", "@type": "@vocab"},
    "addedIn": {"@type": "@id"}
  },
  "@id": "/item/1",
  "statement": {
    "rdf:type": {"term": "A", "addedIn": "v1"}
  }
}

which should expand to:

[
  {
    "@id": "https://example.org/item/1",
    "@reverse": {
      "http://www.w3.org/1999/02/22-rdf-syntax-ns#subject": [
        {
          "https://example.net/ns#addedIn": [
            {
              "@id": "https://example.org/v1"
            }
          ],
          "http://www.w3.org/1999/02/22-rdf-syntax-ns#object": [
            {
              "@id": "https://example.net/ns#A"
            }
          ],
          "http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate": [
            {
              "@id": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
            }
          ]
        }
      ]
    }
  }
]

instead results in:

[
  {
    "@id": "https://example.org/item/1",
    "@reverse": {
      "http://www.w3.org/1999/02/22-rdf-syntax-ns#subject": [
        {
          "https://example.net/ns#addedIn": [
            {
              "@id": "https://example.org/v1"
            }
          ],
          "http://www.w3.org/1999/02/22-rdf-syntax-ns#object": [
            {
              "@id": "https://example.net/ns#A"
            }
          ],
          "@index": "rdf:type"
        }
      ]
    }
  }
]

since the term definition of statement is missing what term to use when interpreting the @index value.

The fix is simple: remove "and return" from the end of step 13, and revise step 14 to begin with "Otherwise".

This is consistent with playground and distiller behaviour, and by looking at implementation, e.g. the ruby implementation does not return early.

I have a fix for TRLD to reflect the above (applying that, example output changed accordingly to the expected behaviour).

pchampin commented 1 year ago

FTR PyLD 2.0.3 also provides the expected result