Skip to content

Commit

Permalink
fix(flagd): improve targeting key check in fractional operator (#1048)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Beemer <[email protected]>
  • Loading branch information
beeme1mr authored Aug 30, 2024
1 parent 00c9e87 commit 7d82649
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions libs/shared/flagd-core/src/lib/targeting/fractional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export function fractionalFactory(logger: Logger) {
bucketBy = args[0];
buckets = args.slice(1, args.length);
} else {
bucketBy = `${flagdProperties[flagKeyPropertyKey]}${context[targetingPropertyKey]}`;
if (!bucketBy) {
const targetingKey = context[targetingPropertyKey];
if (!targetingKey) {
logger.debug('Missing targetingKey property, cannot perform fractional targeting');
return null;
}

bucketBy = `${flagdProperties[flagKeyPropertyKey]}${targetingKey}`;
buckets = args;
}

Expand Down
11 changes: 11 additions & 0 deletions libs/shared/flagd-core/src/lib/targeting/targeting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ describe('fractional operator', () => {

expect(targeting.applyTargeting('flagA', input, { targetingKey: 'bucketKeyB' })).toBe('blue');
});

it('should return null if targeting key is missing', () => {
const input = {
fractional: [
['red', 1],
['blue', 1],
],
};

expect(targeting.applyTargeting('flagA', input, {})).toBe(null);
});
});

describe('fractional operator should validate', () => {
Expand Down

0 comments on commit 7d82649

Please sign in to comment.