Skip to content

Commit 20f0f01

Browse files
committed
fix: exclude-identities-when-traits-is-undefined
1 parent 29c7613 commit 20f0f01

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

.gitmodules

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

flagsmith-engine/index.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import {
44
EvaluationResultWithMetadata,
55
FeatureContextWithMetadata,
66
SDKFeatureMetadata,
7-
FlagResultWithMetadata
7+
FlagResultWithMetadata,
8+
GenericEvaluationContext
89
} from './evaluation/models.js';
9-
import { getIdentitySegments, getIdentityKey } from './segments/evaluators.js';
10+
import { getIdentitySegments } from './segments/evaluators.js';
1011
import { EvaluationResultFlags } from './evaluation/models.js';
1112
import { TARGETING_REASONS } from './features/types.js';
1213
import { getHashedPercentageForObjIds } from './utils/hashing/index.js';
@@ -37,12 +38,29 @@ export type SegmentOverrides = Record<string, SegmentOverride>;
3738
export function getEvaluationResult(
3839
context: EvaluationContextWithMetadata
3940
): EvaluationResultWithMetadata {
40-
const { segments, segmentOverrides } = evaluateSegments(context);
41-
const flags = evaluateFeatures(context, segmentOverrides);
41+
const enrichedContext = getEnrichedContext(context);
42+
const { segments, segmentOverrides } = evaluateSegments(enrichedContext);
43+
const flags = evaluateFeatures(enrichedContext, segmentOverrides);
4244

4345
return { flags, segments };
4446
}
4547

48+
function getEnrichedContext(context: EvaluationContextWithMetadata): EvaluationContextWithMetadata {
49+
const identityKey = getIdentityKey(context);
50+
if (!identityKey) return context;
51+
52+
return {
53+
...context,
54+
...(context.identity && {
55+
identity: {
56+
identifier: context.identity.identifier,
57+
key: identityKey,
58+
traits: context.identity.traits || {}
59+
}
60+
})
61+
};
62+
}
63+
4664
/**
4765
* Evaluates which segments the identity belongs to and collects feature overrides.
4866
*
@@ -234,3 +252,8 @@ const getTargetingMatchReason = (matchObject: TargetingMatchReason) => {
234252

235253
return TARGETING_REASONS.DEFAULT;
236254
};
255+
256+
const getIdentityKey = (context: GenericEvaluationContext): string | undefined => {
257+
if (!context.identity) return undefined;
258+
return context.identity.key || `${context.environment.key}_${context.identity?.identifier}`;
259+
};

flagsmith-engine/segments/evaluators.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,18 @@ export function traitsMatchSegmentCondition(
4848
context?: GenericEvaluationContext
4949
): boolean {
5050
if (condition.operator === PERCENTAGE_SPLIT) {
51-
const contextValueKey =
52-
getContextValue(condition.property, context) || getIdentityKey(context);
53-
const hashedPercentage = getHashedPercentageForObjIds([segmentKey, contextValueKey]);
51+
let splitKey: string | undefined;
52+
53+
if (!condition.property) {
54+
splitKey = context?.identity?.key;
55+
} else {
56+
splitKey = getContextValue(condition.property, context);
57+
}
58+
59+
if (!splitKey) {
60+
return false;
61+
}
62+
const hashedPercentage = getHashedPercentageForObjIds([segmentKey, splitKey]);
5463
return hashedPercentage <= parseFloat(String(condition.value));
5564
}
5665
if (!condition.property) {
@@ -175,12 +184,6 @@ export function getContextValue(jsonPath: string, context?: GenericEvaluationCon
175184
}
176185
}
177186

178-
export function getIdentityKey(context?: GenericEvaluationContext): string | undefined {
179-
if (!context?.identity) return undefined;
180-
181-
return context.identity.key || `${context.environment.key}_${context.identity.identifier}`;
182-
}
183-
184187
function normalizeJsonPath(jsonPath: string): string {
185188
return jsonPath.replace(/\.([^.\[\]]+)$/, "['$1']");
186189
}

tests/engine/unit/segments/segment_evaluators.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,6 @@ describe('percentage split operator', () => {
431431
const mockHashFn = getHashedPercentageForObjIds;
432432
mockHashFn.mockReturnValue(hashedValue);
433433
const condition = {
434-
property: 'any',
435434
operator: 'PERCENTAGE_SPLIT',
436435
value: threshold.toString()
437436
} as SegmentCondition1 | InSegmentCondition;

0 commit comments

Comments
 (0)