Skip to content

Commit 269e76a

Browse files
committed
avoid mutating input context
1 parent 2db832e commit 269e76a

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/main/java/com/flagsmith/flagengine/Engine.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public HashMap<String, ImmutablePair<String, FeatureContext>> getSegmentFeatureC
3737
* @return Evaluation result.
3838
*/
3939
public static EvaluationResult getEvaluationResult(EvaluationContext context) {
40-
enrichEvaluationContext(context);
40+
context = getEnrichedEvaluationContext(context);
4141
SegmentEvaluationResult segmentEvaluationResult = evaluateSegments(context);
4242
Flags flags = evaluateFeatures(context, segmentEvaluationResult.getSegmentFeatureContexts());
4343

@@ -46,13 +46,22 @@ public static EvaluationResult getEvaluationResult(EvaluationContext context) {
4646
.withSegments(segmentEvaluationResult.getSegments());
4747
}
4848

49-
private static void enrichEvaluationContext(EvaluationContext context) {
49+
/*
50+
* Get a version of the evaluation context enriched with derived data.
51+
*
52+
* @param context Evaluation context.
53+
* @return Enriched evaluation context, or the original if no enrichment was needed.
54+
*/
55+
private static EvaluationContext getEnrichedEvaluationContext(EvaluationContext context) {
5056
IdentityContext identity = context.getIdentity();
5157
if (identity != null) {
5258
if (StringUtils.isEmpty(identity.getKey())) {
53-
identity.setKey(context.getEnvironment().getKey() + "_" + identity.getIdentifier());
59+
String identityKey = context.getEnvironment().getKey() + "_" + identity.getIdentifier();
60+
context = new EvaluationContext(context).withIdentity(
61+
new IdentityContext(identity).withKey(identityKey));
5462
}
5563
}
64+
return context;
5665
}
5766

5867
private static SegmentEvaluationResult evaluateSegments(

0 commit comments

Comments
 (0)