Skip to content

Commit 712018c

Browse files
committed
fix(ngCookie): convert non-string values to string
Previously, non-string values stored in $cookies would be removed, without warning the user, and causing difficulty debugging. Now, the value is converted to string before being stored, and the value is not dropped. Serialization may be customized using the toString() method of an object's prototype. Closes angular#6151
1 parent 6680b7b commit 712018c

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/ngCookies/cookies.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,13 @@ angular.module('ngCookies', ['ng']).
9797
if (!angular.isString(value)) {
9898
if (angular.isDefined(lastCookies[name])) {
9999
cookies[name] = lastCookies[name];
100+
continue;
100101
} else {
101-
delete cookies[name];
102+
value = '' + value;
103+
cookies[name] = value;
102104
}
103-
} else if (value !== lastCookies[name]) {
105+
}
106+
if (value !== lastCookies[name]) {
104107
$browser.cookies(name, value);
105108
updated = true;
106109
}

test/ngCookies/cookiesSpec.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,25 @@ describe('$cookies', function() {
4545
}));
4646

4747

48-
it('should drop or reset any cookie that was set to a non-string value',
48+
it('should convert non-string values to string',
4949
inject(function($cookies, $browser, $rootScope) {
5050
$cookies.nonString = [1, 2, 3];
5151
$cookies.nullVal = null;
5252
$cookies.undefVal = undefined;
5353
$cookies.preexisting = function() {};
5454
$rootScope.$digest();
55-
expect($browser.cookies()).toEqual({'preexisting': 'oldCookie'});
56-
expect($cookies).toEqual({'preexisting': 'oldCookie'});
55+
expect($browser.cookies()).toEqual({
56+
'preexisting': 'oldCookie',
57+
'nonString': '1,2,3',
58+
'nullVal': 'null',
59+
'undefVal': 'undefined'
60+
});
61+
expect($cookies).toEqual({
62+
'preexisting': 'oldCookie',
63+
'nonString': '1,2,3',
64+
'nullVal': 'null',
65+
'undefVal': 'undefined'
66+
});
5767
}));
5868

5969

0 commit comments

Comments
 (0)