Skip to content

Commit d1e2b59

Browse files
authored
fix: Exclude identities when PERCENTAGE_SPLIT trait is undefined (#198)
1 parent 75749e6 commit d1e2b59

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[submodule "src/test/java/com/flagsmith/flagengine/enginetestdata"]
22
path = src/test/java/com/flagsmith/flagengine/enginetestdata
33
url = [email protected]:Flagsmith/engine-test-data.git
4-
tag = v3.4.1
4+
tag = v3.5.0

src/main/java/com/flagsmith/flagengine/segments/SegmentEvaluator.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.core.type.TypeReference;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import com.flagsmith.flagengine.EvaluationContext;
6+
import com.flagsmith.flagengine.IdentityContext;
67
import com.flagsmith.flagengine.SegmentCondition;
78
import com.flagsmith.flagengine.SegmentContext;
89
import com.flagsmith.flagengine.SegmentRule;
@@ -21,6 +22,7 @@
2122
import java.util.regex.Pattern;
2223
import java.util.regex.PatternSyntaxException;
2324
import java.util.stream.Collectors;
25+
import org.apache.commons.lang3.StringUtils;
2426

2527
public class SegmentEvaluator {
2628
private static ObjectMapper mapper = new ObjectMapper();
@@ -77,10 +79,25 @@ private static Boolean contextMatchesCondition(
7779
EvaluationContext context,
7880
SegmentCondition condition,
7981
String segmentKey) {
80-
Object contextValue = getContextValue(context, condition.getProperty());
82+
Object contextValue = null;
8183
Object conditionValue = condition.getValue();
84+
String conditionProperty = condition.getProperty();
8285
SegmentConditions operator = condition.getOperator();
8386

87+
if (operator == SegmentConditions.PERCENTAGE_SPLIT && StringUtils.isEmpty(conditionProperty)) {
88+
// Currently, the only supported condition with a blank property
89+
// is percentage split.
90+
// In this case, we use the identity key as context value.
91+
// This is mainly to support legacy segments created before
92+
// we introduced JSONPath support.
93+
IdentityContext identity = context.getIdentity();
94+
if (!(identity == null)) {
95+
contextValue = identity.getKey();
96+
}
97+
} else {
98+
contextValue = getContextValue(context, conditionProperty);
99+
}
100+
84101
switch (operator) {
85102
case IN:
86103
if (contextValue == null || contextValue instanceof Boolean) {
@@ -109,20 +126,14 @@ private static Boolean contextMatchesCondition(
109126
return conditionList.contains(String.valueOf(contextValue));
110127

111128
case PERCENTAGE_SPLIT:
112-
String key;
113129
if (contextValue == null) {
114-
if (context.getIdentity() == null) {
115-
return false;
116-
}
117-
key = context.getIdentity().getKey();
118-
} else {
119-
key = contextValue.toString();
130+
return false;
120131
}
121-
List<String> objectIds = List.of(segmentKey, key);
132+
List<String> objectIds = List.of(segmentKey, contextValue.toString());
122133

123134
final float floatValue;
124135
try {
125-
floatValue = Float.parseFloat(String.valueOf(condition.getValue()));
136+
floatValue = Float.parseFloat(String.valueOf(conditionValue));
126137
} catch (NumberFormatException e) {
127138
return false;
128139
}

0 commit comments

Comments
 (0)