Skip to content

Commit

Permalink
Handle (in-order) type-scoped contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Mar 16, 2020
1 parent ba018c7 commit d2fce0e
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/entryhandler/keyword/EntryHandlerKeywordType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {ParsingContext} from "../../ParsingContext";
import {Util} from "../../Util";
import {EntryHandlerPredicate} from "../EntryHandlerPredicate";
import {EntryHandlerKeyword} from "./EntryHandlerKeyword";
import {IJsonLdContextNormalized} from "jsonld-context-parser/lib/JsonLdContext";

/**
* Handles @graph entries.
Expand Down Expand Up @@ -32,6 +33,21 @@ export class EntryHandlerKeywordType extends EntryHandlerKeyword {
predicate, type, reverse);
}
}

// Collect type-scoped contexts if they exist
let scopedContext: Promise<IJsonLdContextNormalized> = Promise.resolve(context);
let hasTypedScopedContext = false;
for (const element of elements.sort()) { // Spec requires lexicographical ordering
const typeContext = Util.getContextValue(context, '@context', element, null);
if (typeContext) {
hasTypedScopedContext = true;
scopedContext = scopedContext.then((c) => parsingContext.parseContext(typeContext, c));
}
}
// If at least least one type-scoped context applies, set them in the tree.
if (hasTypedScopedContext) {
parsingContext.contextTree.setContext(keys.slice(0, keys.length - 1), scopedContext);
}
}

}
127 changes: 127 additions & 0 deletions test/JsonLdParser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8256,6 +8256,133 @@ describe('JsonLdParser', () => {

});

describe('type scoped contexts', () => {

it('should handle a single type and single property', async () => {
const stream = streamifyString(`
{
"@context": {
"@vocab": "http://vocab.org/",
"Foo": {
"@id": "http://ex.org/Foo",
"@context": {
"bar": "http://ex.org/bar"
}
}
},
"@id": "http://ex.org/myid",
"@type": "Foo",
"bar": "baz"
}`);
return expect(await arrayifyStream(stream.pipe(parser))).toBeRdfIsomorphic([
quad(namedNode('http://ex.org/myid'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
namedNode('http://ex.org/Foo')),
quad(namedNode('http://ex.org/myid'), namedNode('http://ex.org/bar'),
literal('baz')),
]);
});

it('should handle a two types and single property with property overriding', async () => {
const stream = streamifyString(`
{
"@context": {
"@vocab": "http://vocab.org/",
"Foo1": {
"@id": "http://ex.org/Foo1",
"@context": {
"bar": "http://ex.1.org/bar"
}
},
"Foo2": {
"@id": "http://ex.org/Foo2",
"@context": {
"bar": "http://ex.2.org/bar"
}
}
},
"@id": "http://ex.org/myid",
"@type": [ "Foo1", "Foo2" ],
"bar": "baz"
}`);
return expect(await arrayifyStream(stream.pipe(parser))).toBeRdfIsomorphic([
quad(namedNode('http://ex.org/myid'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
namedNode('http://ex.org/Foo1')),
quad(namedNode('http://ex.org/myid'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
namedNode('http://ex.org/Foo2')),
quad(namedNode('http://ex.org/myid'), namedNode('http://ex.2.org/bar'),
literal('baz')),
]);
});

it('should handle a two types and single property with property overriding in lexical order', async () => {
const stream = streamifyString(`
{
"@context": {
"@vocab": "http://vocab.org/",
"Foo1": {
"@id": "http://ex.org/Foo1",
"@context": {
"bar": "http://ex.1.org/bar"
}
},
"Foo2": {
"@id": "http://ex.org/Foo2",
"@context": {
"bar": "http://ex.2.org/bar"
}
}
},
"@id": "http://ex.org/myid",
"@type": [ "Foo2", "Foo1" ],
"bar": "baz"
}`);
return expect(await arrayifyStream(stream.pipe(parser))).toBeRdfIsomorphic([
quad(namedNode('http://ex.org/myid'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
namedNode('http://ex.org/Foo1')),
quad(namedNode('http://ex.org/myid'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
namedNode('http://ex.org/Foo2')),
quad(namedNode('http://ex.org/myid'), namedNode('http://ex.2.org/bar'),
literal('baz')),
]);
});

it('should handle a two types and two properties', async () => {
const stream = streamifyString(`
{
"@context": {
"@vocab": "http://vocab.org/",
"Foo1": {
"@id": "http://ex.org/Foo1",
"@context": {
"bar1": "http://ex.1.org/bar"
}
},
"Foo2": {
"@id": "http://ex.org/Foo2",
"@context": {
"bar2": "http://ex.2.org/bar"
}
}
},
"@id": "http://ex.org/myid",
"@type": [ "Foo1", "Foo2" ],
"bar1": "baz1",
"bar2": "baz2"
}`);
return expect(await arrayifyStream(stream.pipe(parser))).toBeRdfIsomorphic([
quad(namedNode('http://ex.org/myid'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
namedNode('http://ex.org/Foo1')),
quad(namedNode('http://ex.org/myid'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
namedNode('http://ex.org/Foo2')),
quad(namedNode('http://ex.org/myid'), namedNode('http://ex.1.org/bar'),
literal('baz1')),
quad(namedNode('http://ex.org/myid'), namedNode('http://ex.2.org/bar'),
literal('baz2')),
]);
});

});

});

// MARKER: Add tests for new features here, wrapped in new describe blocks.
Expand Down

0 comments on commit d2fce0e

Please sign in to comment.