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

Commit 39d94c7

Browse files
committed
fix(Angular.js): toKeyValue is not serializing null values
Signed-off-by: Josh Kurz <jkurz25@gmail.com>
1 parent 764fa86 commit 39d94c7

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/Angular.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -1062,14 +1062,16 @@ function parseKeyValue(/**string*/keyValue) {
10621062
function toKeyValue(obj) {
10631063
var parts = [];
10641064
forEach(obj, function(value, key) {
1065-
if (isArray(value)) {
1066-
forEach(value, function(arrayValue) {
1065+
if (value !== null) {
1066+
if (isArray(value)) {
1067+
forEach(value, function(arrayValue) {
1068+
parts.push(encodeUriQuery(key, true) +
1069+
(arrayValue === true ? '' : '=' + encodeUriQuery(arrayValue, true)));
1070+
});
1071+
} else {
10671072
parts.push(encodeUriQuery(key, true) +
1068-
(arrayValue === true ? '' : '=' + encodeUriQuery(arrayValue, true)));
1069-
});
1070-
} else {
1071-
parts.push(encodeUriQuery(key, true) +
1072-
(value === true ? '' : '=' + encodeUriQuery(value, true)));
1073+
(value === true ? '' : '=' + encodeUriQuery(value, true)));
1074+
}
10731075
}
10741076
});
10751077
return parts.length ? parts.join('&') : '';

test/AngularSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,14 @@ describe('angular', function() {
491491
expect(toKeyValue({key: [323,'value',true, 1234]})).
492492
toEqual('key=323&key=value&key&key=1234');
493493
});
494+
495+
it('should not serialize null values', function() {
496+
expect(toKeyValue({nullKey: null, key: 'value'})).toEqual('key=value');
497+
});
498+
499+
it('should not serialize undefined', function() {
500+
expect(toKeyValue({undefinedKey: undefined, key: 'value'})).toEqual('key=value');
501+
});
494502
});
495503

496504

0 commit comments

Comments
 (0)