Skip to content

Commit 35edf3b

Browse files
committed
* Indexing on @type requires @type to be either @id or @vocab,
and defaults to `@id`.
1 parent 706ec1b commit 35edf3b

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
@@ -544,6 +544,18 @@ api.compact = async ({
544544
compactedItem[typeKey] = types;
545545
break;
546546
}
547+
548+
// If compactedItem contains a single entry
549+
// whose key maps to @id, recompact without @type
550+
if(Object.keys(compactedItem).length === 1 && '@id' in expandedItem) {
551+
compactedItem = await api.compact({
552+
activeCtx,
553+
activeProperty: itemActiveProperty,
554+
element: {'@id': expandedItem['@id']},
555+
options,
556+
compactionMap
557+
});
558+
}
547559
}
548560

549561
// if compacting this value which has no key, index on @none
@@ -1051,7 +1063,7 @@ function _selectTerm(
10511063

10521064
// determine prefs for @id based on whether or not value compacts to a term
10531065
if((typeOrLanguageValue === '@id' || typeOrLanguageValue === '@reverse') &&
1054-
_isSubjectReference(value)) {
1066+
_isObject(value) && '@id' in value) {
10551067
// prefer @reverse first
10561068
if(typeOrLanguageValue === '@reverse') {
10571069
prefs.push('@reverse');

lib/context.js

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

tests/test-common.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ const TEST_TYPES = {
6363
/compact-manifest.jsonld#tin03$/,
6464
/compact-manifest.jsonld#tin04$/,
6565
/compact-manifest.jsonld#tin05$/,
66-
// index on @type
67-
/compact-manifest.jsonld#tm020$/,
68-
/compact-manifest.jsonld#tm021$/,
69-
/compact-manifest.jsonld#tm022$/,
7066
// context values
7167
/compact-manifest.jsonld#ts001$/,
7268
/compact-manifest.jsonld#ts002$/,
@@ -227,9 +223,6 @@ const TEST_TYPES = {
227223
/expand-manifest.jsonld#tin07$/,
228224
/expand-manifest.jsonld#tin08$/,
229225
/expand-manifest.jsonld#tin09$/,
230-
// index on @type
231-
/expand-manifest.jsonld#tm017$/,
232-
/expand-manifest.jsonld#tm020$/,
233226
// @nest
234227
/expand-manifest.jsonld#tn008$/,
235228
// keywords
@@ -502,9 +495,6 @@ const TEST_TYPES = {
502495
/toRdf-manifest.jsonld#tin04$/,
503496
/toRdf-manifest.jsonld#tin05$/,
504497
/toRdf-manifest.jsonld#tin06$/,
505-
// index on @type
506-
/toRdf-manifest.jsonld#tm017$/,
507-
/toRdf-manifest.jsonld#tm020$/,
508498
// @next
509499
/toRdf-manifest.jsonld#tn008$/,
510500
// keywords

0 commit comments

Comments
 (0)