Skip to content

Commit

Permalink
Allow context nullification in scoped ctx to clear protections
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Mar 16, 2020
1 parent 922e301 commit 20c6834
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
10 changes: 7 additions & 3 deletions lib/ParsingContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,19 @@ export class ParsingContext {
|| scopedContext[key]['@context']['@propagate']; // Propagation is true by default

if (propagate !== false || i === keysOriginal.length - 1 - offset) {
if (propagate !== false) {
this.contextTree.setContext(keysOriginal.slice(0, i + offset), Promise.resolve(scopedContext));
}
context = scopedContext;

// Clean up final context
delete context['@propagate'];
context[key] = { ...context[key] };
if ('@id' in contextKeyEntry) {
context[key]['@id'] = contextKeyEntry['@id'];
}
delete context[key]['@context'];

if (propagate !== false) {
this.contextTree.setContext(keysOriginal.slice(0, i + offset), Promise.resolve(context));
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ export class Util {

// Handle local context in the value
if ('@context' in value) {
context = await this.parsingContext.parseContext(value['@context'], context);
context = await this.parsingContext.parseContext(value['@context'],
await this.parsingContext.getContext(keys, 0));
}

// In all other cases, we have a hash
Expand Down
30 changes: 28 additions & 2 deletions test/JsonLdParser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9370,7 +9370,7 @@ describe('JsonLdParser', () => {
ERROR_CODES.PROTECTED_TERM_REDIFINITION));
});

it('should not error on protected term overrides in before a property scoped-context', async () => {
it('should not error on protected term overrides before a property scoped-context', async () => {
const stream = streamifyString(`
{
"@context": {
Expand All @@ -9396,7 +9396,7 @@ describe('JsonLdParser', () => {
]);
});

it('should error on protected term overrides in after a property scoped-context', async () => {
it('should error on protected term overrides after a property scoped-context', async () => {
const stream = streamifyString(`
{
"@context": {
Expand All @@ -9420,6 +9420,32 @@ describe('JsonLdParser', () => {
ERROR_CODES.PROTECTED_TERM_REDIFINITION));
});

it('should not error on protected term, context null in a property scoped-context, and override', async () => {
const stream = streamifyString(`
{
"@context": {
"@protected": true,
"foo": "http://ex.org/foo",
"scope": {
"@id": "http://ex.org/scope",
"@context": null
}
},
"scope": {
"@context": {
"foo": "http://ex.2.org/foo"
},
"foo": "bar"
}
}`);
return expect(await arrayifyStream(stream.pipe(parser))).toBeRdfIsomorphic([
quad(blankNode(''), namedNode('http://ex.org/scope'),
blankNode('b1')),
quad(blankNode('b1'), namedNode('http://ex.2.org/foo'),
literal('bar')),
]);
});

});

// MARKER: Add tests for new features here, wrapped in new describe blocks.
Expand Down
1 change: 1 addition & 0 deletions test/Util-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ describe('Util', () => {

it('with a relative @id and empty local @context with @base in parent context', async () => {
context = { '@base': 'http://ex.org/' };
util.parsingContext.contextTree.setContext([], Promise.resolve({ '@base': 'http://ex.org/' }));
return expect(await util.valueToTerm(context, 'key', { '@context': {}, '@id': 'abc' }, 0, []))
.toEqualRdfTermArray([namedNode('http://ex.org/abc')]);
});
Expand Down

0 comments on commit 20c6834

Please sign in to comment.