Skip to content

Commit 10d7e48

Browse files
committed
Support @JSON ranges in arrays
1 parent 623057e commit 10d7e48

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

lib/serialize/ContextConstructor.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class ContextConstructor {
7272
const shortcut = parameter['@id'].slice(Math.max(0, component['@id'].length + 1));
7373
typeScopedContext[shortcut] = {
7474
'@id': parameter['@id'],
75-
...parameter.range === 'rdf:JSON' ? { '@type': '@json' } : {},
75+
...ContextConstructor.isParameterRangeJson(parameter.range) ? { '@type': '@json' } : {},
7676
// Mark as list container if range is array
7777
...ContextConstructor.isParameterRangeList(parameter.range) ?
7878
{ '@container': '@list' } :
@@ -133,6 +133,14 @@ export class ContextConstructor {
133133
public static isParameterRangeUndefined(range: ParameterDefinitionRange): boolean {
134134
return typeof range !== 'string' && '@type' in range && range['@type'] === 'ParameterRangeUndefined';
135135
}
136+
137+
public static isParameterRangeJson(range: ParameterDefinitionRange | undefined): boolean {
138+
if (range && typeof range !== 'string' && '@type' in range &&
139+
range['@type'] === 'ParameterRangeArray' && this.isParameterRangeJson(range.parameterRangeValue)) {
140+
return true;
141+
}
142+
return range === 'rdf:JSON';
143+
}
136144
}
137145

138146
export interface ContextConstructorArgs {

test/serialize/ContextConstructor.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,76 @@ describe('ContextConstructor', () => {
612612
});
613613
});
614614

615+
it('should handle non-empty component definitions for JSON ranges in arrays', () => {
616+
expect(ctor.constructComponentShortcuts({
617+
'/docs/package/components/file1': {
618+
'@context': [
619+
'https://linkedsoftwaredependencies.org/bundles/npm/my-package/context.jsonld',
620+
],
621+
'@id': 'npmd:my-package',
622+
components: [
623+
{
624+
'@id': 'mp:file1#MyClass1',
625+
'@type': 'Class',
626+
constructorArguments: [],
627+
parameters: [
628+
{
629+
'@id': 'mp:file1#MyClass1_param1',
630+
range: {
631+
'@type': 'ParameterRangeArray',
632+
parameterRangeValue: 'rdf:JSON',
633+
},
634+
},
635+
{
636+
'@id': 'mp:file1#MyClass1_param2',
637+
range: 'rdf:JSON',
638+
},
639+
],
640+
memberFields: [],
641+
requireElement: 'MyClass1',
642+
},
643+
],
644+
},
645+
'/docs/package/components/b/file2': {
646+
'@context': [
647+
'https://linkedsoftwaredependencies.org/bundles/npm/my-package/context.jsonld',
648+
],
649+
'@id': 'npmd:my-package',
650+
components: [
651+
{
652+
'@id': 'mp:b/file2#MyClass2',
653+
'@type': 'Class',
654+
requireElement: 'MyClass2',
655+
constructorArguments: [],
656+
parameters: [],
657+
memberFields: [],
658+
},
659+
],
660+
},
661+
})).toEqual({
662+
MyClass1: {
663+
'@id': 'mp:file1#MyClass1',
664+
'@prefix': true,
665+
'@context': {
666+
param1: {
667+
'@id': 'mp:file1#MyClass1_param1',
668+
'@type': '@json',
669+
'@container': '@list',
670+
},
671+
param2: {
672+
'@id': 'mp:file1#MyClass1_param2',
673+
'@type': '@json',
674+
},
675+
},
676+
},
677+
MyClass2: {
678+
'@id': 'mp:b/file2#MyClass2',
679+
'@prefix': true,
680+
'@context': {},
681+
},
682+
});
683+
});
684+
615685
it('should handle non-empty component definitions when typeScopedContexts is true for opt arrays', () => {
616686
ctor = new ContextConstructor({
617687
packageMetadata,

0 commit comments

Comments
 (0)