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

Commit 18a2e4f

Browse files
committed
fix(httpParamSerializerJQLike): Follow jQuery for index of arrays of objects
Follow jQuery when serializing arrays that contain objects Close #12393 Close #12398
1 parent 32d3cbb commit 18a2e4f

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/ng/http.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ function $HttpParamSerializerJQLikeProvider() {
108108
function serialize(toSerialize, prefix, topLevel) {
109109
if (toSerialize === null || isUndefined(toSerialize)) return;
110110
if (isArray(toSerialize)) {
111-
forEach(toSerialize, function(value) {
112-
serialize(value, prefix + '[]');
111+
forEach(toSerialize, function(value, index) {
112+
serialize(value, prefix + '[' + (isObject(value) ? index : '') + ']');
113113
});
114114
} else if (isObject(toSerialize) && !isDate(toSerialize)) {
115115
forEachSorted(toSerialize, function(value, key) {

test/ng/httpSpec.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -2022,8 +2022,14 @@ describe('$http param serializers', function() {
20222022

20232023
it('should serialize nested objects by repeating param name with [key] suffix', function() {
20242024
expect(jqrSer({a: ['b', {c: 'd'}], e: {f: 'g', 'h': ['i', 'j']}})).toEqual(
2025-
'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');
2026-
//a[]=b&a[][c]=d&e[f]=g&e[h][]=i&e[h][]=j
2025+
'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');
2026+
//a[]=b&a[1][c]=d&e[f]=g&e[h][]=i&e[h][]=j
2027+
});
2028+
2029+
it('should serialize objects inside array elements using their index', function() {
2030+
expect(jqrSer({a: ['b', 'c'], d: [{e: 'f', g: 'h'}, 'i', {j: 'k'}]})).toEqual(
2031+
'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');
2032+
//a[]=b&a[]=c&d[0][e]=f&d[0][g]=h&d[]=i&d[2][j]=k
20272033
});
20282034
});
20292035

0 commit comments

Comments
 (0)