Skip to content

Commit 989970f

Browse files
author
jianggang
authored
🐛 fix: User attribute is empty, it will cause the conditional ma… (#41)
* 🐛 fix: User attribute is empty, it will cause the conditional match to fail
1 parent ca04c4d commit 989970f

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/main/java/com/featureprobe/sdk/server/model/Condition.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,25 @@ public final class Condition {
5353
static {
5454

5555
stringMatchers.put(PredicateType.IS_ONE_OF, (target, objects) -> objects.contains(target));
56-
stringMatchers.put(PredicateType.ENDS_WITH, (target, objects) -> objects.stream().anyMatch(target::endsWith));
56+
stringMatchers.put(PredicateType.ENDS_WITH,
57+
(target, objects) -> Objects.nonNull(target) && objects.stream().anyMatch(target::endsWith));
5758
stringMatchers.put(PredicateType.STARTS_WITH,
58-
(target, objects) -> objects.stream().anyMatch(target::startsWith));
59-
stringMatchers.put(PredicateType.CONTAINS, (target, objects) -> objects.stream().anyMatch(target::contains));
59+
(target, objects) -> Objects.nonNull(target) && objects.stream().anyMatch(target::startsWith));
60+
stringMatchers.put(PredicateType.CONTAINS,
61+
(target, objects) -> Objects.nonNull(target) && objects.stream().anyMatch(target::contains));
6062
stringMatchers.put(PredicateType.MATCHES_REGEX,
61-
(target, objects) -> objects.stream().anyMatch(s -> Pattern.compile(s).matcher(target).find()));
63+
(target, objects) -> objects.stream()
64+
.anyMatch(s -> Objects.nonNull(target) && Pattern.compile(s).matcher(target).find()));
6265
stringMatchers.put(PredicateType.IS_NOT_ANY_OF, (target, objects) -> !objects.contains(target));
6366
stringMatchers.put(PredicateType.DOES_NOT_END_WITH,
64-
(target, objects) -> objects.stream().noneMatch(target::endsWith));
67+
(target, objects) -> Objects.nonNull(target) && objects.stream().noneMatch(target::endsWith));
6568
stringMatchers.put(PredicateType.DOES_NOT_START_WITH,
66-
(target, objects) -> objects.stream().noneMatch(target::startsWith));
69+
(target, objects) -> Objects.nonNull(target) && objects.stream().noneMatch(target::startsWith));
6770
stringMatchers.put(PredicateType.DOES_NOT_CONTAIN,
68-
(target, objects) -> objects.stream().noneMatch(target::contains));
71+
(target, objects) -> Objects.nonNull(target) && objects.stream().noneMatch(target::contains));
6972
stringMatchers.put(PredicateType.DOES_NOT_MATCH_REGEX,
70-
(target, objects) -> objects.stream().noneMatch(s -> Pattern.compile(s).matcher(target).find()));
73+
(target, objects) -> Objects.nonNull(target)
74+
&& objects.stream().noneMatch(s -> Pattern.compile(s).matcher(target).find()));
7175

7276
segmentMatchers.put(PredicateType.IS_IN,
7377
(user, segments, objects) -> objects.stream().anyMatch(s -> segments.get(s).contains(user, segments)));
@@ -130,10 +134,10 @@ public boolean matchObjects(FPUser user, Map<String, Segment> segments) {
130134
}
131135

132136
private boolean matchStringCondition(FPUser user) {
133-
String subjectValue = user.getAttr(subject);
134-
if (StringUtils.isBlank(subjectValue)) {
137+
if (!user.containAttr(subject)) {
135138
return false;
136139
}
140+
String subjectValue = user.getAttr(subject);
137141

138142
StringMatcher stringMatcher = stringMatchers.get(this.predicate);
139143
if (Objects.isNull(stringMatcher)) {

src/test/resources/test

0 commit comments

Comments
 (0)