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

type-scoped local context is not applied if the @type property is @nested #831

Open grenik opened 6 months ago

grenik commented 6 months ago

The following example works as expected (playground permalink)

{
  "@context": {
    "type": "@type",
    "name": "http://example.com/vocab#top-level-name",
    "MyType": {
      "@id": "http://example.com/vocab#MyTypeURI",
      "@context": {
        "name": "http://example.com/vocab#typed-name"
      }
    }
  },
  "type": "MyType",
  "name": "top-level-name"
}

is expanded to:

[
  {
    "http://example.com/vocab#typed-name": [
      {
        "@value": "top-level-name"
      }
    ],
    "@type": [
      "http://example.com/vocab#MyTypeURI"
    ]
  }
]

so the URI for name is taken from the type-scoped context and is ...typed-name

Error:

Once I @nest the @type definition in __metadata wrapper object, the typed-context is not applied anymore and thus the name is not mapped to an IRI, even though @type is extracted:

Example 1:

Playground permalinks: with and without @nest for @type.

{
  "@context": {
    "type": "@type",
+   "__metadata": "@nest",
    "MyType": {
      "@id": "http://example.com/vocab#MyTypeURI",
      "@context": {
        "name": "http://example.com/vocab#typed-name"
      }
    }
  },
  "name": "top-level-name",
+ "__metadata": {
    "type": "MyType"
+ }
}

results in typed context not being applied and the name being not mapped anymore:

[
  {
-   "http://example.com/vocab#typed-name": [
-     {
-       "@value": "top-level-name"
-     }
-   ],
    "@type": [
      "http://example.com/vocab#MyTypeURI"
    ]
  }
]

same if I specify @nest for the @type as well:

{
  "@context": {
    "type": {
+     "@nest": "__metadata",
      "@id": "@type"
    },
+   "__metadata": "@nest",
    "MyType": {
      "@id": "http://example.com/vocab#MyTypeURI",
      "@context": {
        "name": "http://example.com/vocab#typed-name"
      }
    }
  },
  "name": "top-level-name",
+ "__metadata": {
    "type": "MyType"
+ }
}

I expect the same result as in the original example on top, with name being mapped to http://example.com/vocab#typed-name

grenik commented 6 months ago

It looks like type-scoped contexts are applied before @nesting, but maybe I missed the part of the specification that states this processing order explicitly.

As for me the spec states the opposite and should behave as I expect:

Semantically, nesting is treated as if the properties and values were declared directly within the containing node object.

And here are even some examples:

image