From 2170ea270ac1d5881958c92bec7601afbf03c9b1 Mon Sep 17 00:00:00 2001 From: Anas Barghoud Date: Tue, 16 Jul 2019 23:58:47 +0200 Subject: [PATCH] fix(router): fix error when calling ParamMap.get function (#31599) fix this.params.hasOwnProperty is not a function in case of creating an object using Object.create() PR Close #31599 --- packages/router/src/shared.ts | 2 +- packages/router/test/shared.spec.ts | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/router/src/shared.ts b/packages/router/src/shared.ts index d72d578240..6df50faa06 100644 --- a/packages/router/src/shared.ts +++ b/packages/router/src/shared.ts @@ -74,7 +74,7 @@ class ParamsAsMap implements ParamMap { } has(name: string): boolean { - return this.params.hasOwnProperty(name); + return Object.prototype.hasOwnProperty.call(this.params, name); } get(name: string): string|null { diff --git a/packages/router/test/shared.spec.ts b/packages/router/test/shared.spec.ts index 46f1fb43a7..1dfaf1826c 100644 --- a/packages/router/test/shared.spec.ts +++ b/packages/router/test/shared.spec.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {convertToParamMap, ParamMap} from '../src/shared'; +import {convertToParamMap, ParamMap, Params} from '../src/shared'; describe('ParamsMap', () => { it('should returns whether a parameter is present', () => { @@ -42,4 +42,14 @@ describe('ParamsMap', () => { const map = convertToParamMap({}); expect(map.getAll('name')).toEqual([]); }); + + it('should not error when trying to call ParamMap.get function using an object created with Object.create() function', + () => { + const objectToMap: Params = Object.create(null); + objectToMap['single'] = 's'; + objectToMap['multiple'] = ['m1', 'm2']; + const paramMaps: ParamMap = convertToParamMap(objectToMap); + expect(() => paramMaps.get('single')).not.toThrow(); + expect(paramMaps.get('single')).toEqual('s'); + }); });