w3c / json-ld-api

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

Using flatten() on HTML input having a single script element with an object with `@graph` results in named graph #604

Closed gkellogg closed 1 month ago

gkellogg commented 5 months ago

(Note, this impacts YAML-LD which will treat input as multiple documents when extractAllScripts is true, which is the default for flatten() and toRdf()).

Given an input document such as the following:

<html>
  <head>
    <script type="application/ld+json">
    {
      "@context": {"ex": "http://example.com/"},
      "@graph": [
        {"ex:foo": {"@value": "foo"}},
        {"ex:bar": {"@value": "bar"}}
      ]
    }
    </script>
  </head>
</html>

Extracting this using the extractAllScripts options gives the following:

[{
  "@context": {"ex": "http://example.com/"},
  "@graph": [
    {"ex:foo": {"@value": "foo"}},
    {"ex:bar": {"@value": "bar"}}
  ]
}]

Expanding this retains @graph:

[{
  "@graph": [
    {"http://example.com/foo": [{"@value": "foo"}]},
    {"http://example.com/bar": [{"@value": "bar"}]}
  ]
}]

Where I think the expected result is:

[[
  {"http://example.com/foo": [{"@value": "foo"}]},
  {"http://example.com/bar": [{"@value": "bar"}]}
]]

Because expansion is part of flattening, that @graph will now be treated like a named graph. In the expand() algorithm, it's only if the result is an object containing only the key @graph that @graph is eliminated.

Updating flatten() to make sure that the default for extractAllScripts is false (see #603) would help, but would still leave a problem with toRdf() where the default is explicitly true resulting in a named graph result, which does not seem to be intended.

A possible remedy for this would be to update step 8.1 of expand() to allow for an array and extracting the content of @graph for each contained object should achieve the desired affect.

Even though the HTML use case is fairly narrow, it affects all use of flatten() and toRdf() for YAML-LD.

gkellogg commented 4 months ago
Discussed on [today's call](https://json-ld.org/minutes/2024-07-10/#44)
https://github.com/w3c/json-ld-api/issues/604 -> Issue 604 Using flatten() on HTML input having a single script element with an object with `@graph` results in named graph (by gkellogg) [spec:enhancement] [needs discussion] [ErratumRaised]
Gregg Kellogg: Use `@included` instead of `@graph` if this is a problem.
PROPOSAL: No change to current behavior. Document issues in JSON-LD and YAML-LD
Gregg Kellogg: +1
Pierre-Antoine Champin: +1
Ted Thibodeau Jr.: +1
Benjamin Young: +1
David I. Lehn: +0
Pierre-Antoine Champin: Looking at the issue, I realized that the Rust library for JSON-LD is not compliant. It does remove `@graph` using example input.
... We might be missing a test about this case. I haven't had a chance to check yet.
... I mostly copied the first JSON example which is just an array with an object using @context and `@graph` and nothing else.

RESOLUTION: No change to current behavior. Document issues in JSON-LD and YAML-LD