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

fix($httpParamSerializerJQLike): Follow jQuery parameter serialization l... #11635

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
38 changes: 32 additions & 6 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,56 @@ var JSON_PROTECTION_PREFIX = /^\)\]\}',?\n/;

function paramSerializerFactory(jQueryMode) {

return jQueryMode ? jQueryLikeParamSerializer : paramSerializer;
Copy link
Contributor

Choose a reason for hiding this comment

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

If we are going to do this then we might as wel ditch the paramSerializerFactory altogether and just reference these two serializers directly in their respective providers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They only share serializeValue, maybe move a little more logic to this function but keep it as is?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's already refactored in 2420a0a


function serializeValue(v) {
if (isObject(v)) {
return isDate(v) ? v.toISOString() : toJson(v);
}
return v;
}

return function paramSerializer(params) {
function paramSerializer(params) {
if (!params) return '';
var parts = [];
forEachSorted(params, function(value, key) {
if (value === null || isUndefined(value)) return;
if (isArray(value) || isObject(value) && jQueryMode) {
if (isArray(value)) {
forEach(value, function(v, k) {
var keySuffix = jQueryMode ? '[' + (!isArray(value) ? k : '') + ']' : '';
parts.push(encodeUriQuery(key + keySuffix) + '=' + encodeUriQuery(serializeValue(v)));
parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(serializeValue(v)));
});
} else {
parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(serializeValue(value)));
}
});

return parts.length > 0 ? parts.join('&') : '';
};
return parts.join('&');
}

function jQueryLikeParamSerializer(params) {
if (!params) return '';
var parts = [];
serialize(params, '', true);
return parts.join('&');

function serialize(toSerialize, prefix, topLevel) {
if (toSerialize === null || isUndefined(toSerialize)) return;
if (isArray(toSerialize)) {
forEach(toSerialize, function(value) {
serialize(value, prefix + '[]');
});
} else if (isObject(toSerialize) && !isDate(toSerialize)) {
forEachSorted(toSerialize, function(value, key) {
serialize(value, prefix +
(topLevel ? '' : '[') +
key +
(topLevel ? '' : ']'));
});
} else {
parts.push(encodeUriQuery(prefix) + '=' + encodeUriQuery(serializeValue(toSerialize)));
}
}
}
}

function $HttpParamSerializerProvider() {
Expand Down
10 changes: 9 additions & 1 deletion test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1992,6 +1992,8 @@ describe('$http param serializers', function() {
it('should serialize objects', function() {
expect(defSer({foo: 'foov', bar: 'barv'})).toEqual('bar=barv&foo=foov');
expect(jqrSer({foo: 'foov', bar: 'barv'})).toEqual('bar=barv&foo=foov');
expect(defSer({someDate: new Date('2014-07-15T17:30:00.000Z')})).toEqual('someDate=2014-07-15T17:30:00.000Z');
expect(jqrSer({someDate: new Date('2014-07-15T17:30:00.000Z')})).toEqual('someDate=2014-07-15T17:30:00.000Z');
});

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

it('should serialize objects by repeating param name with [kay] suffix', function() {
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 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
});
});

});