Skip to content

Commit 2f2ea3c

Browse files
tplookerdavidlehn
authored andcommitted
fix: add support for when @vocab results in relative iri
1 parent a525dd2 commit 2f2ea3c

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

lib/context.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,11 +1043,11 @@ function _expandIri(activeCtx, value, relativeTo, localCtx, defined, options) {
10431043

10441044
// prepend vocab
10451045
if(relativeTo.vocab && '@vocab' in activeCtx) {
1046-
return activeCtx['@vocab'] + value;
1046+
value = activeCtx['@vocab'] + value;
10471047
}
10481048

10491049
// prepend base
1050-
if(relativeTo.base && '@base' in activeCtx) {
1050+
else if(relativeTo.base && '@base' in activeCtx) {
10511051
if(activeCtx['@base']) {
10521052
// The null case preserves value as potentially relative
10531053
value = prependBase(prependBase(options.base, activeCtx['@base']), value);
@@ -1061,6 +1061,7 @@ function _expandIri(activeCtx, value, relativeTo, localCtx, defined, options) {
10611061
const expandedResult = options.expansionMap({
10621062
relativeIri: value,
10631063
activeCtx,
1064+
options
10641065
});
10651066
if(expandedResult !== undefined) {
10661067
value = expandedResult;

lib/expand.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ api.expand = async ({
7575
typeScopedContext = null,
7676
expansionMap = () => undefined
7777
}) => {
78+
79+
// Add expansion map to the processing options
80+
options = { ...options, expansionMap };
81+
7882
// nothing to expand
7983
if(element === null || element === undefined) {
8084
return null;

tests/misc.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,12 +686,52 @@ describe('expansionMap', () => {
686686
'@context': {
687687
"@base": "./",
688688
},
689-
'@id': "absoluteiri",
689+
'@id': "relativeiri",
690+
};
691+
692+
let expansionMapCalled = false;
693+
const expansionMap = info => {
694+
if(info.relativeIri === '/relativeiri') {
695+
expansionMapCalled = true;
696+
}
697+
};
698+
699+
await jsonld.expand(docWithRelativeIriId, {expansionMap});
700+
701+
assert.equal(expansionMapCalled, true);
702+
});
703+
704+
it("should be called on relative iri when @base value is './'", async () => {
705+
const docWithRelativeIriId = {
706+
'@context': {
707+
"@base": "./",
708+
},
709+
'@id': "relativeiri",
710+
};
711+
712+
let expansionMapCalled = false;
713+
const expansionMap = info => {
714+
if(info.relativeIri === '/relativeiri') {
715+
expansionMapCalled = true;
716+
}
717+
};
718+
719+
await jsonld.expand(docWithRelativeIriId, {expansionMap});
720+
721+
assert.equal(expansionMapCalled, true);
722+
});
723+
724+
it("should be called on relative iri when @vocab value is './'", async () => {
725+
const docWithRelativeIriId = {
726+
'@context': {
727+
"@vocab": "./",
728+
},
729+
'@type': "relativeiri",
690730
};
691731

692732
let expansionMapCalled = false;
693733
const expansionMap = info => {
694-
if(info.relativeIri === '/absoluteiri') {
734+
if(info.relativeIri === '/relativeiri') {
695735
expansionMapCalled = true;
696736
}
697737
};

0 commit comments

Comments
 (0)