Skip to content

Commit

Permalink
Fix missing bnode generation for empty nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Oct 14, 2020
1 parent 830da21 commit dd8022e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,8 @@ export class Util {
}
} else {
// Only make a blank node if at least one triple was emitted at the value's level.
if (this.parsingContext.emittedStack[depth + 1]) {
if (this.parsingContext.emittedStack[depth + 1]
|| (value && typeof value === 'object' && Object.keys(value).length === 0)) {
return (this.parsingContext.idStack[depth + 1]
|| (this.parsingContext.idStack[depth + 1] = [ this.dataFactory.blankNode() ]));
} else {
Expand Down
10 changes: 10 additions & 0 deletions test/JsonLdParser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,16 @@ describe('JsonLdParser', () => {
]);
});

it('without @id and anonymous blank node value', async () => {
const stream = streamifyString(`
{
"http://ex.org/pred1": {}
}`);
return expect(await arrayifyStream(stream.pipe(parser))).toBeRdfIsomorphic([
DF.quad(DF.blankNode(), DF.namedNode('http://ex.org/pred1'), DF.blankNode()),
]);
});

it('with @id', async () => {
const stream = streamifyString(`
{
Expand Down
4 changes: 2 additions & 2 deletions test/Util-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ describe('Util', () => {
});

describe('for an object', () => {
it('without an @id should return []', async () => {
it('without an @id should return a bnode', async () => {
return expect(await util.valueToTerm(context, 'key', {}, 0, []))
.toEqual([]);
.toEqualRdfTermArray([DF.blankNode()]);
});

it('without an @id should return a blank node when a value was emitted at a deeper depth', async () => {
Expand Down

0 comments on commit dd8022e

Please sign in to comment.