Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix(core): context value for feature flag should be parsed for boolean
  • Loading branch information
kumsmrit committed Jun 3, 2025
commit 57b4aa769df5e8b1699e20a3afb0e56c6d2099d4
14 changes: 13 additions & 1 deletion packages/aws-cdk-lib/core/lib/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ export class FeatureFlags {
}
return true;
}
return context !== undefined ? Boolean(context) : cxapi.futureFlagDefault(featureFlag);
if (context === undefined) {
return cxapi.futureFlagDefault(featureFlag);
}
if (typeof context === 'string') {
let parsedContext;
try {
parsedContext = JSON.parse(context.trim().toLowerCase());
} catch (e) {
throw new Error(`Feature flag '${featureFlag}' expected boolean context, but instead got '${context}'`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe log a warning to stderr and return true instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer an Annotation over a log statement.

}
return Boolean(parsedContext);
}
return Boolean(context);
}
}
8 changes: 8 additions & 0 deletions packages/aws-cdk-lib/core/test/feature-flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,13 @@ describe('feature flags', () => {
const actual = FeatureFlags.of(stack).isEnabled(cxapi.NEW_STYLE_STACK_SYNTHESIS_CONTEXT);
expect(actual).toEqual(true);
});

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

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