Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mgberg opened this issue Sep 12, 2023 · 2 comments
Open

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

mgberg opened this issue Sep 12, 2023 · 2 comments

Comments

@mgberg
Copy link

mgberg commented Sep 12, 2023

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?

@mgberg mgberg changed the title Issue framing using a context with a property value index Issue compacting using a context with a property value index Sep 12, 2023
@BigBlueHat
Copy link
Member

@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
Copy link
Author

mgberg commented Dec 6, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants