Skip to content

Commit d9e45cb

Browse files
committed
* Indexing on @type requires @type to be either @id or @vocab,
and defaults to `@id`.
1 parent b3da66a commit d9e45cb

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
@@ -1062,7 +1074,7 @@ function _selectTerm(
10621074

10631075
// determine prefs for @id based on whether or not value compacts to a term
10641076
if((typeOrLanguageValue === '@id' || typeOrLanguageValue === '@reverse') &&
1065-
_isSubjectReference(value)) {
1077+
_isObject(value) && '@id' in value) {
10661078
// prefer @reverse first
10671079
if(typeOrLanguageValue === '@reverse') {
10681080
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)