ruby-rdf/json-ld

Too much compacting mixing external with local context

digitalheir opened this issue · 3 comments

Consider:

require 'rubygems'
require 'json/ld'

input = JSON.parse %([{
    "http://xmlns.com/foaf/0.1/name": ["Manu Sporny"],
    "http://xmlns.com/foaf/0.1/homepage": [{"@id": "http://manu.sporny.org/"}],
    "http://xmlns.com/foaf/0.1/avatar": [{"@id": "http://twitter.com/account/profile_image/manusporny"}]
}])

context = JSON.parse('{
  "@context": [
"http://assets.lawly.eu/ld/context.jsonld",
{
    "name": "http://xmlns.com/foaf/0.1/name",
    "homepage": {"@id": "http://xmlns.com/foaf/0.1/homepage", "@type": "@id"},
    "avatar": {"@id": "http://xmlns.com/foaf/0.1/avatar", "@type": "@id"}
  }
]
}')['@context']

puts JSON::LD::API.compact(input, context)

Result:

{"@context"=>"http://assets.lawly.eu/ld/context.jsonld", "avatar"=>"http://twitter.com/account/profile_image/manusporny", "homepage"=>"http://manu.sporny.org/", "name"=>"Manu Sporny"}

I would expect the name, homepage and avatar mappings to show up in the context.

To compact values as terms, in addition to properties and types, use @ype: @vocab instead of @id in the term definition.

You might try examples in the JSON-LD playground: http://json-ld.org/playground

Sorry, I miss-understood your issue when reading earlier. Putting the equivalent of this in the JSON-LD playground does generate an issue though: it seems you're not setting CORS headers on the context, so the playground can't process it.

The bug can more easily be demonstrated as follows:

context = [
  "http://assets.lawly.eu/ld/context.jsonld",
  {
    "name" => "http://xmlns.com/foaf/0.1/name",
    "homepage" => {"@id" => "http://xmlns.com/foaf/0.1/homepage", "@type" => "@id"},
    "avatar" => {"@id" => "http://xmlns.com/foaf/0.1/avatar", "@type" => "@id"}
  }
]

puts JSON::LD::Context.new().parse(context).serialize
  # => {"@context"=>"http://assets.lawly.eu/ld/context.jsonld"}

There is some code which tries to "optimize" the resulting context serialization, which obviously has a bug. I'll get a fix out shortly. Thanks.

Thanks, also thanks for spotting the CORS settings!