Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions flagsmith-engine/segments/evaluators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as jsonpathModule from 'jsonpath';
import { JSONPath } from 'jsonpath-plus';
import {
GenericEvaluationContext,
InSegmentCondition,
Expand All @@ -10,9 +10,6 @@ import { getHashedPercentageForObjIds } from '../utils/hashing/index.js';
import { SegmentConditionModel } from './models.js';
import { IS_NOT_SET, IS_SET, PERCENTAGE_SPLIT } from './constants.js';

// Handle ESM/CJS interop - jsonpath exports default in ESM
const jsonpath = (jsonpathModule as any).default || jsonpathModule;

/**
* Returns all segments that the identity belongs to based on segment rules evaluation.
*
Expand Down Expand Up @@ -140,8 +137,22 @@ function evaluateRuleConditions(ruleType: string, conditionResults: boolean[]):
}
}

const TRAITS_DOT_PATTERN = /^\$\.identity\.traits\.(.+)$/;
const TRAITS_BRACKET_PATTERN = /^\$\.identity\.traits\['(.+)'\]$/;

function extractTraitNameFromPath(property: string): string | undefined {
return TRAITS_DOT_PATTERN.exec(property)?.[1] ?? TRAITS_BRACKET_PATTERN.exec(property)?.[1];
}

function getTraitValue(property: string, context?: GenericEvaluationContext): any {
if (property.startsWith('$.')) {
// Look up $.identity.traits.X and $.identity.traits['X'] paths directly
// to avoid jsonpath-plus mis-parsing special characters (e.g. $, [, ]) in
// trait names that appear inside bracket-notation strings.
const traitName = extractTraitNameFromPath(property);
if (traitName !== undefined) {
return context?.identity?.traits?.[traitName];
}
const contextValue = getContextValue(property, context);
if (contextValue !== undefined && isPrimitive(contextValue)) {
return contextValue;
Expand Down Expand Up @@ -179,14 +190,9 @@ export function getContextValue(jsonPath: string, context?: GenericEvaluationCon
if (!context || !jsonPath?.startsWith('$.')) return undefined;

try {
const normalizedPath = normalizeJsonPath(jsonPath);
const results = jsonpath.query(context, normalizedPath);
const results = JSONPath({ path: jsonPath, json: context });
return results.length > 0 ? results[0] : undefined;
} catch (error) {
return undefined;
}
}

function normalizeJsonPath(jsonPath: string): string {
return jsonPath.replace(/\.([^.\[\]]+)$/, "['$1']");
}
214 changes: 47 additions & 167 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"generate-engine-types": "npm run generate-evaluation-result-types && npm run generate-evaluation-context-types"
},
"dependencies": {
"jsonpath": "^1.1.1",
"jsonpath-plus": "^10.4.0",
"pino": "^10",
"semver": "^7.3.7",
"undici-types": "^6.19.8"
Expand Down
Loading