Skip to content

Commit

Permalink
Support @JSON ranges in arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Oct 23, 2024
1 parent 623057e commit 10d7e48
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/serialize/ContextConstructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class ContextConstructor {
const shortcut = parameter['@id'].slice(Math.max(0, component['@id'].length + 1));
typeScopedContext[shortcut] = {
'@id': parameter['@id'],
...parameter.range === 'rdf:JSON' ? { '@type': '@json' } : {},
...ContextConstructor.isParameterRangeJson(parameter.range) ? { '@type': '@json' } : {},
// Mark as list container if range is array
...ContextConstructor.isParameterRangeList(parameter.range) ?
{ '@container': '@list' } :
Expand Down Expand Up @@ -133,6 +133,14 @@ export class ContextConstructor {
public static isParameterRangeUndefined(range: ParameterDefinitionRange): boolean {
return typeof range !== 'string' && '@type' in range && range['@type'] === 'ParameterRangeUndefined';
}

public static isParameterRangeJson(range: ParameterDefinitionRange | undefined): boolean {
if (range && typeof range !== 'string' && '@type' in range &&
range['@type'] === 'ParameterRangeArray' && this.isParameterRangeJson(range.parameterRangeValue)) {
return true;
}
return range === 'rdf:JSON';
}
}

export interface ContextConstructorArgs {
Expand Down
70 changes: 70 additions & 0 deletions test/serialize/ContextConstructor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,76 @@ describe('ContextConstructor', () => {
});
});

it('should handle non-empty component definitions for JSON ranges in arrays', () => {
expect(ctor.constructComponentShortcuts({
'/docs/package/components/file1': {
'@context': [
'https://linkedsoftwaredependencies.org/bundles/npm/my-package/context.jsonld',
],
'@id': 'npmd:my-package',
components: [
{
'@id': 'mp:file1#MyClass1',
'@type': 'Class',
constructorArguments: [],
parameters: [
{
'@id': 'mp:file1#MyClass1_param1',
range: {
'@type': 'ParameterRangeArray',
parameterRangeValue: 'rdf:JSON',
},
},
{
'@id': 'mp:file1#MyClass1_param2',
range: 'rdf:JSON',
},
],
memberFields: [],
requireElement: 'MyClass1',
},
],
},
'/docs/package/components/b/file2': {
'@context': [
'https://linkedsoftwaredependencies.org/bundles/npm/my-package/context.jsonld',
],
'@id': 'npmd:my-package',
components: [
{
'@id': 'mp:b/file2#MyClass2',
'@type': 'Class',
requireElement: 'MyClass2',
constructorArguments: [],
parameters: [],
memberFields: [],
},
],
},
})).toEqual({
MyClass1: {
'@id': 'mp:file1#MyClass1',
'@prefix': true,
'@context': {
param1: {
'@id': 'mp:file1#MyClass1_param1',
'@type': '@json',
'@container': '@list',
},
param2: {
'@id': 'mp:file1#MyClass1_param2',
'@type': '@json',
},
},
},
MyClass2: {
'@id': 'mp:b/file2#MyClass2',
'@prefix': true,
'@context': {},
},
});
});

it('should handle non-empty component definitions when typeScopedContexts is true for opt arrays', () => {
ctor = new ContextConstructor({
packageMetadata,
Expand Down

0 comments on commit 10d7e48

Please sign in to comment.