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

Commit 0c5e1dd

Browse files
jkurzjoshkurz
authored andcommitted
fix(Angular.js): toKeyValue is not serializing null values
Signed-off-by: Josh Kurz <[email protected]>
1 parent f9eb324 commit 0c5e1dd

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/Angular.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1085,14 +1085,16 @@ function parseKeyValue(/**string*/keyValue) {
10851085
function toKeyValue(obj) {
10861086
var parts = [];
10871087
forEach(obj, function(value, key) {
1088-
if (isArray(value)) {
1089-
forEach(value, function(arrayValue) {
1090-
parts.push(encodeUriQuery(key, true) +
1091-
(arrayValue === true ? '' : '=' + encodeUriQuery(arrayValue, true)));
1092-
});
1093-
} else {
1094-
parts.push(encodeUriQuery(key, true) +
1095-
(value === true ? '' : '=' + encodeUriQuery(value, true)));
1088+
if(value !== null){
1089+
if (isArray(value)) {
1090+
forEach(value, function(arrayValue) {
1091+
parts.push(encodeUriQuery(key, true) +
1092+
(arrayValue === true ? '' : '=' + encodeUriQuery(arrayValue, true)));
1093+
});
1094+
} else {
1095+
parts.push(encodeUriQuery(key, true) +
1096+
(value === true ? '' : '=' + encodeUriQuery(value, true)));
1097+
}
10961098
}
10971099
});
10981100
return parts.length ? parts.join('&') : '';

test/AngularSpec.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,11 @@ describe('angular', function() {
457457
expect(toKeyValue({key: [323,'value',true]})).toEqual('key=323&key=value&key');
458458
expect(toKeyValue({key: [323,'value',true, 1234]})).
459459
toEqual('key=323&key=value&key&key=1234');
460-
});
460+
});
461+
462+
it('should not serialize null values', function() {
463+
expect(toKeyValue({nullKey: null, key: 'value'})).toEqual('key=value');
464+
});
461465
});
462466

463467

0 commit comments

Comments
 (0)