Skip to content

Commit

Permalink
Handle JSON as ParameterRangeArray if an array
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Oct 23, 2024
1 parent e710060 commit 4a1d064
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/preprocess/parameterproperty/ParameterPropertyHandlerRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,23 @@ export class ParameterPropertyHandlerRange implements IParameterPropertyHandler
// Check if the param type is an array
if (value && type.isA('ParameterRangeArray')) {
if (!value.list) {
// If the value is a JSON literal, try to interpret as an array
if (value.term.termType === 'Literal' && value.term.datatype.value === IRIS_RDF.JSON) {
const jsonString = value.value;
if (jsonString.startsWith('[') && jsonString.endsWith(']')) {
try {
const parsed = JSON.parse(value.value);
(<any>value.term).valueRaw = parsed;
return;
} catch (error: unknown) {
return {
description: `JSON parse exception: ${(<Error> error).message}`,
context: errorContext,
};
}
}
}

return {
description: `value is not an RDF list`,
context: errorContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,61 @@ describe('ParameterPropertyHandlerRange', () => {
});
});

it('should handle array type with json array', () => {
const value1 = objectLoader.createCompactedResource(DF.literal(
'["a", "b", "c"]',
DF.namedNode(IRIS_RDF.JSON),
));
expect(handler.hasValueType(
value1,
objectLoader.createCompactedResource({
'@type': 'ParameterRangeArray',
parameterRangeValue: { '@id': 'ex:SomeType1' },
}),
errorContext,
genericsContext,
)).toBeUndefined();
expect((<any> value1.term).valueRaw).toEqual([ 'a', 'b', 'c' ]);
});

it('should throw on array type with invalid json array', () => {
const value1 = objectLoader.createCompactedResource(DF.literal(
'["a" "b", "c"]',
DF.namedNode(IRIS_RDF.JSON),
));
expect(handler.hasValueType(
value1,
objectLoader.createCompactedResource({
'@type': 'ParameterRangeArray',
parameterRangeValue: { '@id': 'ex:SomeType1' },
}),
errorContext,
genericsContext,
)).toEqual({
description: expect.stringContaining('JSON parse exception'),
context: expect.anything(),
});
});

it('should not handle array type with json non-array', () => {
const value1 = objectLoader.createCompactedResource(DF.literal(
'"a"',
DF.namedNode(IRIS_RDF.JSON),
));
expect(handler.hasValueType(
value1,
objectLoader.createCompactedResource({
'@type': 'ParameterRangeArray',
parameterRangeValue: { '@id': 'ex:SomeType1' },
}),
errorContext,
genericsContext,
)).toEqual({
description: `value is not an RDF list`,
context: expect.anything(),
});
});

it('should handle tuple type with single entry', () => {
expect(handler.hasValueType(
objectLoader.createCompactedResource({
Expand Down

0 comments on commit 4a1d064

Please sign in to comment.