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

fix($httpParamSerializerJQLike): call functions as jQuery does #16139

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ function $HttpParamSerializerJQLikeProvider() {
(topLevel ? '' : ']'));
});
} else {
if (isFunction(toSerialize)) {
Copy link
Member

@gkalpak gkalpak Aug 1, 2017

Choose a reason for hiding this comment

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

EDIT: This comment is now obsolete.

Merge with the else above for consistency:

  ...
} else if (isFunction(toSerialize)) {
  ...
} else {
  ...
}

toSerialize = toSerialize();
}
parts.push(encodeUriQuery(prefix) + '=' +
(toSerialize == null ? '' : encodeUriQuery(serializeValue(toSerialize))));
}
Expand Down
24 changes: 24 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2364,17 +2364,41 @@ describe('$http param serializers', function() {
expect(decodeURIComponent(jqrSer({a: 'b', foo: ['bar', 'baz']}))).toEqual('a=b&foo[]=bar&foo[]=baz');
});

it('should serialize arrays with functions', function() {
expect(jqrSer({foo: [valueFn('bar')]})).toEqual('foo%5B%5D=bar');
Copy link
Member

Choose a reason for hiding this comment

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

Where's the decoded comment (foo[]=bar)? 😁

});

it('should serialize arrays with functions inside objects', function() {
expect(jqrSer({foo: {bar: [valueFn('baz')]}})).toEqual('foo%5Bbar%5D%5B%5D=baz'); // foo[bar][]=baz
});

it('should serialize objects by repeating param name with [key] suffix', function() {
expect(jqrSer({a: 'b', foo: {'bar': 'barv', 'baz': 'bazv'}})).toEqual('a=b&foo%5Bbar%5D=barv&foo%5Bbaz%5D=bazv');
//a=b&foo[bar]=barv&foo[baz]=bazv
});

it('should serialize objects with function properties', function() {
expect(jqrSer({a: valueFn('b')})).toEqual('a=b');
});

it('should serialize objects with function properties returning an object by repeating param name with [key] suffix', function() {
Copy link
Member

Choose a reason for hiding this comment

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

Inaccurate description.

expect(jqrSer({a: valueFn({b: 'c'})})).toEqual('a=%7B%22b%22:%22c%22%7D'); //a={"b":"c"}
});
Copy link
Member

Choose a reason for hiding this comment

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

These two tests are testing more (independent) things than necessary. They should focus on the function part. The rest is irrelevant.


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%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 nested objects with function properties', function() {
expect(jqrSer({foo: {bar: valueFn('barv')}})).toEqual('foo%5Bbar%5D=barv'); //foo[bar]=barv
});

it('should serialize nested objects with function properties returning an object', function() {
expect(jqrSer({foo: {bar: valueFn({ bav: 'barv'})}})).toEqual('foo%5Bbar%5D=%7B%22bav%22:%22barv%22%7D'); //foo[bar]={"bav":"barv"}
Copy link
Member

Choose a reason for hiding this comment

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

Extra whitespace in { bav. 😱

});

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');
Expand Down