From a3d9f94e4489bdbebb13b86bc1d4c34daf9dbc00 Mon Sep 17 00:00:00 2001 From: Frederik Prijck Date: Mon, 31 Jul 2017 20:07:08 +0200 Subject: [PATCH 1/3] fix($httpParamSerializerJQLike): call functions as jQuery does Previously, `httpParamSerializerJQLike` stringified function properties without executing them. This commit ensures function properties are executed and the return value is used. Closes #16138 --- src/ng/http.js | 3 +++ test/ng/httpSpec.js | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/ng/http.js b/src/ng/http.js index a4e2094ab203..a3eedd565924 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -122,6 +122,9 @@ function $HttpParamSerializerJQLikeProvider() { (topLevel ? '' : ']')); }); } else { + if (isFunction(toSerialize)) { + toSerialize = toSerialize(); + } parts.push(encodeUriQuery(prefix) + '=' + (toSerialize == null ? '' : encodeUriQuery(serializeValue(toSerialize)))); } diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index 7ed6993c4109..123bd0aa5843 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -2364,17 +2364,41 @@ describe('$http param serializers', function() { expect(decodeURIComponent(jqrSer({a: 'b', foo: ['bar', 'baz']}))).toEqual('a=b&foo[]=bar&foo[]=baz'); }); + it('should serialize arrays with functions', function() { + expect(jqrSer({foo: [valueFn('bar')]})).toEqual('foo%5B%5D=bar'); + }); + + it('should serialize arrays with functions inside objects', function() { + expect(jqrSer({foo: {bar: [valueFn('baz')]}})).toEqual('foo%5Bbar%5D%5B%5D=baz'); // foo[bar][]=baz + }); + it('should serialize objects by repeating param name with [key] suffix', function() { expect(jqrSer({a: 'b', foo: {'bar': 'barv', 'baz': 'bazv'}})).toEqual('a=b&foo%5Bbar%5D=barv&foo%5Bbaz%5D=bazv'); //a=b&foo[bar]=barv&foo[baz]=bazv }); + it('should serialize objects with function properties', function() { + expect(jqrSer({a: valueFn('b')})).toEqual('a=b'); + }); + + it('should serialize objects with function properties returning an object by repeating param name with [key] suffix', function() { + expect(jqrSer({a: valueFn({b: 'c'})})).toEqual('a=%7B%22b%22:%22c%22%7D'); //a={"b":"c"} + }); + it('should serialize nested objects by repeating param name with [key] suffix', function() { expect(jqrSer({a: ['b', {c: 'd'}], e: {f: 'g', 'h': ['i', 'j']}})).toEqual( 'a%5B%5D=b&a%5B1%5D%5Bc%5D=d&e%5Bf%5D=g&e%5Bh%5D%5B%5D=i&e%5Bh%5D%5B%5D=j'); //a[]=b&a[1][c]=d&e[f]=g&e[h][]=i&e[h][]=j }); + it('should serialize nested objects with function properties', function() { + expect(jqrSer({foo: {bar: valueFn('barv')}})).toEqual('foo%5Bbar%5D=barv'); //foo[bar]=barv + }); + + it('should serialize nested objects with function properties returning an object', function() { + expect(jqrSer({foo: {bar: valueFn({ bav: 'barv'})}})).toEqual('foo%5Bbar%5D=%7B%22bav%22:%22barv%22%7D'); //foo[bar]={"bav":"barv"} + }); + it('should serialize objects inside array elements using their index', function() { expect(jqrSer({a: ['b', 'c'], d: [{e: 'f', g: 'h'}, 'i', {j: 'k'}]})).toEqual( 'a%5B%5D=b&a%5B%5D=c&d%5B0%5D%5Be%5D=f&d%5B0%5D%5Bg%5D=h&d%5B%5D=i&d%5B2%5D%5Bj%5D=k'); From 5d561bd28e58ee62a71cc4bd1348e84dc7d87dc0 Mon Sep 17 00:00:00 2001 From: Frederik Prijck Date: Thu, 3 Aug 2017 12:08:07 +0200 Subject: [PATCH 2/3] fixup! fix($httpParamSerializerJQLike): call functions as jQuery does --- test/ng/httpSpec.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index 123bd0aa5843..5373f9e584d8 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -2365,7 +2365,7 @@ describe('$http param serializers', function() { }); it('should serialize arrays with functions', function() { - expect(jqrSer({foo: [valueFn('bar')]})).toEqual('foo%5B%5D=bar'); + expect(jqrSer({foo: [valueFn('bar')]})).toEqual('foo%5B%5D=bar'); // foo[]=bar }); it('should serialize arrays with functions inside objects', function() { @@ -2381,7 +2381,7 @@ describe('$http param serializers', function() { expect(jqrSer({a: valueFn('b')})).toEqual('a=b'); }); - it('should serialize objects with function properties returning an object by repeating param name with [key] suffix', function() { + it('should serialize objects with function properties returning an object by serializing the returned object', function() { expect(jqrSer({a: valueFn({b: 'c'})})).toEqual('a=%7B%22b%22:%22c%22%7D'); //a={"b":"c"} }); @@ -2395,8 +2395,8 @@ describe('$http param serializers', function() { expect(jqrSer({foo: {bar: valueFn('barv')}})).toEqual('foo%5Bbar%5D=barv'); //foo[bar]=barv }); - it('should serialize nested objects with function properties returning an object', function() { - expect(jqrSer({foo: {bar: valueFn({ bav: 'barv'})}})).toEqual('foo%5Bbar%5D=%7B%22bav%22:%22barv%22%7D'); //foo[bar]={"bav":"barv"} + it('should serialize nested objects with function properties returning an object by serializing the returned object', function() { + expect(jqrSer({foo: {bar: valueFn({bav: 'barv'})}})).toEqual('foo%5Bbar%5D=%7B%22bav%22:%22barv%22%7D'); //foo[bar]={"bav":"barv"} }); it('should serialize objects inside array elements using their index', function() { From 64c0a6567a048c5ff41fabd2b0c58c128e7d90b2 Mon Sep 17 00:00:00 2001 From: Frederik Prijck Date: Thu, 3 Aug 2017 12:11:14 +0200 Subject: [PATCH 3/3] fixup! fix($httpParamSerializerJQLike): call functions as jQuery does --- test/ng/httpSpec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index 5373f9e584d8..b87d5750abdb 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -2381,7 +2381,7 @@ describe('$http param serializers', function() { expect(jqrSer({a: valueFn('b')})).toEqual('a=b'); }); - it('should serialize objects with function properties returning an object by serializing the returned object', function() { + it('should serialize objects with function properties returning an object', function() { expect(jqrSer({a: valueFn({b: 'c'})})).toEqual('a=%7B%22b%22:%22c%22%7D'); //a={"b":"c"} }); @@ -2395,7 +2395,7 @@ describe('$http param serializers', function() { expect(jqrSer({foo: {bar: valueFn('barv')}})).toEqual('foo%5Bbar%5D=barv'); //foo[bar]=barv }); - it('should serialize nested objects with function properties returning an object by serializing the returned object', function() { + it('should serialize nested objects with function properties returning an object', function() { expect(jqrSer({foo: {bar: valueFn({bav: 'barv'})}})).toEqual('foo%5Bbar%5D=%7B%22bav%22:%22barv%22%7D'); //foo[bar]={"bav":"barv"} });