Skip to content

Commit 4a1d064

Browse files
committed
Handle JSON as ParameterRangeArray if an array
1 parent e710060 commit 4a1d064

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

lib/preprocess/parameterproperty/ParameterPropertyHandlerRange.ts

+17
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,23 @@ export class ParameterPropertyHandlerRange implements IParameterPropertyHandler
133133
// Check if the param type is an array
134134
if (value && type.isA('ParameterRangeArray')) {
135135
if (!value.list) {
136+
// If the value is a JSON literal, try to interpret as an array
137+
if (value.term.termType === 'Literal' && value.term.datatype.value === IRIS_RDF.JSON) {
138+
const jsonString = value.value;
139+
if (jsonString.startsWith('[') && jsonString.endsWith(']')) {
140+
try {
141+
const parsed = JSON.parse(value.value);
142+
(<any>value.term).valueRaw = parsed;
143+
return;
144+
} catch (error: unknown) {
145+
return {
146+
description: `JSON parse exception: ${(<Error> error).message}`,
147+
context: errorContext,
148+
};
149+
}
150+
}
151+
}
152+
136153
return {
137154
description: `value is not an RDF list`,
138155
context: errorContext,

test/unit/preprocess/parameterproperty/ParameterPropertyHandlerRange-test.ts

+55
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,61 @@ describe('ParameterPropertyHandlerRange', () => {
716716
});
717717
});
718718

719+
it('should handle array type with json array', () => {
720+
const value1 = objectLoader.createCompactedResource(DF.literal(
721+
'["a", "b", "c"]',
722+
DF.namedNode(IRIS_RDF.JSON),
723+
));
724+
expect(handler.hasValueType(
725+
value1,
726+
objectLoader.createCompactedResource({
727+
'@type': 'ParameterRangeArray',
728+
parameterRangeValue: { '@id': 'ex:SomeType1' },
729+
}),
730+
errorContext,
731+
genericsContext,
732+
)).toBeUndefined();
733+
expect((<any> value1.term).valueRaw).toEqual([ 'a', 'b', 'c' ]);
734+
});
735+
736+
it('should throw on array type with invalid json array', () => {
737+
const value1 = objectLoader.createCompactedResource(DF.literal(
738+
'["a" "b", "c"]',
739+
DF.namedNode(IRIS_RDF.JSON),
740+
));
741+
expect(handler.hasValueType(
742+
value1,
743+
objectLoader.createCompactedResource({
744+
'@type': 'ParameterRangeArray',
745+
parameterRangeValue: { '@id': 'ex:SomeType1' },
746+
}),
747+
errorContext,
748+
genericsContext,
749+
)).toEqual({
750+
description: expect.stringContaining('JSON parse exception'),
751+
context: expect.anything(),
752+
});
753+
});
754+
755+
it('should not handle array type with json non-array', () => {
756+
const value1 = objectLoader.createCompactedResource(DF.literal(
757+
'"a"',
758+
DF.namedNode(IRIS_RDF.JSON),
759+
));
760+
expect(handler.hasValueType(
761+
value1,
762+
objectLoader.createCompactedResource({
763+
'@type': 'ParameterRangeArray',
764+
parameterRangeValue: { '@id': 'ex:SomeType1' },
765+
}),
766+
errorContext,
767+
genericsContext,
768+
)).toEqual({
769+
description: `value is not an RDF list`,
770+
context: expect.anything(),
771+
});
772+
});
773+
719774
it('should handle tuple type with single entry', () => {
720775
expect(handler.hasValueType(
721776
objectLoader.createCompactedResource({

0 commit comments

Comments
 (0)