Skip to content

Commit 873d702

Browse files
gkelloggdavidlehn
authored andcommitted
* Indexing on @type requires @type to be either @id or @vocab,
and defaults to `@id`.
1 parent 871b82f commit 873d702

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- Expanding the value of a graph container which is already a graph object
77
generates a recursive graph object.
88
- Compacting multiple nodes in a graph container places them in `@included`.
9+
- Indexing on "@type" requires "@type" to be either "@id" or "@vocab",
10+
and defaults to "@id".
911

1012
### Changed
1113
- Default processing mode changed to json-ld-1.1. Allows a 1.1 context to be

lib/compact.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,18 @@ api.compact = async ({
545545
compactedItem[typeKey] = types;
546546
break;
547547
}
548+
549+
// If compactedItem contains a single entry
550+
// whose key maps to @id, recompact without @type
551+
if(Object.keys(compactedItem).length === 1 && '@id' in expandedItem) {
552+
compactedItem = await api.compact({
553+
activeCtx,
554+
activeProperty: itemActiveProperty,
555+
element: {'@id': expandedItem['@id']},
556+
options,
557+
compactionMap
558+
});
559+
}
548560
}
549561

550562
// if compacting this value which has no key, index on @none
@@ -1063,7 +1075,7 @@ function _selectTerm(
10631075

10641076
// determine prefs for @id based on whether or not value compacts to a term
10651077
if((typeOrLanguageValue === '@id' || typeOrLanguageValue === '@reverse') &&
1066-
_isSubjectReference(value)) {
1078+
_isObject(value) && '@id' in value) {
10671079
// prefer @reverse first
10681080
if(typeOrLanguageValue === '@reverse') {
10691081
prefs.push('@reverse');

lib/context.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,20 @@ api.createTermDefinition = (
560560
// otherwise, container may also include @set
561561
isValid &= container.length <= (hasSet ? 2 : 1);
562562
}
563+
564+
if(container.includes('@type')) {
565+
// If mapping does not have an @type,
566+
// set it to @id
567+
mapping['@type'] = mapping['@type'] || '@id';
568+
569+
// type mapping must be either @id or @vocab
570+
if(!['@id', '@vocab'].includes(mapping['@type'])) {
571+
throw new JsonLdError(
572+
'Invalid JSON-LD syntax; container: @type requires @type to be @id or @vocab.',
573+
'jsonld.SyntaxError',
574+
{code: 'invalid type mapping', context: localCtx});
575+
}
576+
}
563577
} else {
564578
// in JSON-LD 1.0, container must not be an array (it must be a string,
565579
// which is one of the validContainers)

tests/test-common.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ const TEST_TYPES = {
5555
/compact-manifest.jsonld#tin03$/,
5656
/compact-manifest.jsonld#tin04$/,
5757
/compact-manifest.jsonld#tin05$/,
58-
// index on @type
59-
/compact-manifest.jsonld#tm020$/,
60-
/compact-manifest.jsonld#tm021$/,
61-
/compact-manifest.jsonld#tm022$/,
6258
// context values
6359
/compact-manifest.jsonld#ts001$/,
6460
/compact-manifest.jsonld#ts002$/,
@@ -203,9 +199,6 @@ const TEST_TYPES = {
203199
/expand-manifest.jsonld#tin07$/,
204200
/expand-manifest.jsonld#tin08$/,
205201
/expand-manifest.jsonld#tin09$/,
206-
// index on @type
207-
/expand-manifest.jsonld#tm017$/,
208-
/expand-manifest.jsonld#tm020$/,
209202
// @nest
210203
/expand-manifest.jsonld#tn008$/,
211204
// keywords
@@ -473,9 +466,6 @@ const TEST_TYPES = {
473466
/toRdf-manifest.jsonld#tin04$/,
474467
/toRdf-manifest.jsonld#tin05$/,
475468
/toRdf-manifest.jsonld#tin06$/,
476-
// index on @type
477-
/toRdf-manifest.jsonld#tm017$/,
478-
/toRdf-manifest.jsonld#tm020$/,
479469
// @next
480470
/toRdf-manifest.jsonld#tn008$/,
481471
// keywords

0 commit comments

Comments
 (0)