Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e77080f

Browse files
committedApr 19, 2015
fix($httpParamSerializerJQLike): Follow jQuery parameter serialization logic
Follow jQuery parameter serialization logic for nested objects. Closes #11551
1 parent 08411cf commit e77080f

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed
 

‎src/ng/http.js

+32-6
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,56 @@ var JSON_PROTECTION_PREFIX = /^\)\]\}',?\n/;
1111

1212
function paramSerializerFactory(jQueryMode) {
1313

14+
return jQueryMode ? jQueryLikeParamSerializer : paramSerializer;
15+
1416
function serializeValue(v) {
1517
if (isObject(v)) {
1618
return isDate(v) ? v.toISOString() : toJson(v);
1719
}
1820
return v;
1921
}
2022

21-
return function paramSerializer(params) {
23+
function paramSerializer(params) {
2224
if (!params) return '';
2325
var parts = [];
2426
forEachSorted(params, function(value, key) {
2527
if (value === null || isUndefined(value)) return;
26-
if (isArray(value) || isObject(value) && jQueryMode) {
28+
if (isArray(value)) {
2729
forEach(value, function(v, k) {
28-
var keySuffix = jQueryMode ? '[' + (!isArray(value) ? k : '') + ']' : '';
29-
parts.push(encodeUriQuery(key + keySuffix) + '=' + encodeUriQuery(serializeValue(v)));
30+
parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(serializeValue(v)));
3031
});
3132
} else {
3233
parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(serializeValue(value)));
3334
}
3435
});
3536

36-
return parts.length > 0 ? parts.join('&') : '';
37-
};
37+
return parts.join('&');
38+
}
39+
40+
function jQueryLikeParamSerializer(params) {
41+
if (!params) return '';
42+
var parts = [];
43+
serialize(params, '', true);
44+
return parts.join('&');
45+
46+
function serialize(toSerialize, prefix, topLevel) {
47+
if (toSerialize === null || isUndefined(toSerialize)) return;
48+
if (isArray(toSerialize)) {
49+
forEach(toSerialize, function(value) {
50+
serialize(value, prefix + '[]');
51+
});
52+
} else if (isObject(toSerialize) && !isDate(toSerialize)) {
53+
forEachSorted(toSerialize, function(value, key) {
54+
serialize(value, prefix +
55+
(topLevel ? '' : '[') +
56+
key +
57+
(topLevel ? '' : ']'));
58+
});
59+
} else {
60+
parts.push(encodeUriQuery(prefix) + '=' + encodeUriQuery(serializeValue(toSerialize)));
61+
}
62+
}
63+
}
3864
}
3965

4066
function $HttpParamSerializerProvider() {

‎test/ng/httpSpec.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,8 @@ describe('$http param serializers', function() {
19921992
it('should serialize objects', function() {
19931993
expect(defSer({foo: 'foov', bar: 'barv'})).toEqual('bar=barv&foo=foov');
19941994
expect(jqrSer({foo: 'foov', bar: 'barv'})).toEqual('bar=barv&foo=foov');
1995+
expect(defSer({someDate: new Date('2014-07-15T17:30:00.000Z')})).toEqual('someDate=2014-07-15T17:30:00.000Z');
1996+
expect(jqrSer({someDate: new Date('2014-07-15T17:30:00.000Z')})).toEqual('someDate=2014-07-15T17:30:00.000Z');
19951997
});
19961998

19971999
});
@@ -2010,10 +2012,16 @@ describe('$http param serializers', function() {
20102012
expect(decodeURIComponent(jqrSer({a: 'b', foo: ['bar', 'baz']}))).toEqual('a=b&foo[]=bar&foo[]=baz');
20112013
});
20122014

2013-
it('should serialize objects by repeating param name with [kay] suffix', function() {
2015+
it('should serialize objects by repeating param name with [key] suffix', function() {
20142016
expect(jqrSer({a: 'b', foo: {'bar': 'barv', 'baz': 'bazv'}})).toEqual('a=b&foo%5Bbar%5D=barv&foo%5Bbaz%5D=bazv');
20152017
//a=b&foo[bar]=barv&foo[baz]=bazv
20162018
});
2019+
2020+
it('should serialize nested objects by repeating param name with [key] suffix', function() {
2021+
expect(jqrSer({a: ['b', {c: 'd'}], e: {f: 'g', 'h': ['i', 'j']}})).toEqual(
2022+
'a%5B%5D=b&a%5B%5D%5Bc%5D=d&e%5Bf%5D=g&e%5Bh%5D%5B%5D=i&e%5Bh%5D%5B%5D=j');
2023+
//a[]=b&a[][c]=d&e[f]=g&e[h][]=i&e[h][]=j
2024+
});
20172025
});
20182026

20192027
});

0 commit comments

Comments
 (0)
This repository has been archived.