Skip to content

Commit cf3109e

Browse files
author
Philip Z
committed
fix: forbid global metadata fallback in portable repository
1 parent 9e197d7 commit cf3109e

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

‎teaql-sql-portable/src/main/java/io/teaql/core/sql/portable/PortableSQLRepository.java‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,12 +1147,15 @@ private EntityDescriptor resolveDescriptor(Class<? extends Entity> entityType) {
11471147
if (entityDescriptor.getTargetType() == entityType) {
11481148
return entityDescriptor;
11491149
}
1150-
EntityMetaFactory descriptorSource = metadata != null ? metadata : EntityMetaFactory.get();
1151-
if (descriptorSource != null) {
1152-
for (EntityDescriptor descriptor : descriptorSource.allEntityDescriptors()) {
1153-
if (descriptor.getTargetType() == entityType) {
1154-
return descriptor;
1155-
}
1150+
if (metadata == null) {
1151+
throw new IllegalStateException("Repository for " + entityDescriptor.getType()
1152+
+ " has no runtime metadata snapshot for relation target "
1153+
+ entityType.getName()
1154+
+ "; schema-only repositories cannot hydrate relations");
1155+
}
1156+
for (EntityDescriptor descriptor : metadata.allEntityDescriptors()) {
1157+
if (descriptor.getTargetType() == entityType) {
1158+
return descriptor;
11561159
}
11571160
}
11581161
throw new IllegalStateException("No entity descriptor registered for " + entityType.getName());

‎teaql-sql-portable/src/test/java/io/teaql/core/sql/portable/PortableSQLDatabaseTest.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,32 @@ public void streamRelationHydrationMustFailRatherThanReturnPartialEntity() {
401401
}
402402
}
403403

404+
@Test
405+
public void schemaOnlyRepositoryMustNotHydrateRelationFromGlobalMetadata() {
406+
registerTopNFixture();
407+
EntityDescriptor child = metaFactory.resolveEntityDescriptor("TopNChild");
408+
PortableSQLRepository<TopNChild> schemaOnlyRepository =
409+
new PortableSQLRepository<>(child, sqliteDb, null);
410+
TopNChildRequest request = new TopNChildRequest()
411+
.where("id", Operator.EQUAL, 11L)
412+
.comment("load relation without a runtime metadata snapshot");
413+
request.purpose("prove a schema-only repository cannot borrow global metadata");
414+
415+
EntityMetaFactory previousGlobal = EntityMetaFactory.get();
416+
EntityMetaFactory.registerGlobal(metaFactory);
417+
try (var rows = schemaOnlyRepository.streamInternal(context, request)) {
418+
rows.findFirst();
419+
fail("A schema-only repository must not resolve a relation from global metadata");
420+
} catch (TeaQLRuntimeException failure) {
421+
assertTrue(failure.getMessage(), failure.getMessage().contains("TopNChild.parent"));
422+
assertTrue(failure.getCause() instanceof IllegalStateException);
423+
assertTrue(failure.getCause().getMessage(),
424+
failure.getCause().getMessage().contains("no runtime metadata snapshot"));
425+
} finally {
426+
EntityMetaFactory.registerGlobal(previousGlobal);
427+
}
428+
}
429+
404430
@Test
405431
public void testConstantBootstrapIsIdempotentAndReconcilesModelChanges() throws Exception {
406432
SQLiteTeaQLDatabase database = new SQLiteTeaQLDatabase();

0 commit comments

Comments
 (0)