From aa0de0d6624669ed43c62e7e27221ba3410da336 Mon Sep 17 00:00:00 2001 From: Gabriel Monteagudo Date: Sun, 12 Jul 2015 04:32:43 -0300 Subject: [PATCH] feat($httpParamSerializerJQLike): honor object.toJSON function if present. --- src/ng/http.js | 6 ++++-- test/ng/httpSpec.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/ng/http.js b/src/ng/http.js index 6c9785e5ac06..43bb7843f990 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -110,12 +110,14 @@ function $HttpParamSerializerJQLikeProvider() { return parts.join('&'); function serialize(toSerialize, prefix, topLevel) { - if (toSerialize === null || isUndefined(toSerialize)) return; + if (toSerialize === null || isUndefined(toSerialize) || isFunction(toSerialize)) return; if (isArray(toSerialize)) { forEach(toSerialize, function(value, index) { serialize(value, prefix + '[' + (isObject(value) ? index : '') + ']'); }); - } else if (isObject(toSerialize) && !isDate(toSerialize)) { + } else if (isFunction(toSerialize.toJSON)) { + serialize(toSerialize.toJSON(), prefix, topLevel); + } else if (isObject(toSerialize)) { forEachSorted(toSerialize, function(value, key) { serialize(value, prefix + (topLevel ? '' : '[') + diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index f03582bdde23..ee70abbcaeca 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -2274,6 +2274,37 @@ describe('$http param serializers', function() { expect(jqrSer({someDate: new Date('2014-07-15T17:30:00.000Z')})).toEqual('someDate=2014-07-15T17:30:00.000Z'); }); + it('should ignore functions', function() { + expect(jqrSer({ + foo: { + a: 'b', + c: function() { + return 'd'; + } + } + })).toEqual('foo%5Ba%5D=b'); + }); + + it('should honor toJSON function', function() { + expect(jqrSer({ + foo: { + a: 'b', + toJSON: function() { + return { + e: 'f', + g: 'h' + }; + } + }, + bar: { + c: 'd', + toJSON: function() { + return 'baz'; + } + } + })).toEqual('bar=baz&foo%5Be%5D=f&foo%5Bg%5D=h'); + }); + }); describe('default array serialization', function() {