Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit aa0de0d

Browse files
committed
feat($httpParamSerializerJQLike): honor object.toJSON function if present.
1 parent 8f563e2 commit aa0de0d

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/ng/http.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,14 @@ function $HttpParamSerializerJQLikeProvider() {
110110
return parts.join('&');
111111

112112
function serialize(toSerialize, prefix, topLevel) {
113-
if (toSerialize === null || isUndefined(toSerialize)) return;
113+
if (toSerialize === null || isUndefined(toSerialize) || isFunction(toSerialize)) return;
114114
if (isArray(toSerialize)) {
115115
forEach(toSerialize, function(value, index) {
116116
serialize(value, prefix + '[' + (isObject(value) ? index : '') + ']');
117117
});
118-
} else if (isObject(toSerialize) && !isDate(toSerialize)) {
118+
} else if (isFunction(toSerialize.toJSON)) {
119+
serialize(toSerialize.toJSON(), prefix, topLevel);
120+
} else if (isObject(toSerialize)) {
119121
forEachSorted(toSerialize, function(value, key) {
120122
serialize(value, prefix +
121123
(topLevel ? '' : '[') +

test/ng/httpSpec.js

+31
Original file line numberDiff line numberDiff line change
@@ -2274,6 +2274,37 @@ describe('$http param serializers', function() {
22742274
expect(jqrSer({someDate: new Date('2014-07-15T17:30:00.000Z')})).toEqual('someDate=2014-07-15T17:30:00.000Z');
22752275
});
22762276

2277+
it('should ignore functions', function() {
2278+
expect(jqrSer({
2279+
foo: {
2280+
a: 'b',
2281+
c: function() {
2282+
return 'd';
2283+
}
2284+
}
2285+
})).toEqual('foo%5Ba%5D=b');
2286+
});
2287+
2288+
it('should honor toJSON function', function() {
2289+
expect(jqrSer({
2290+
foo: {
2291+
a: 'b',
2292+
toJSON: function() {
2293+
return {
2294+
e: 'f',
2295+
g: 'h'
2296+
};
2297+
}
2298+
},
2299+
bar: {
2300+
c: 'd',
2301+
toJSON: function() {
2302+
return 'baz';
2303+
}
2304+
}
2305+
})).toEqual('bar=baz&foo%5Be%5D=f&foo%5Bg%5D=h');
2306+
});
2307+
22772308
});
22782309

22792310
describe('default array serialization', function() {

0 commit comments

Comments
 (0)