Skip to content

Commit 51f3c64

Browse files
committed
bugfixing SimpleContentChecker
1 parent 6b4b11c commit 51f3c64

File tree

2 files changed

+57
-42
lines changed

2 files changed

+57
-42
lines changed

src/main/java/org/restheart/hal/metadata/singletons/SimpleContentChecker.java

+27-21
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ public boolean check(HttpServerExchange exchange, RequestContext context, DBObje
8989
if (args instanceof BasicDBList) {
9090
boolean patching = context.getMethod() == RequestContext.METHOD.PATCH;
9191

92-
BasicDBList conditions = filterMissingOptionalAndNullNullableConditions((BasicDBList) args, context.getContent());
93-
9492
if (patching) {
9593
// if patching the keys can use the dot notation
9694
// example {"a.b": 1, "a.c": 2}
@@ -99,9 +97,15 @@ public boolean check(HttpServerExchange exchange, RequestContext context, DBObje
9997
DBObject content = context.getContent();
10098

10199
return !content.keySet().stream().anyMatch(key -> {
102-
return !applyConditions(conditions, remapJson(key, content.get(key)), context);
100+
DBObject remappedContent = remapJson(key, content.get(key));
101+
102+
BasicDBList conditions = filterMissingOptionalAndNullNullableConditions((BasicDBList) args, remappedContent, true);
103+
104+
return !applyConditions(conditions, remappedContent, context);
103105
});
104106
} else {
107+
BasicDBList conditions = filterMissingOptionalAndNullNullableConditions((BasicDBList) args, context.getContent(), false);
108+
105109
return applyConditions(conditions, context.getContent(), context);
106110
}
107111
} else {
@@ -239,7 +243,7 @@ private boolean applyConditions(BasicDBList conditions, DBObject json, final Req
239243
});
240244
}
241245

242-
private BasicDBList filterMissingOptionalAndNullNullableConditions(BasicDBList conditions, DBObject content) {
246+
private BasicDBList filterMissingOptionalAndNullNullableConditions(BasicDBList conditions, DBObject content, boolean patching) {
243247
// nullPaths contains all paths that result to null and condition is nullable or optional
244248
Set<String> nullPaths = new HashSet<>();
245249

@@ -283,7 +287,7 @@ private BasicDBList filterMissingOptionalAndNullNullableConditions(BasicDBList c
283287
}
284288
}
285289

286-
if (optional) {
290+
if (optional || patching) {
287291
Object _path = ((BasicDBObject) condition).get("path");
288292

289293
if (_path != null && _path instanceof String) {
@@ -452,29 +456,31 @@ private boolean checkRegex(DBObject json, String path, String regex, boolean opt
452456
try {
453457
props = JsonUtils.getPropsFromPath(_json, path);
454458
} catch (IllegalArgumentException ex) {
459+
LOGGER.debug("checkRegex({}, {}) -> {}", path, regex, ex.getMessage());
460+
context.addWarning("checkRegex condition failed: path: " + path + ", regex: " + regex + ", got: " + ex.getMessage());
455461
return false;
456462
}
457463

458-
// props is null when path does not exist.
459-
if (props == null) {
460-
return optional;
461-
}
462-
463464
boolean ret;
464465

465-
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
466+
// props is null when path does not exist.
467+
if (props == null) {
468+
ret = optional;
469+
} else {
470+
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
466471

467-
ret = props.stream().allMatch((Optional<Object> prop) -> {
468-
if (prop == null) {
469-
return optional;
470-
}
472+
ret = props.stream().allMatch((Optional<Object> prop) -> {
473+
if (prop == null) {
474+
return optional;
475+
}
471476

472-
if (prop.isPresent()) {
473-
return p.matcher(JsonUtils.serialize(prop.get())).find();
474-
} else {
475-
return nullable;
476-
}
477-
});
477+
if (prop.isPresent()) {
478+
return p.matcher(JsonUtils.serialize(prop.get())).find();
479+
} else {
480+
return nullable;
481+
}
482+
});
483+
}
478484

479485
LOGGER.debug("checkRegex({}, {}) -> {} -> {}", path, regex, props, ret);
480486

src/main/java/org/restheart/utils/JsonUtils.java

+30-21
Original file line numberDiff line numberDiff line change
@@ -197,37 +197,46 @@ public static boolean isAncestorPath(final String left, final String right) {
197197
throw new IllegalArgumentException("wrong right path: " + right);
198198
}
199199

200-
if (right.startsWith(left)) {
201-
return true;
202-
} else {
200+
boolean ret = true;
201+
202+
if (!right.startsWith(left)) {
203203
String leftPathTokens[] = left.split(Pattern.quote("."));
204204
String rightPathTokens[] = right.split(Pattern.quote("."));
205205

206206
if (leftPathTokens.length > rightPathTokens.length) {
207-
return false;
208-
}
209-
210-
for (int cont = 0; cont < leftPathTokens.length; cont++) {
211-
String lt = leftPathTokens[cont];
212-
String rt = rightPathTokens[cont];
207+
ret = false;
208+
} else {
209+
outerloop:
210+
for (int cont = 0; cont < leftPathTokens.length; cont++) {
211+
String lt = leftPathTokens[cont];
212+
String rt = rightPathTokens[cont];
213213

214-
switch (lt) {
215-
case "*":
216-
break;
217-
case "[*]":
218-
try {
219-
Integer.parseInt(rt);
214+
switch (lt) {
215+
case "*":
220216
break;
221-
} catch (NumberFormatException nfe) {
222-
return false;
223-
}
224-
default:
225-
return rt.equals(lt);
217+
case "[*]":
218+
try {
219+
Integer.parseInt(rt);
220+
break;
221+
} catch (NumberFormatException nfe) {
222+
ret = false;
223+
break outerloop;
224+
}
225+
default:
226+
ret = rt.equals(lt);
227+
228+
if (!ret) {
229+
break outerloop;
230+
} else {
231+
break;
232+
}
233+
}
226234
}
227235
}
228236
}
229237

230-
return true;
238+
LOGGER.debug("{} -> {} -> {}", left, right, ret);
239+
return ret;
231240
}
232241

233242
/**

0 commit comments

Comments
 (0)