Skip to content

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

@gkellogg

Description

@gkellogg

(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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions