Skip to content

Commit 869d8a5

Browse files
authored
fix(RDF): Output not always as expected when using table inheritance (#4417)
* Fixed test succeeding due to re-use of RDFHandler * Split buggy test into 2 separate tests * fixed the test: describeTable() only used if 'rowId == null' so predicate not available as subject * reduced duplicate code * removed unneeded import * Added multiple tests to validate behavior * Added second child table to ensure correct functioning * enhanced tests to validate table inheritance from different schema * test fixes * if RDF API uses table filter, ensures all child tables from same scheme are included as well * Added additional tests to validate cross-schema behavior * Improved tests to include multi-layer external schema & reduced testing code duplicity * updated test to ensure a grandchild uses all inherited columns and validate on this * minor fix * Added test when using non-existing table * fixed some tests * RDFSchema filtering on table now works with a filter defining multiple schemas * removed unneeded code * minor fixes / comment adjustments
1 parent c612410 commit 869d8a5

File tree

2 files changed

+532
-67
lines changed

2 files changed

+532
-67
lines changed

backend/molgenis-emx2-rdf/src/main/java/org/molgenis/emx2/rdf/RDFService.java

+46-16
Original file line numberDiff line numberDiff line change
@@ -186,26 +186,31 @@ public void describeAsRDF(
186186
describeRoot(builder);
187187
}
188188

189+
// Collect all tables present in selected schemas.
190+
Set<Table> allTables = new HashSet<>();
191+
189192
for (final Schema schema : schemas) {
190193
if (table == null) {
191194
describeSchema(builder, schema);
192195
}
193-
final List<Table> tables = table != null ? Arrays.asList(table) : schema.getTablesSorted();
194-
for (final Table tableToDescribe : tables) {
195-
// for full-schema retrieval, don't print the (huge and mostly unused) ontologies
196-
// of course references to ontologies are still included and are fully retrievable
197-
if (table == null
198-
&& tableToDescribe.getMetadata().getTableType().equals(TableType.ONTOLOGIES)) {
199-
continue;
200-
}
201-
if (rowId == null) {
202-
describeTable(builder, tableToDescribe);
203-
describeColumns(builder, tableToDescribe, columnName);
204-
}
205-
// if a column name is provided then only provide column metadata, no row values
206-
if (columnName == null) {
207-
rowsToRdf(builder, tableToDescribe, rowId);
208-
}
196+
allTables.addAll(schema.getTablesSorted());
197+
}
198+
199+
final Set<Table> tables = table != null ? tablesToDescribe(allTables, table) : allTables;
200+
for (final Table tableToDescribe : tables) {
201+
// for full-schema retrieval, don't print the (huge and mostly unused) ontologies
202+
// of course references to ontologies are still included and are fully retrievable
203+
if (table == null
204+
&& tableToDescribe.getMetadata().getTableType().equals(TableType.ONTOLOGIES)) {
205+
continue;
206+
}
207+
if (rowId == null) {
208+
describeTable(builder, tableToDescribe);
209+
describeColumns(builder, tableToDescribe, columnName);
210+
}
211+
// if a column name is provided then only provide column metadata, no row values
212+
if (columnName == null) {
213+
rowsToRdf(builder, tableToDescribe, rowId);
209214
}
210215
}
211216
Rio.write(builder.build(), outputStream, rdfFormat, config);
@@ -215,6 +220,31 @@ public void describeAsRDF(
215220
}
216221
}
217222

223+
private Set<Table> tablesToDescribe(Set<Table> allTables, Table tableFilter) {
224+
Set<Table> tablesToDescribe = new HashSet<>();
225+
for (Table currentTable : allTables) {
226+
processInheritedTable(tableFilter, tablesToDescribe, currentTable);
227+
}
228+
return tablesToDescribe;
229+
}
230+
231+
private boolean processInheritedTable(
232+
Table tableFilter, Set<Table> tablesToDescribe, Table currentTable) {
233+
if (currentTable == null) {
234+
return false;
235+
}
236+
if (currentTable.getSchema().getName().equals(tableFilter.getSchema().getName())
237+
&& currentTable.getName().equals(tableFilter.getName())) {
238+
tablesToDescribe.add(currentTable);
239+
return true;
240+
}
241+
if (processInheritedTable(tableFilter, tablesToDescribe, currentTable.getInheritedTable())) {
242+
tablesToDescribe.add(currentTable);
243+
return true;
244+
}
245+
return false;
246+
}
247+
218248
private void addModelToBuilder(ModelBuilder builder, Model model) {
219249
model.getNamespaces().forEach(builder::setNamespace);
220250
model.forEach(e -> builder.add(e.getSubject(), e.getPredicate(), e.getObject()));

0 commit comments

Comments
 (0)