1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-14 00:19:16 +01:00

fix: ensure that param values from /api/admin/features are strings (#1761)

* refactor: improve parameter value string casting

* fix: ensure that param values from /api/admin/features are strings
This commit is contained in:
olav 2022-06-29 10:11:34 +02:00 committed by GitHub
parent 390ae8df2b
commit b2b0958573
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 11 deletions

View File

@ -15,6 +15,8 @@ import {
} from '../types/model'; } from '../types/model';
import { IFeatureStrategiesStore } from '../types/stores/feature-strategies-store'; import { IFeatureStrategiesStore } from '../types/stores/feature-strategies-store';
import { PartialSome } from '../types/partial'; import { PartialSome } from '../types/partial';
import { ensureStringValue } from '../util/ensureStringValue';
import { mapValues } from '../util/map-values';
const COLUMNS = [ const COLUMNS = [
'id', 'id',
@ -53,15 +55,6 @@ interface IFeatureStrategiesTable {
created_at?: Date; created_at?: Date;
} }
function ensureStringValues(data: object): { [key: string]: string } {
const stringEntries = Object.entries(data).map(([key, value]) => [
key,
String(value),
]);
return Object.fromEntries(stringEntries);
}
function mapRow(row: IFeatureStrategiesTable): IFeatureStrategy { function mapRow(row: IFeatureStrategiesTable): IFeatureStrategy {
return { return {
id: row.id, id: row.id,
@ -69,7 +62,7 @@ function mapRow(row: IFeatureStrategiesTable): IFeatureStrategy {
projectId: row.project_name, projectId: row.project_name,
environment: row.environment, environment: row.environment,
strategyName: row.strategy_name, strategyName: row.strategy_name,
parameters: ensureStringValues(row.parameters), parameters: mapValues(row.parameters || {}, ensureStringValue),
constraints: (row.constraints as unknown as IConstraint[]) || [], constraints: (row.constraints as unknown as IConstraint[]) || [],
createdAt: row.created_at, createdAt: row.created_at,
sortOrder: row.sort_order, sortOrder: row.sort_order,

View File

@ -11,6 +11,8 @@ import { IFeatureToggleClientStore } from '../types/stores/feature-toggle-client
import { DEFAULT_ENV } from '../util/constants'; import { DEFAULT_ENV } from '../util/constants';
import { PartialDeep } from '../types/partial'; import { PartialDeep } from '../types/partial';
import EventEmitter from 'events'; import EventEmitter from 'events';
import { ensureStringValue } from '../util/ensureStringValue';
import { mapValues } from '../util/map-values';
export interface FeaturesTable { export interface FeaturesTable {
name: string; name: string;
@ -180,7 +182,7 @@ export default class FeatureToggleClientStore
id: row.strategy_id, id: row.strategy_id,
name: row.strategy_name, name: row.strategy_name,
constraints: row.constraints || [], constraints: row.constraints || [],
parameters: row.parameters, parameters: mapValues(row.parameters || {}, ensureStringValue),
}; };
} }

View File

@ -0,0 +1,16 @@
import { ensureStringValue } from './ensureStringValue';
test('ensureStringValue', () => {
expect(ensureStringValue(null)).toEqual('');
expect(ensureStringValue(undefined)).toEqual('');
expect(ensureStringValue('null')).toEqual('null');
expect(ensureStringValue('undefined')).toEqual('undefined');
expect(ensureStringValue('')).toEqual('');
expect(ensureStringValue('a')).toEqual('a');
expect(ensureStringValue(0)).toEqual('0');
expect(ensureStringValue(true)).toEqual('true');
expect(ensureStringValue({})).toEqual('{}');
expect(ensureStringValue({ b: 1 })).toEqual('{"b":1}');
});

View File

@ -0,0 +1,13 @@
import { isDefined } from './isDefined';
export function ensureStringValue(value: unknown): string {
if (!isDefined(value)) {
return '';
}
if (typeof value === 'object') {
return JSON.stringify(value);
}
return String(value);
}