fix url query parser treating empty string as a number (#32825)

This commit is contained in:
Domas 2021-04-09 11:23:08 +03:00 committed by GitHub
parent 9efaa2c13f
commit 7c3a528d35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 1 deletions

View File

@ -40,6 +40,11 @@ describe('parseKeyValue', () => {
expect(obj).toEqual({ num1: 12, num2: 12.2 }); expect(obj).toEqual({ num1: 12, num2: 12.2 });
}); });
it('should not parse empty strinhg as number', () => {
const obj = urlUtil.parseKeyValue('num1=&num2=12.2');
expect(obj).toEqual({ num1: '', num2: 12.2 });
});
it('should parse boolean params', () => { it('should parse boolean params', () => {
const obj = urlUtil.parseKeyValue('bool1&bool2=true&bool3=false'); const obj = urlUtil.parseKeyValue('bool1&bool2=true&bool3=false');
expect(obj).toEqual({ bool1: true, bool2: true, bool3: false }); expect(obj).toEqual({ bool1: true, bool2: true, bool3: false });

View File

@ -155,7 +155,7 @@ export function parseKeyValue(keyValue: string) {
val = val !== undefined ? tryDecodeURIComponent(val as string) : true; val = val !== undefined ? tryDecodeURIComponent(val as string) : true;
let parsedVal: any; let parsedVal: any;
if (typeof val === 'string') { if (typeof val === 'string' && val !== '') {
parsedVal = val === 'true' || val === 'false' ? val === 'true' : _.toNumber(val); parsedVal = val === 'true' || val === 'false' ? val === 'true' : _.toNumber(val);
} else { } else {
parsedVal = val; parsedVal = val;