diff --git a/.gitmodules b/.gitmodules index 85b786b2..84683c5b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +1,4 @@ [submodule "src/test/java/com/flagsmith/flagengine/enginetestdata"] path = src/test/java/com/flagsmith/flagengine/enginetestdata url = git@github.com:Flagsmith/engine-test-data.git - tag = v3.4.1 \ No newline at end of file + tag = v3.5.0 \ No newline at end of file diff --git a/src/main/java/com/flagsmith/flagengine/segments/SegmentEvaluator.java b/src/main/java/com/flagsmith/flagengine/segments/SegmentEvaluator.java index 214b4a1f..a38ce3aa 100644 --- a/src/main/java/com/flagsmith/flagengine/segments/SegmentEvaluator.java +++ b/src/main/java/com/flagsmith/flagengine/segments/SegmentEvaluator.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.flagsmith.flagengine.EvaluationContext; +import com.flagsmith.flagengine.IdentityContext; import com.flagsmith.flagengine.SegmentCondition; import com.flagsmith.flagengine.SegmentContext; import com.flagsmith.flagengine.SegmentRule; @@ -21,6 +22,7 @@ import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; import java.util.stream.Collectors; +import org.apache.commons.lang3.StringUtils; public class SegmentEvaluator { private static ObjectMapper mapper = new ObjectMapper(); @@ -77,10 +79,25 @@ private static Boolean contextMatchesCondition( EvaluationContext context, SegmentCondition condition, String segmentKey) { - Object contextValue = getContextValue(context, condition.getProperty()); + Object contextValue = null; Object conditionValue = condition.getValue(); + String conditionProperty = condition.getProperty(); SegmentConditions operator = condition.getOperator(); + if (operator == SegmentConditions.PERCENTAGE_SPLIT && StringUtils.isEmpty(conditionProperty)) { + // Currently, the only supported condition with a blank property + // is percentage split. + // In this case, we use the identity key as context value. + // This is mainly to support legacy segments created before + // we introduced JSONPath support. + IdentityContext identity = context.getIdentity(); + if (!(identity == null)) { + contextValue = identity.getKey(); + } + } else { + contextValue = getContextValue(context, conditionProperty); + } + switch (operator) { case IN: if (contextValue == null || contextValue instanceof Boolean) { @@ -109,20 +126,14 @@ private static Boolean contextMatchesCondition( return conditionList.contains(String.valueOf(contextValue)); case PERCENTAGE_SPLIT: - String key; if (contextValue == null) { - if (context.getIdentity() == null) { - return false; - } - key = context.getIdentity().getKey(); - } else { - key = contextValue.toString(); + return false; } - List objectIds = List.of(segmentKey, key); + List objectIds = List.of(segmentKey, contextValue.toString()); final float floatValue; try { - floatValue = Float.parseFloat(String.valueOf(condition.getValue())); + floatValue = Float.parseFloat(String.valueOf(conditionValue)); } catch (NumberFormatException e) { return false; } diff --git a/src/test/java/com/flagsmith/flagengine/enginetestdata b/src/test/java/com/flagsmith/flagengine/enginetestdata index 839e8d5e..7840a134 160000 --- a/src/test/java/com/flagsmith/flagengine/enginetestdata +++ b/src/test/java/com/flagsmith/flagengine/enginetestdata @@ -1 +1 @@ -Subproject commit 839e8d5e5f2e9af6392062cf5e575d43c03770d4 +Subproject commit 7840a1349b601df3b6b4a089f40864f659801afb