Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/ContextResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ module.exports = class ContextResolver {
}

async resolve({
activeCtx, context, documentLoader, base, cycles = new Set()
activeCtx, context, documentLoader, base, cycles = new Set(),
isRemote = false
}) {
// process `@context`
if(context && _isObject(context) && context['@context']) {
// process `@context`; only a dereferenced remote document is unwrapped
// (JSON-LD 1.1 API 4.1.2 step 5.2.5). An inline context map that carries
// an `@context` entry is a context definition: its `@context` key
// reaches Create Term Definition (step 5.13 -> 4.2.2 step 5) and is
// refused as a keyword redefinition, not silently unwrapped.
if(isRemote && context && _isObject(context) &&
context['@context']) {
context = context['@context'];
}

Expand Down Expand Up @@ -123,7 +129,7 @@ module.exports = class ContextResolver {

// resolve, cache, and return context
const resolved = await this.resolve(
{activeCtx, context, documentLoader, base, cycles});
{activeCtx, context, documentLoader, base, cycles, isRemote: true});
this._cacheResolvedContext({key: url, resolved, tag: remoteDoc.tag});
return resolved;
}
Expand Down
10 changes: 6 additions & 4 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,12 @@ api.process = async ({
continue;
}

// dereference @context key if present
if(_isObject(ctx) && '@context' in ctx) {
ctx = ctx['@context'];
}
// Note: no `@context` dereference happens here. Only a dereferenced
// remote document is unwrapped (JSON-LD 1.1 API 4.1.2 step 5.2.5), and
// ContextResolver does that for remote contexts; an inline context map
// that carries an `@context` entry is a context definition, whose
// `@context` key reaches Create Term Definition (step 5.13 -> 4.2.2
// step 5) and is refused as a keyword redefinition instead.

// context must be an object by now, all URLs retrieved before this call
if(!_isObject(ctx)) {
Expand Down
65 changes: 65 additions & 0 deletions tests/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,71 @@ const assert = require('assert');

// TODO: need more tests for jsonld.link and jsonld.merge

describe('inline context `@context` keyword redefinition (issue #588)', () => {
const REMOTE = 'https://example.org/remote-context.jsonld';
const held = {
[REMOTE]: {'@context': {name: 'http://example.org/name'}}
};
const documentLoader = async url => {
if(!(url in held)) {
throw new Error(`not held: ${url}`);
}
return {contextUrl: null, documentUrl: url, document: held[url]};
};

it('refuses an inline context map that carries an \'@context\' entry', async () => {
const doc = {
'@context': {'@context': {name: 'http://example.org/name'}},
name: 'x'
};
let error;
try {
await jsonld.expand(doc, {documentLoader});
} catch(e) {
error = e;
}
assert(error, 'expected a keyword redefinition error');
assert.equal(error.details.code, 'keyword redefinition');
});

it('refuses the same map as an element of a context array', async () => {
const doc = {
'@context': [{'@context': {name: 'http://example.org/name'}}],
name: 'x'
};
let error;
try {
await jsonld.expand(doc, {documentLoader});
} catch(e) {
error = e;
}
assert(error, 'expected a keyword redefinition error');
assert.equal(error.details.code, 'keyword redefinition');
});

it('still refuses other keywords as terms of an inline context', async () => {
const doc = {
'@context': {'@id': 'http://example.org/id'},
name: 'x'
};
let error;
try {
await jsonld.expand(doc, {documentLoader});
} catch(e) {
error = e;
}
assert(error, 'expected a keyword redefinition error');
assert.equal(error.details.code, 'keyword redefinition');
});

it('still unwraps a dereferenced remote context (control)', async () => {
const doc = {'@context': REMOTE, name: 'x'};
const expanded = await jsonld.expand(doc, {documentLoader});
assert.deepEqual(expanded,
[{'http://example.org/name': [{'@value': 'x'}]}]);
});
});

describe('link tests', () => {
const doc = {
"@id": "ex:1",
Expand Down