Skip to content

Commit 8101388

Browse files
committed
Fix graph property empty array and safe mode.
- Fix handling of graph property with empty array. - Fix safe mode for `@graph` use cases. - Check all elements of graph property with array.
1 parent 1f34863 commit 8101388

File tree

3 files changed

+229
-10
lines changed

3 files changed

+229
-10
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# jsonld ChangeLog
22

3+
## 8.2.1 - 2023-xx-xx
4+
5+
### Fixed
6+
- Fix handling of graph property with empty array.
7+
- Fix safe mode for `@graph` use cases.
8+
- Check all elements of graph property with array.
9+
310
## 8.2.0 - 2023-05-19
411

512
### Changed

lib/expand.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,12 @@ api.expand = async ({
386386

387387
/**
388388
* Drop empty object, top-level @value/@list, or object with only @id
389+
*
390+
* @param value Value to check.
391+
* @param count Number of properties in object.
392+
* @param options The expansion options.
393+
*
394+
* @return null if dropped, value otherwise.
389395
*/
390396
function _dropUnsafeObject({
391397
value,
@@ -948,15 +954,17 @@ async function _expandObject({
948954
// index cases handled above
949955
if(container.includes('@graph') &&
950956
!container.some(key => key === '@id' || key === '@index')) {
951-
// ensure expanded values are arrays
952-
// ensure an array
957+
// ensure expanded values are in an array
953958
expandedValue = _asArray(expandedValue);
954-
// check if needs to be dropped
955-
const count = Object.keys(expandedValue[0]).length;
956-
if(!options.isFrame && _dropUnsafeObject({
957-
value: expandedValue[0], count, options
958-
}) === null) {
959-
// skip adding and continue
959+
if(!options.isFrame) {
960+
// drop items if needed
961+
expandedValue = expandedValue.filter(v => {
962+
const count = Object.keys(v).length;
963+
return _dropUnsafeObject({value: v, count, options}) !== null;
964+
});
965+
}
966+
if(expandedValue.length === 0) {
967+
// all items dropped, skip adding and continue
960968
continue;
961969
}
962970
// convert to graph

tests/misc.js

Lines changed: 206 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,72 @@ _:b0 <ex:p> "[null]"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON> .
18851885
});
18861886
});
18871887

1888+
it('should emit for @graph with empty array (1)', async () => {
1889+
const input =
1890+
{
1891+
"@context": {
1892+
"p": {
1893+
"@id": "urn:p",
1894+
"@type": "@id",
1895+
"@container": "@graph"
1896+
}
1897+
},
1898+
"@id": "urn:id",
1899+
"p": []
1900+
}
1901+
;
1902+
const expected = [];
1903+
1904+
await _test({
1905+
type: 'expand',
1906+
input,
1907+
expected,
1908+
eventCodeLog: [
1909+
'object with only @id'
1910+
],
1911+
testNotSafe: true
1912+
});
1913+
});
1914+
1915+
it('should not emit for @graph with empty array (2)', async () => {
1916+
const input =
1917+
{
1918+
"@context": {
1919+
"p": {
1920+
"@id": "urn:p",
1921+
"@type": "@id",
1922+
"@container": "@graph"
1923+
},
1924+
"urn:t": {
1925+
"@type": "@id"
1926+
}
1927+
},
1928+
"@id": "urn:id",
1929+
"urn:t": "urn:id",
1930+
"p": []
1931+
}
1932+
;
1933+
const expected =
1934+
[
1935+
{
1936+
"@id": "urn:id",
1937+
"urn:t": [
1938+
{
1939+
"@id": "urn:id"
1940+
}
1941+
]
1942+
}
1943+
]
1944+
;
1945+
1946+
await _test({
1947+
type: 'expand',
1948+
input,
1949+
expected,
1950+
eventCodeLog: []
1951+
});
1952+
});
1953+
18881954
it('should emit for @graph with relative @id (1)', async () => {
18891955
const input =
18901956
{
@@ -1915,6 +1981,144 @@ _:b0 <ex:p> "[null]"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON> .
19151981

19161982
it('should emit for @graph with relative @id (2)', async () => {
19171983
const input =
1984+
{
1985+
"@context": {
1986+
"p": {
1987+
"@id": "urn:p",
1988+
"@type": "@id",
1989+
"@container": "@graph"
1990+
}
1991+
},
1992+
"@id": "urn:id",
1993+
"p": [
1994+
"rel0",
1995+
"rel1"
1996+
]
1997+
}
1998+
;
1999+
const expected = [];
2000+
2001+
await _test({
2002+
type: 'expand',
2003+
input,
2004+
expected,
2005+
eventCodeLog: [
2006+
'object with only @id',
2007+
'object with only @id',
2008+
'object with only @id'
2009+
],
2010+
testNotSafe: true
2011+
});
2012+
});
2013+
2014+
it('should emit for @graph with relative @id (3)', async () => {
2015+
const input =
2016+
{
2017+
"@context": {
2018+
"p": {
2019+
"@id": "urn:p",
2020+
"@type": "@id",
2021+
"@container": "@graph"
2022+
}
2023+
},
2024+
"@id": "urn:g0",
2025+
"p": [
2026+
{
2027+
"@id": "urn:g1",
2028+
"urn:p1": "v1"
2029+
},
2030+
"rel"
2031+
]
2032+
}
2033+
;
2034+
const expected =
2035+
[
2036+
{
2037+
"@id": "urn:g0",
2038+
"urn:p": [
2039+
{
2040+
"@graph": [
2041+
{
2042+
"@id": "urn:g1",
2043+
"urn:p1": [
2044+
{
2045+
"@value": "v1"
2046+
}
2047+
]
2048+
}
2049+
]
2050+
}
2051+
]
2052+
}
2053+
]
2054+
;
2055+
2056+
await _test({
2057+
type: 'expand',
2058+
input,
2059+
expected,
2060+
eventCodeLog: [
2061+
'object with only @id'
2062+
],
2063+
testNotSafe: true
2064+
});
2065+
});
2066+
2067+
it('should emit for @graph with relative @id (4)', async () => {
2068+
const input =
2069+
{
2070+
"@context": {
2071+
"p": {
2072+
"@id": "urn:p",
2073+
"@type": "@id",
2074+
"@container": "@graph"
2075+
}
2076+
},
2077+
"@id": "urn:g0",
2078+
"p": [
2079+
"rel",
2080+
{
2081+
"@id": "urn:g1",
2082+
"urn:p1": "v1"
2083+
}
2084+
]
2085+
}
2086+
;
2087+
const expected =
2088+
[
2089+
{
2090+
"@id": "urn:g0",
2091+
"urn:p": [
2092+
{
2093+
"@graph": [
2094+
{
2095+
"@id": "urn:g1",
2096+
"urn:p1": [
2097+
{
2098+
"@value": "v1"
2099+
}
2100+
]
2101+
}
2102+
]
2103+
}
2104+
]
2105+
}
2106+
]
2107+
;
2108+
2109+
await _test({
2110+
type: 'expand',
2111+
input,
2112+
expected,
2113+
eventCodeLog: [
2114+
'object with only @id'
2115+
],
2116+
testNotSafe: true
2117+
});
2118+
});
2119+
2120+
it('should emit for @graph with relative @id (5)', async () => {
2121+
const input =
19182122
{
19192123
"@context": {
19202124
"p": {
@@ -1955,7 +2159,7 @@ _:b0 <ex:p> "[null]"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON> .
19552159
});
19562160
});
19572161

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

2000-
it('should emit for @graph with relative @id (4)', async () => {
2204+
it('should emit for @graph with relative @id (7)', async () => {
20012205
const input =
20022206
{
20032207
"@context": {

0 commit comments

Comments
 (0)