Skip to content
This repository has been archived by the owner on Apr 13, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(httpParamSerializerJQLike): Follow jQuery for index of arrays of …
…objects

Follow jQuery when serializing arrays that contain objects

Close #12393
Close #12398
  • Loading branch information
lgalfaso committed Jul 23, 2015
1 parent 32d3cbb commit 18a2e4f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/ng/http.js
Expand Up @@ -108,8 +108,8 @@ function $HttpParamSerializerJQLikeProvider() {
function serialize(toSerialize, prefix, topLevel) {
if (toSerialize === null || isUndefined(toSerialize)) return;
if (isArray(toSerialize)) {
forEach(toSerialize, function(value) {
serialize(value, prefix + '[]');
forEach(toSerialize, function(value, index) {
serialize(value, prefix + '[' + (isObject(value) ? index : '') + ']');
});
} else if (isObject(toSerialize) && !isDate(toSerialize)) {
forEachSorted(toSerialize, function(value, key) {
Expand Down
10 changes: 8 additions & 2 deletions test/ng/httpSpec.js
Expand Up @@ -2022,8 +2022,14 @@ describe('$http param serializers', function() {

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%5B%5D%5Bc%5D=d&e%5Bf%5D=g&e%5Bh%5D%5B%5D=i&e%5Bh%5D%5B%5D=j');
//a[]=b&a[][c]=d&e[f]=g&e[h][]=i&e[h][]=j
'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 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');
//a[]=b&a[]=c&d[0][e]=f&d[0][g]=h&d[]=i&d[2][j]=k
});
});

Expand Down

2 comments on commit 18a2e4f

@earcanal
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why the index should only be serialized on arrays of objects? My use case (Moodle web services API) wants also wants array indices for arrays of integers e.g.

foo = [0,1] -> foo[0]=0&foo[1]=1&

so perhaps something like

serialize(value, prefix + '[' + index '') + ']');

@lgalfaso
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.