Skip to content

Commit 5d86f49

Browse files
committed
SchemaService - improve resolve ref. function and naming
* In the resolveRef function: check that the linking property's schema has a type * Added test case for this * Renamed findTargetReferences to getFindReferenceTargetsFunction to make clear that it returns a function
1 parent 058f8ad commit 5d86f49

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/core/schema.service.impl.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ const resolveRef = (schema: JsonSchema, findTargets: (rootData: Object) => Objec
9292
// get all objects that could be referenced.
9393
const candidates = findTargets(rootData);
9494

95+
if (_.isEmpty(schema.properties[propName].type)) {
96+
throw Error(`The schema of the property '${propName}' does not specify a schema type.`);
97+
}
9598
if (schema.properties[propName].type === 'array') {
9699
const ids: object[] = data[propName];
97100
const resultList = candidates.filter(value => ids.indexOf(value[identifyingProperty]) > -1);
@@ -124,7 +127,7 @@ const resolveRef = (schema: JsonSchema, findTargets: (rootData: Object) => Objec
124127
}
125128
};
126129

127-
const findReferenceTargets = (pathToContainment: string, schemaId: string) =>
130+
const getFindReferenceTargetsFunction = (pathToContainment: string, schemaId: string) =>
128131
(rootData: Object) => {
129132
const candidates = pathToContainment
130133
.split('/')
@@ -197,17 +200,17 @@ export class SchemaServiceImpl implements SchemaService {
197200
const variableWrapped = href.match(/\{.*\}/)[0];
198201
const pathToContainment = href.split(/\/\{.*\}/)[0];
199202
const variable = variableWrapped.substring(1, variableWrapped.length - 1);
200-
const foundTargets = findReferenceTargets(pathToContainment, targetSchema.id);
203+
const findTargets = getFindReferenceTargetsFunction(pathToContainment, targetSchema.id);
201204
const identifyingProp = JsonForms.config.getIdentifyingProp();
202205
result.push(
203206
new ReferencePropertyImpl(
204207
schema.properties[variable],
205208
targetSchema,
206209
variable,
207210
variable,
208-
foundTargets,
211+
findTargets,
209212
addReference(schema, identifyingProp, variable),
210-
resolveRef(schema, foundTargets, identifyingProp, variable)
213+
resolveRef(schema, findTargets, identifyingProp, variable)
211214
)
212215
);
213216
});

test/schema.service.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,43 @@ test('reference array properties get multiple', t => {
795795
t.is(getDataArray[0], data.classes[0]);
796796
t.is(getDataArray[1], data.classes[2]);
797797
});
798+
test(`reference properties get - linking property's schema without type`, t => {
799+
const schema = {
800+
definitions: {
801+
class: {
802+
type: 'object',
803+
properties: {
804+
id: {
805+
type: 'string'
806+
},
807+
association: {
808+
type: ''
809+
}
810+
},
811+
links: [{
812+
rel: 'full',
813+
href: '#/classes/{association}',
814+
targetSchema: {$ref: '#/definitions/class'}
815+
}]
816+
}
817+
},
818+
type: 'object',
819+
properties: {
820+
classes: {
821+
type: 'array',
822+
items: {
823+
$ref: '#/definitions/class'
824+
}
825+
}
826+
}
827+
};
828+
829+
const service: SchemaService = new SchemaServiceImpl(schema);
830+
const property = service.getReferenceProperties(schema.properties.classes.items as JsonSchema)[0];
831+
const data = {classes: [{id: 'c1'}, {id: 'c2', association: 'c1'}]};
832+
const error: Error = t.throws(() => property.getData(data, data.classes[1]));
833+
t.is(error.message, `The schema of the property 'association' does not specify a schema type.`);
834+
});
798835

799836
test('reference property find reference targets', t => {
800837
const schema = t.context.referenceFindSchema;

0 commit comments

Comments
 (0)