flagsmith-nodejs-client icon indicating copy to clipboard operation
flagsmith-nodejs-client copied to clipboard

"Contains" and "Does not contain" operators throw on non-string/array trait values

Open rolodato opened this issue 1 year ago • 0 comments
trafficstars

Minimal reproduction example:

import Flagsmith from "flagsmith-nodejs"

const flagsmith = new Flagsmith({
  environmentKey: process.env.FLAGSMITH_SERVER_ENVIRONMENT_KEY,
  enableLocalEvaluation: true,
});

const { flags } = await flagsmith.getIdentityFlags("foo", {
  my_trait: null
})

console.log(flags)

with the following segment definition: my_trait Contains whatever

Result:

% bun index.ts
50 |     _a[constants_1.CONDITION_OPERATORS.LESS_THAN_INCLUSIVE] = function (thisValue, otherValue) {
51 |         return thisValue >= otherValue;
52 |     },
53 |     _a[constants_1.CONDITION_OPERATORS.NOT_EQUAL] = function (thisValue, otherValue) { return thisValue != otherValue; },
54 |     _a[constants_1.CONDITION_OPERATORS.CONTAINS] = function (thisValue, otherValue) {
55 |         return otherValue.includes(thisValue);
                    ^
TypeError: null is not an object (evaluating 'otherValue.includes')
      at /Users/rolodato/source/flagsmith/sandbox/node/node_modules/flagsmith-nodejs/build/flagsmith-engine/segments/models.js:55:16
      at /Users/rolodato/source/flagsmith/sandbox/node/node_modules/flagsmith-nodejs/build/flagsmith-engine/segments/models.js:110:16
      at traitsMatchSegmentCondition (/Users/rolodato/source/flagsmith/sandbox/node/node_modules/flagsmith-nodejs/build/flagsmith-engine/segments/evaluators.js:43:20)
      at /Users/rolodato/source/flagsmith/sandbox/node/node_modules/flagsmith-nodejs/build/flagsmith-engine/segments/evaluators.js:22:20
      at map (:1:21)
      at traitsMatchSegmentRule (/Users/rolodato/source/flagsmith/sandbox/node/node_modules/flagsmith-nodejs/build/flagsmith-engine/segments/evaluators.js:21:35)
      at /Users/rolodato/source/flagsmith/sandbox/node/node_modules/flagsmith-nodejs/build/flagsmith-engine/segments/evaluators.js:27:20
      at filter (:1:21)
      at traitsMatchSegmentRule (/Users/rolodato/source/flagsmith/sandbox/node/node_modules/flagsmith-nodejs/build/flagsmith-engine/segments/evaluators.js:26:9)
      at /Users/rolodato/source/flagsmith/sandbox/node/node_modules/flagsmith-nodejs/build/flagsmith-engine/segments/evaluators.js:15:20

This happens the same for Does not contain as well. We should be checking for null-sy values here:

https://github.com/Flagsmith/flagsmith-nodejs-client/blob/3b3a078eb0ecd3842aaf7080730461fad95b7cd6/flagsmith-engine/segments/models.ts#L30 https://github.com/Flagsmith/flagsmith-nodejs-client/blob/3b3a078eb0ecd3842aaf7080730461fad95b7cd6/flagsmith-engine/segments/models.ts#L67

rolodato avatar May 07 '24 23:05 rolodato