Skip to content

Commit

Permalink
Fix graph property empty array and safe mode.
Browse files Browse the repository at this point in the history
- Fix handling of graph property with empty array.
- Fix safe mode for `@graph` use cases.
  - Check all elements of graph property with array.
  • Loading branch information
davidlehn committed Aug 31, 2023
1 parent 1f34863 commit 8101388
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# jsonld ChangeLog

## 8.2.1 - 2023-xx-xx

### Fixed
- Fix handling of graph property with empty array.
- Fix safe mode for `@graph` use cases.
- Check all elements of graph property with array.

## 8.2.0 - 2023-05-19

### Changed
Expand Down
24 changes: 16 additions & 8 deletions lib/expand.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,12 @@ api.expand = async ({

/**
* Drop empty object, top-level @value/@list, or object with only @id
*
* @param value Value to check.
* @param count Number of properties in object.
* @param options The expansion options.
*
* @return null if dropped, value otherwise.
*/
function _dropUnsafeObject({
value,
Expand Down Expand Up @@ -948,15 +954,17 @@ async function _expandObject({
// index cases handled above
if(container.includes('@graph') &&
!container.some(key => key === '@id' || key === '@index')) {
// ensure expanded values are arrays
// ensure an array
// ensure expanded values are in an array
expandedValue = _asArray(expandedValue);
// check if needs to be dropped
const count = Object.keys(expandedValue[0]).length;
if(!options.isFrame && _dropUnsafeObject({
value: expandedValue[0], count, options
}) === null) {
// skip adding and continue
if(!options.isFrame) {
// drop items if needed
expandedValue = expandedValue.filter(v => {
const count = Object.keys(v).length;
return _dropUnsafeObject({value: v, count, options}) !== null;
});
}
if(expandedValue.length === 0) {
// all items dropped, skip adding and continue
continue;
}
// convert to graph
Expand Down
208 changes: 206 additions & 2 deletions tests/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1885,6 +1885,72 @@ _:b0 <ex:p> "[null]"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON> .
});
});

it('should emit for @graph with empty array (1)', async () => {
const input =
{
"@context": {
"p": {
"@id": "urn:p",
"@type": "@id",
"@container": "@graph"
}
},
"@id": "urn:id",
"p": []
}
;
const expected = [];

await _test({
type: 'expand',
input,
expected,
eventCodeLog: [
'object with only @id'
],
testNotSafe: true
});
});

it('should not emit for @graph with empty array (2)', async () => {
const input =
{
"@context": {
"p": {
"@id": "urn:p",
"@type": "@id",
"@container": "@graph"
},
"urn:t": {
"@type": "@id"
}
},
"@id": "urn:id",
"urn:t": "urn:id",
"p": []
}
;
const expected =
[
{
"@id": "urn:id",
"urn:t": [
{
"@id": "urn:id"
}
]
}
]
;

await _test({
type: 'expand',
input,
expected,
eventCodeLog: []
});
});

it('should emit for @graph with relative @id (1)', async () => {
const input =
{
Expand Down Expand Up @@ -1915,6 +1981,144 @@ _:b0 <ex:p> "[null]"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON> .

it('should emit for @graph with relative @id (2)', async () => {
const input =
{
"@context": {
"p": {
"@id": "urn:p",
"@type": "@id",
"@container": "@graph"
}
},
"@id": "urn:id",
"p": [
"rel0",
"rel1"
]
}
;
const expected = [];

await _test({
type: 'expand',
input,
expected,
eventCodeLog: [
'object with only @id',
'object with only @id',
'object with only @id'
],
testNotSafe: true
});
});

it('should emit for @graph with relative @id (3)', async () => {
const input =
{
"@context": {
"p": {
"@id": "urn:p",
"@type": "@id",
"@container": "@graph"
}
},
"@id": "urn:g0",
"p": [
{
"@id": "urn:g1",
"urn:p1": "v1"
},
"rel"
]
}
;
const expected =
[
{
"@id": "urn:g0",
"urn:p": [
{
"@graph": [
{
"@id": "urn:g1",
"urn:p1": [
{
"@value": "v1"
}
]
}
]
}
]
}
]
;

await _test({
type: 'expand',
input,
expected,
eventCodeLog: [
'object with only @id'
],
testNotSafe: true
});
});

it('should emit for @graph with relative @id (4)', async () => {
const input =
{
"@context": {
"p": {
"@id": "urn:p",
"@type": "@id",
"@container": "@graph"
}
},
"@id": "urn:g0",
"p": [
"rel",
{
"@id": "urn:g1",
"urn:p1": "v1"
}
]
}
;
const expected =
[
{
"@id": "urn:g0",
"urn:p": [
{
"@graph": [
{
"@id": "urn:g1",
"urn:p1": [
{
"@value": "v1"
}
]
}
]
}
]
}
]
;

await _test({
type: 'expand',
input,
expected,
eventCodeLog: [
'object with only @id'
],
testNotSafe: true
});
});

it('should emit for @graph with relative @id (5)', async () => {
const input =
{
"@context": {
"p": {
Expand Down Expand Up @@ -1955,7 +2159,7 @@ _:b0 <ex:p> "[null]"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON> .
});
});

it('should emit for @graph with relative @id (3)', async () => {
it('should emit for @graph with relative @id (6)', async () => {
const input =
{
"@context": {
Expand Down Expand Up @@ -1997,7 +2201,7 @@ _:b0 <ex:p> "[null]"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON> .
});
});

it('should emit for @graph with relative @id (4)', async () => {
it('should emit for @graph with relative @id (7)', async () => {
const input =
{
"@context": {
Expand Down

0 comments on commit 8101388

Please sign in to comment.