Skip to content

Commit

Permalink
fix: flag resolver get variant (#6020)
Browse files Browse the repository at this point in the history
https://linear.app/unleash/issue/2-1880/fix-flag-resolver-getvariant-behavior

Fixes the flag resolver `getVariant` behavior when there's a variant
object set in `experimental` - The flag resolver should call the
external resolver `getVariant` when not overridden to be true, even if
set as variant object in `experimental`.

Related: #3808
  • Loading branch information
nunogois authored Jan 24, 2024
1 parent cee2500 commit 3310334
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
41 changes: 41 additions & 0 deletions src/lib/util/flag-resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,44 @@ test('should expose an helper to get variant value', () => {
foo: 'bar',
});
});

test('should call external resolver getVariant when not overridden to be true, even if set as object in experimental', () => {
const variant = {
enabled: true,
name: 'variant',
payload: {
type: PayloadType.STRING,
value: 'variant-A',
},
};

const externalResolver = {
isEnabled: () => true,
getVariant: (name: string) => {
if (name === 'variantFlag') {
return variant;
}
return getDefaultVariant();
},
};

const config = {
flags: {
variantFlag: {
name: 'variant-flag',
enabled: false,
payload: {
type: PayloadType.JSON,
value: '',
},
},
},
externalResolver,
};

const resolver = new FlagResolver(config as IExperimentalOptions);

expect(resolver.getVariant('variantFlag' as IFlagKey)).toStrictEqual(
variant,
);
});
2 changes: 1 addition & 1 deletion src/lib/util/flag-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class FlagResolver implements IFlagResolver {
const exp = this.experiments[expName];
if (exp) {
if (typeof exp === 'boolean') return getDefaultVariant();
else return exp;
else if (exp.enabled) return exp;
}
return this.externalResolver.getVariant(expName, context);
}
Expand Down

0 comments on commit 3310334

Please sign in to comment.