Skip to content

Commit 7c602cc

Browse files
author
Philip Z
committed
fix: reject invalid context-owned relation attachment
1 parent d2c1b5e commit 7c602cc

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

‎teaql-core/src/main/java/io/teaql/core/BaseEntity.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,21 @@ public void addRelation(UserContext context, String relationName, Entity value)
245245
io.teaql.core.meta.EntityDescriptor descriptor =
246246
io.teaql.core.meta.EntityMetaFactory.requireFrom(context)
247247
.resolveEntityDescriptor(this.typeName());
248+
if (descriptor == null) {
249+
throw new IllegalStateException("No entity descriptor registered in the invoking context for "
250+
+ this.typeName());
251+
}
252+
io.teaql.core.meta.PropertyDescriptor property = descriptor.findProperty(relationName);
253+
if (property == null || property.getType() == null) {
254+
throw new IllegalArgumentException("No relation " + this.typeName() + "." + relationName
255+
+ " registered in the invoking context");
256+
}
257+
Class<?> relationType = property.getType().javaType();
258+
if (!SmartList.class.isAssignableFrom(relationType)
259+
&& !Entity.class.isAssignableFrom(relationType)) {
260+
throw new IllegalArgumentException("Property " + this.typeName() + "." + relationName
261+
+ " is not a relation in the invoking context");
262+
}
248263
addRelation(descriptor, relationName, value);
249264
}
250265

‎teaql-core/src/test/java/io/teaql/core/BaseEntityTest.java‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,11 @@ public void testAddRelationUsesInvokingContextMetadata() {
418418
new io.teaql.core.meta.PropertyDescriptor();
419419
singleProperty.setName("entityRel");
420420
singleProperty.setType(new io.teaql.core.meta.SimplePropertyType(Entity.class));
421-
single.setProperties(java.util.List.of(singleProperty));
421+
io.teaql.core.meta.PropertyDescriptor scalarProperty =
422+
new io.teaql.core.meta.PropertyDescriptor();
423+
scalarProperty.setName("label");
424+
scalarProperty.setType(new io.teaql.core.meta.SimplePropertyType(String.class));
425+
single.setProperties(java.util.List.of(singleProperty, scalarProperty));
422426
io.teaql.core.meta.SimpleEntityMetaFactory singleMetadata =
423427
new io.teaql.core.meta.SimpleEntityMetaFactory();
424428
singleMetadata.register(single);
@@ -452,6 +456,18 @@ public void testAddRelationUsesInvokingContextMetadata() {
452456
((Entity) singleEntity).addRelation(singleContext, "entityRel", singleChild);
453457
assertSame(singleChild, singleEntity.getProperty("entityRel"));
454458

459+
TeaQLRuntimeException missingEntity = assertThrows(TeaQLRuntimeException.class,
460+
() -> new TestEntity().addRelation(
461+
contextWithMetadata(new io.teaql.core.meta.SimpleEntityMetaFactory()),
462+
"entityRel", singleChild));
463+
assertTrue(missingEntity.getMessage().contains("TestEntity"));
464+
IllegalArgumentException missingRelation = assertThrows(IllegalArgumentException.class,
465+
() -> singleEntity.addRelation(singleContext, "missingRel", singleChild));
466+
assertTrue(missingRelation.getMessage().contains("TestEntity.missingRel"));
467+
IllegalArgumentException scalarRelation = assertThrows(IllegalArgumentException.class,
468+
() -> singleEntity.addRelation(singleContext, "label", singleChild));
469+
assertTrue(scalarRelation.getMessage().contains("not a relation"));
470+
455471
try {
456472
singleEntity.addRelation("entityRel", singleChild);
457473
fail("Legacy no-context attachment must not silently pick another model");

0 commit comments

Comments
 (0)