Skip to content

Commit 1f51f75

Browse files
tplookerdavidlehn
authored andcommitted
fix: add hook for typeExpansion, cover @base null
1 parent e7c6dc0 commit 1f51f75

File tree

3 files changed

+90
-7
lines changed

3 files changed

+90
-7
lines changed

lib/context.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,14 @@ function _expandIri(activeCtx, value, relativeTo, localCtx, defined, options) {
10411041
}
10421042
}
10431043

1044+
// A flag that captures whether the iri being expanded is
1045+
// the value for an @type
1046+
let typeExpansion = false;
1047+
1048+
if (options !== undefined && options.typeExpansion !== undefined) {
1049+
typeExpansion = options.typeExpansion;
1050+
}
1051+
10441052
if(relativeTo.vocab && '@vocab' in activeCtx) {
10451053
// prepend vocab
10461054
const prependedResult = activeCtx['@vocab'] + value;
@@ -1056,7 +1064,8 @@ function _expandIri(activeCtx, value, relativeTo, localCtx, defined, options) {
10561064
type: '@vocab',
10571065
vocab: activeCtx['@vocab'],
10581066
value,
1059-
result: prependedResult
1067+
result: prependedResult,
1068+
typeExpansion,
10601069
},
10611070
activeCtx,
10621071
options
@@ -1078,6 +1087,9 @@ function _expandIri(activeCtx, value, relativeTo, localCtx, defined, options) {
10781087
if(activeCtx['@base']) {
10791088
base = prependBase(options.base, activeCtx['@base']);
10801089
prependedResult = prependBase(base, value);
1090+
} else {
1091+
base = activeCtx['@base'];
1092+
prependedResult = value;
10811093
}
10821094
} else {
10831095
base = options.base;
@@ -1094,15 +1106,16 @@ function _expandIri(activeCtx, value, relativeTo, localCtx, defined, options) {
10941106
type: '@base',
10951107
base,
10961108
value,
1097-
result: prependedResult
1109+
result: prependedResult,
1110+
typeExpansion,
10981111
},
10991112
activeCtx,
11001113
options
11011114
});
11021115
}
11031116
if(expansionMapResult !== undefined) {
11041117
value = expansionMapResult;
1105-
} else if(prependedResult !== undefined) {
1118+
} else {
11061119
// the null case preserves value as potentially relative
11071120
value = prependedResult;
11081121
}
@@ -1118,6 +1131,7 @@ function _expandIri(activeCtx, value, relativeTo, localCtx, defined, options) {
11181131
const expandedResult = options.expansionMap({
11191132
relativeIri: value,
11201133
activeCtx,
1134+
typeExpansion,
11211135
options
11221136
});
11231137
if(expandedResult !== undefined) {

lib/expand.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ async function _expandObject({
431431
const isJsonType = element[typeKey] &&
432432
_expandIri(activeCtx,
433433
(_isArray(element[typeKey]) ? element[typeKey][0] : element[typeKey]),
434-
{vocab: true}, options) === '@json';
434+
{vocab: true}, { ...options, typeExpansion: true }) === '@json';
435435

436436
for(const key of keys) {
437437
let value = element[key];
@@ -527,7 +527,7 @@ async function _expandObject({
527527
value = Object.fromEntries(Object.entries(value).map(([k, v]) => [
528528
_expandIri(typeScopedContext, k, {vocab: true}),
529529
_asArray(v).map(vv =>
530-
_expandIri(typeScopedContext, vv, {base: true, vocab: true})
530+
_expandIri(typeScopedContext, vv, {base: true, vocab: true}, { ...options, typeExpansion: true })
531531
)
532532
]));
533533
}
@@ -537,7 +537,7 @@ async function _expandObject({
537537
_asArray(value).map(v =>
538538
_isString(v) ?
539539
_expandIri(typeScopedContext, v,
540-
{base: true, vocab: true}, options) : v),
540+
{base: true, vocab: true}, { ...options, typeExpansion: true }) : v),
541541
{propertyIsArray: options.isFrame});
542542
continue;
543543
}
@@ -937,7 +937,7 @@ function _expandValue({activeCtx, activeProperty, value, options}) {
937937
if(expandedProperty === '@id') {
938938
return _expandIri(activeCtx, value, {base: true}, options);
939939
} else if(expandedProperty === '@type') {
940-
return _expandIri(activeCtx, value, {vocab: true, base: true}, options);
940+
return _expandIri(activeCtx, value, {vocab: true, base: true}, { ...options, typeExpansion: true });
941941
}
942942

943943
// get type definition from context

tests/misc.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,36 @@ describe('expansionMap', () => {
614614
assert.equal(expansionMapCalled, true);
615615
});
616616

617+
it('should be called on relative iri for type term in scoped context', async () => {
618+
const docWithRelativeIriId = {
619+
'@context': {
620+
'definedType': {
621+
'@id': 'https://example.com#definedType',
622+
'@context': {
623+
'definedTerm': 'https://example.com#definedTerm'
624+
625+
}
626+
}
627+
},
628+
'id': "urn:absoluteiri",
629+
'@type': "definedType",
630+
definedTerm: {
631+
'@type': 'relativeiri'
632+
}
633+
};
634+
635+
let expansionMapCalled = false;
636+
const expansionMap = info => {
637+
if(info.relativeIri === 'relativeiri') {
638+
expansionMapCalled = true;
639+
}
640+
};
641+
642+
await jsonld.expand(docWithRelativeIriId, {expansionMap});
643+
644+
assert.equal(expansionMapCalled, true);
645+
});
646+
617647
it('should be called on relative iri for \
618648
type term with multiple relative iri types', async () => {
619649
const docWithRelativeIriId = {
@@ -638,6 +668,38 @@ describe('expansionMap', () => {
638668
assert.equal(expansionMapCalledTimes, 3);
639669
});
640670

671+
it('should be called on relative iri for \
672+
type term with multiple relative iri types in scoped context', async () => {
673+
const docWithRelativeIriId = {
674+
'@context': {
675+
'definedType': {
676+
'@id': 'https://example.com#definedType',
677+
'@context': {
678+
'definedTerm': 'https://example.com#definedTerm'
679+
680+
}
681+
}
682+
},
683+
'id': "urn:absoluteiri",
684+
'@type': "definedType",
685+
definedTerm: {
686+
'@type': ["relativeiri", "anotherRelativeiri" ]
687+
}
688+
};
689+
690+
let expansionMapCalledTimes = 0;
691+
const expansionMap = info => {
692+
if(info.relativeIri === 'relativeiri' ||
693+
info.relativeIri === 'anotherRelativeiri') {
694+
expansionMapCalledTimes++;
695+
}
696+
};
697+
698+
await jsonld.expand(docWithRelativeIriId, {expansionMap});
699+
700+
assert.equal(expansionMapCalledTimes, 3);
701+
});
702+
641703
it('should be called on relative iri for \
642704
type term with multiple types', async () => {
643705
const docWithRelativeIriId = {
@@ -764,6 +826,7 @@ describe('expansionMap', () => {
764826
type: '@vocab',
765827
vocab: 'http://example.com/',
766828
value: 'term',
829+
typeExpansion: false,
767830
result: 'http://example.com/term'
768831
});
769832
expansionMapCalled = true;
@@ -789,6 +852,7 @@ describe('expansionMap', () => {
789852
type: '@vocab',
790853
vocab: 'http://example.com/',
791854
value: 'relativeIri',
855+
typeExpansion: true,
792856
result: 'http://example.com/relativeIri'
793857
});
794858
expansionMapCalled = true;
@@ -815,6 +879,7 @@ describe('expansionMap', () => {
815879
type: '@vocab',
816880
vocab: 'http://example.com/',
817881
value: 'relativeIri',
882+
typeExpansion: true,
818883
result: 'http://example.com/relativeIri'
819884
});
820885
expansionMapCalled = true;
@@ -841,6 +906,7 @@ describe('expansionMap', () => {
841906
type: '@base',
842907
base: 'http://example.com/',
843908
value: 'relativeIri',
909+
typeExpansion: false,
844910
result: 'http://example.com/relativeIri'
845911
});
846912
expansionMapCalled = true;
@@ -869,6 +935,7 @@ describe('expansionMap', () => {
869935
type: '@base',
870936
base: 'http://example.com/',
871937
value: 'relativeIri',
938+
typeExpansion: false,
872939
result: 'http://example.com/relativeIri'
873940
});
874941
expansionMapCalled = true;
@@ -896,6 +963,7 @@ describe('expansionMap', () => {
896963
type: '@base',
897964
base: 'http://example.com/',
898965
value: 'relativeIri',
966+
typeExpansion: true,
899967
result: 'http://example.com/relativeIri'
900968
});
901969
expansionMapCalled = true;
@@ -924,6 +992,7 @@ describe('expansionMap', () => {
924992
type: '@base',
925993
base: 'http://example.com/',
926994
value: 'relativeIri',
995+
typeExpansion: true,
927996
result: 'http://example.com/relativeIri'
928997
});
929998
expansionMapCalled = true;

0 commit comments

Comments
 (0)