Skip to content

Commit

Permalink
fix(core): feature flag values should be booleans (#21759)
Browse files Browse the repository at this point in the history
`FeatureFlags.isEnabled()` returns `boolean | undefined`, but if the
value of the feature flag is something other than a boolean, it will
actually return that type instead. For example,

```ts
node.setContext(someFeatureFlag, 'true');
const enabled = FeatureFlags.of(node).isEnabled(someFeatureFlag);
if (enabled) {
  // will work
}

if (enabled === true) {
  // will not work because enabled === 'true'
}
```

It looks like the only place this bug crops up is in the
`FunctionVersionUpgrade` aspect.

https://github.com/aws/aws-cdk/blob/8b0832334afee496355e7aeb684773d8f939f058/packages/@aws-cdk/aws-lambda/lib/function.ts#L1306


----

### All Submissions:

* [ ] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
corymhall authored Aug 25, 2022
1 parent da61adc commit daf885f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/lib/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ export class FeatureFlags {
}
return true;
}
return context ?? cxapi.futureFlagDefault(featureFlag);
return context !== undefined ? Boolean(context) : cxapi.futureFlagDefault(featureFlag);
}
}
8 changes: 8 additions & 0 deletions packages/@aws-cdk/core/test/feature-flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@ describe('feature flags', () => {

expect(FeatureFlags.of(stack).isEnabled('non-existent-flag')).toEqual(false);
});

test('strings are evaluated as boolean', () => {
const stack = new Stack();
stack.node.setContext(cxapi.NEW_STYLE_STACK_SYNTHESIS_CONTEXT, 'true');

const actual = FeatureFlags.of(stack).isEnabled(cxapi.NEW_STYLE_STACK_SYNTHESIS_CONTEXT);
expect(actual).toEqual(true);
});
});
});

0 comments on commit daf885f

Please sign in to comment.