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

fix(Angular.js): not serializing null values #6964

Merged
merged 1 commit into from
Dec 1, 2014
Merged
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
16 changes: 9 additions & 7 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -1062,14 +1062,16 @@ function parseKeyValue(/**string*/keyValue) {
function toKeyValue(obj) {
var parts = [];
forEach(obj, function(value, key) {
if (isArray(value)) {
forEach(value, function(arrayValue) {
if (value !== null) {
if (isArray(value)) {
forEach(value, function(arrayValue) {
parts.push(encodeUriQuery(key, true) +
(arrayValue === true ? '' : '=' + encodeUriQuery(arrayValue, true)));
});
} else {
parts.push(encodeUriQuery(key, true) +
(arrayValue === true ? '' : '=' + encodeUriQuery(arrayValue, true)));
});
} else {
parts.push(encodeUriQuery(key, true) +
(value === true ? '' : '=' + encodeUriQuery(value, true)));
(value === true ? '' : '=' + encodeUriQuery(value, true)));
}
}
});
return parts.length ? parts.join('&') : '';
Expand Down
8 changes: 8 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,14 @@ describe('angular', function() {
expect(toKeyValue({key: [323,'value',true, 1234]})).
toEqual('key=323&key=value&key&key=1234');
});

it('should not serialize null values', function() {
expect(toKeyValue({nullKey: null, key: 'value'})).toEqual('key=value');
Copy link
Contributor

Choose a reason for hiding this comment

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

This is very different to how jQuery.param() behaves, is there any particular reason for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well my original use of setting a value to null was to delete it, without using delete on the object key.

But yes you are right. Maybe it should do the exact same thing as jQuery, but I don't think it should return a serialized 'null' value.

So this test could be rewritten to be:

expect(toKeyValue({nullKey: null, key: 'value'})).toEqual('nullKey=&key=value')

I would be happy to make this PR use the jQuery method as it seems that it is the excepted format.

Yes this can be accomplished today with just setting nullKey = ''; which is a solution that requires no changes.

Also maybe we should also consider the 'undefined' condition as well.

});

it('should not serialize undefined', function() {
expect(toKeyValue({undefinedKey: undefined, key: 'value'})).toEqual('key=value');
});
});


Expand Down