json-ld / json-ld.org

JSON for Linked Data's documentation and playground site
https://json-ld.org/
Other
852 stars 151 forks source link

Issue compacting using a context with a property value index #822

Open mgberg opened 9 months ago

mgberg commented 9 months ago

I'm running into (what seems like on the surface) a strange situation where documents with contexts containing an index can be parsed (e.g. expanded) correctly but cannot be round tripped back to the original form by compacting.

Consider the following example which comes directly from the JSON-LD specification here:

{
  "@context": {
    "@version": 1.1,
    "schema": "http://schema.org/",
    "name": "schema:name",
    "body": "schema:articleBody",
    "athletes": {
      "@id": "schema:athlete",
      "@container": "@index",
      "@index": "schema:jobTitle"
    }
  },
  "@id": "http://example.com/",
  "@type": "schema:SportsTeam",
  "name": "San Francisco Giants",
  "athletes": {
    "Catcher": {
      "@type": "schema:Person",
      "name": "Buster Posey"
    },
    "Starting Pitcher": {
      "@type": "schema:Person",
      "name": "Madison Bumgarner"
    }
  }
}

If I try to expand that JSON-LD, I get the following result:

[
  {
    "@id": "http://example.com/",
    "@type": [
      "http://schema.org/SportsTeam"
    ],
    "http://schema.org/athlete": [
      {
        "@type": [
          "http://schema.org/Person"
        ],
        "http://schema.org/name": [
          {
            "@value": "Buster Posey"
          }
        ],
        "http://schema.org/jobTitle": [
          {
            "@value": "Catcher"
          }
        ]
      },
      {
        "@type": [
          "http://schema.org/Person"
        ],
        "http://schema.org/name": [
          {
            "@value": "Madison Bumgarner"
          }
        ],
        "http://schema.org/jobTitle": [
          {
            "@value": "Starting Pitcher"
          }
        ]
      }
    ],
    "http://schema.org/name": [
      {
        "@value": "San Francisco Giants"
      }
    ]
  }
]

This is the correct expansion of the original document.

However, if I try to compact the expanded document using the original context, the JSON-LD playground returns the following error: jsonld.SyntaxError: Absolute IRI "schema:jobTitle" confused with prefix "schema".

Seems odd. Furthermore, if I use an absolute IRI as the error implies (i.e. "@index": "http://schema.org/jobTitle" instead of "@index": "schema:jobTitle"), the error isn't raised but the index isn't constructed correctly; the two athletes are sorted under "@none".

However, I can force it to compact the data to the original form using the following slightly modified context:

{
  "@context": {
    "@version": 1.1,
    "schema": "http://schema.org/",
    "name": "schema:name",
    "body": "schema:articleBody",
    "job_title": "schema:jobTitle",
    "athletes": {
      "@id": "schema:athlete",
      "@container": "@index",
      "@index": "job_title"
    }
  }
}

The problem with that, of course, is that the context is different from the original. However, the original context is technically still applicable since "job_title" alias cannot show up in the data (unless, of course, schema:jobTitle is used in a different way somewhere else in other data).

The JSON-LD playground was used to create the above examples, but its JSON-LD processor is not the only one that suffers from this issue; I originally happened upon it using a different JSON-LD processor.

I understand that I can avoid this issue by just using the modified context instead. However, I'm curious as to why it's even an issue in the first place. Why doesn't the compacting algorithm accept the index as defined in the original context (which can be used to expand the document correctly)? According to the error, it seems to expect an absolute IRI; if that's the case, why does the modified context work when "job_title" is also not an absolute IRI?

BigBlueHat commented 7 months ago

@mgberg do you happen to remember what other JSON-LD processor you were using that had this same issue?

@gkellogg's Distiller or @pchampin's SoWasm are good ones to try (though I don't think SoWasm has a compact interface...yet 😉).

Great find, though!

@davidlehn not sure this is a jsonld.js bug...but something to review at least.

mgberg commented 7 months ago

If I remember correctly I was using pyld at the time.