Skip to content

Commit 240304b

Browse files
committed
create update delete throw error
"Cannot read field "tenantColumn" because "m" is null" if table is misspelled #482
1 parent 6f4f682 commit 240304b

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

dashjoin-core/src/main/java/org/dashjoin/model/AbstractDatabase.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ public Object cast(Property p, Object object) {
251251
try {
252252
return Double.parseDouble(s);
253253
} catch (Exception e3) {
254-
throw new NumberFormatException("Expecting a numerical value: " + s + " Property: " + p.ID + " / " + p.name);
254+
throw new NumberFormatException(
255+
"Expecting a numerical value: " + s + " Property: " + p.ID + " / " + p.name);
255256
}
256257
}
257258
}
@@ -507,4 +508,14 @@ public List<Origin> incoming(@Context SecurityContext sc, String database, Strin
507508
}
508509
return res;
509510
}
511+
512+
/**
513+
* centralized null safe map.get(name)
514+
*/
515+
public Table table(String table) {
516+
Table m = tables.get(table);
517+
if (m == null)
518+
throw new IllegalArgumentException("Unknown table: " + table);
519+
return m;
520+
}
510521
}

dashjoin-core/src/main/java/org/dashjoin/service/Data.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public List<SearchResult> search(@Context SecurityContext sc,
176176
@PathParam("search") String search, @QueryParam("limit") Integer limit) throws Exception {
177177

178178
AbstractDatabase db = services.getConfig().getDatabase(dj(database));
179-
Table m = db.tables.get(table);
179+
Table m = db.table(table);
180180
ACLContainerRequestFilter.check(sc, db, m);
181181
return db.search(sc, m, search, limit);
182182
}
@@ -345,9 +345,7 @@ QueryAndParams getAnalytics(SecurityContext sc, AbstractDatabase db, Analytics a
345345
ACLContainerRequestFilter.check(sc, info);
346346
}
347347

348-
Table m = db.tables.get(a.table);
349-
if (m == null)
350-
throw new IllegalArgumentException("Unknown table: " + a.table);
348+
Table m = db.table(a.table);
351349
ACLContainerRequestFilter.check(sc, db, m);
352350

353351
for (ColInfo e : a.cols) {
@@ -441,11 +439,7 @@ public List<Map<String, Object>> all(@Context SecurityContext sc,
441439
@QueryParam("sort") String sort, @QueryParam("descending") boolean descending,
442440
Map<String, Object> arguments) throws Exception {
443441
AbstractDatabase db = services.getConfig().getDatabase(dj(database));
444-
Table m = db.tables.get(table);
445-
446-
if (m == null)
447-
throw new IllegalArgumentException("Unknown table: " + table);
448-
442+
Table m = db.table(table);
449443
ACLContainerRequestFilter.check(sc, db, m);
450444
db.cast(m, arguments);
451445
return db.all(m, offset, limit, sort, descending,
@@ -484,7 +478,7 @@ public List<Choice> keys(@Context SecurityContext sc,
484478
example = "EMPLOYEES") @PathParam("table") String table,
485479
@QueryParam("prefix") String prefix, @QueryParam("limit") Integer limit) throws Exception {
486480
AbstractDatabase db = services.getConfig().getDatabase(dj(database));
487-
Table m = db.tables.get(table);
481+
Table m = db.table(table);
488482
ACLContainerRequestFilter.check(sc, db, m);
489483

490484
if (m.properties != null) {
@@ -566,7 +560,7 @@ public Resource createInternal(@Context SecurityContext sc,
566560
Map<String, Object> object) throws Exception {
567561
MapUtil.clean(object);
568562
AbstractDatabase db = services.getConfig().getDatabase(dj(database));
569-
Table m = db.tables.get(table);
563+
Table m = db.table(table);
570564
ACLContainerRequestFilter.check(sc, db, m, CREATE);
571565
ACLContainerRequestFilter.checkRow(sc, m, object);
572566

@@ -679,7 +673,7 @@ public Object traverse(SecurityContext sc, String database, String table, List<S
679673
String fk) throws Exception {
680674
Map<String, Object> o = read(sc, database, table, objectId);
681675
AbstractDatabase db = services.getConfig().getDatabase(dj(database));
682-
Table m = db.tables.get(table);
676+
Table m = db.table(table);
683677
Property p = m.properties.get(fk);
684678
if (p != null && p.ref != null) {
685679
// fk is an outgoing fk
@@ -805,7 +799,7 @@ public Map<String, Object> read(@Context SecurityContext sc,
805799
public Map<String, Object> read(@Context SecurityContext sc, String database, String table,
806800
List<String> objectId) throws Exception {
807801
AbstractDatabase db = services.getConfig().getDatabase(dj(database));
808-
Table m = db.tables.get(table);
802+
Table m = db.table(table);
809803
ACLContainerRequestFilter.check(sc, db, m);
810804
Map<String, Object> search = key(m, objectId);
811805
db.cast(m, search);
@@ -1062,7 +1056,7 @@ public List<Origin> incoming(@Context SecurityContext sc, String database, Strin
10621056
Integer timeout = services.getConfig().getAllTimeoutMs();
10631057

10641058
AbstractDatabase db = services.getConfig().getDatabase(dj(database));
1065-
Table m = db.tables.get(table);
1059+
Table m = db.table(table);
10661060
ACLContainerRequestFilter.check(sc, db, m);
10671061
String objectId = objectIds.get(0);
10681062

@@ -1106,7 +1100,7 @@ public Map<String, Map<String, Object>> list(@Context SecurityContext sc,
11061100
if (Boolean.TRUE.equals(ignoreMissing)) {
11071101
// record not found - add the {pk=search} to the result
11081102
AbstractDatabase db = services.getConfig().getDatabase(dj(database));
1109-
Table m = db.tables.get(table);
1103+
Table m = db.table(table);
11101104
Map<String, Object> search = key(m, Arrays.asList(objectId));
11111105
db.cast(m, search);
11121106
res.put(objectId, search);
@@ -1200,7 +1194,7 @@ public void update(SecurityContext sc, String database, String table, List<Strin
12001194
Map<String, Object> object) throws Exception {
12011195
MapUtil.clean(object);
12021196
AbstractDatabase db = services.getConfig().getDatabase(dj(database));
1203-
Table m = db.tables.get(table);
1197+
Table m = db.table(table);
12041198

12051199
if (m.tenantColumn != null)
12061200
read(sc, database, table, objectId);
@@ -1300,7 +1294,7 @@ public void delete(@Context SecurityContext sc,
13001294
public void delete(SecurityContext sc, String database, String table, List<String> objectId)
13011295
throws Exception {
13021296
AbstractDatabase db = services.getConfig().getDatabase(dj(database));
1303-
Table m = db.tables.get(table);
1297+
Table m = db.table(table);
13041298

13051299
if (m.tenantColumn != null)
13061300
read(sc, database, table, objectId);

0 commit comments

Comments
 (0)