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
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