Merge pull request #15423 from SayakMukhopadhyay/fix-15422

fix: environment variable substitution in config doesn't work with boolean values
This commit is contained in:
Ben Lambert
2022-12-29 16:36:26 +01:00
committed by GitHub
3 changed files with 45 additions and 3 deletions
+7
View File
@@ -0,0 +1,7 @@
---
'@backstage/config': patch
---
Adds the ability to coerce values to their boolean representatives.
Values such as `"true"` `1` `on` and `y` will become `true` when using `getBoolean` and the opposites `false`.
This happens particularly when such parameters are used with environmental substitution as environment variables are always strings.
+21 -1
View File
@@ -22,6 +22,15 @@ const DATA = {
one: 1,
true: true,
false: false,
yes: 'yes',
no: 'no',
y: 'y',
n: 'n',
on: 'on',
off: 'off',
zeroString: '0',
oneString: '1',
stringFalse: 'false',
null: null,
string: 'string',
emptyString: '',
@@ -53,6 +62,17 @@ function expectValidValues(config: ConfigReader) {
expect(config.getOptional('true')).toBe(true);
expect(config.getBoolean('true')).toBe(true);
expect(config.getBoolean('false')).toBe(false);
expect(config.getBoolean('stringFalse')).toBe(false);
expect(config.getBoolean('zero')).toBe(false);
expect(config.getBoolean('one')).toBe(true);
expect(config.getBoolean('zeroString')).toBe(false);
expect(config.getBoolean('oneString')).toBe(true);
expect(config.getBoolean('yes')).toBe(true);
expect(config.getBoolean('no')).toBe(false);
expect(config.getBoolean('y')).toBe(true);
expect(config.getBoolean('n')).toBe(false);
expect(config.getBoolean('on')).toBe(true);
expect(config.getBoolean('off')).toBe(false);
expect(config.getString('string')).toBe('string');
expect(config.get('strings')).toEqual(['string1', 'string2']);
expect(config.getStringArray('strings')).toEqual(['string1', 'string2']);
@@ -86,7 +106,7 @@ function expectValidValues(config: ConfigReader) {
function expectInvalidValues(config: ConfigReader) {
expect(() => config.getBoolean('string')).toThrow(
"Invalid type in config for key 'string' in 'ctx', got string, wanted boolean",
"Unable to convert config value for key 'string' in 'ctx' to a boolean",
);
expect(() => config.getNumber('string')).toThrow(
"Unable to convert config value for key 'string' in 'ctx' to a number",
+17 -2
View File
@@ -280,10 +280,25 @@ export class ConfigReader implements Config {
/** {@inheritdoc Config.getOptionalBoolean} */
getOptionalBoolean(key: string): boolean | undefined {
return this.readConfigValue(
const value = this.readConfigValue<string | number | boolean>(
key,
value => typeof value === 'boolean' || { expected: 'boolean' },
val =>
typeof val === 'boolean' ||
typeof val === 'number' ||
typeof val === 'string' || { expected: 'boolean' },
);
if (typeof value === 'boolean' || value === undefined) {
return value;
}
const valueString = String(value).trim();
if (/^(?:y|yes|true|1|on)$/i.test(valueString)) {
return true;
}
if (/^(?:n|no|false|0|off)$/i.test(valueString)) {
return false;
}
throw new Error(errors.convert(this.fullKey(key), this.context, 'boolean'));
}
/** {@inheritdoc Config.getString} */