Skip to content

Commit

Permalink
🐛 fix settings cache (#8506)
Browse files Browse the repository at this point in the history
closes #8505

- cache.get(..) auto converted "1" to integer
  • Loading branch information
kirrg001 authored and aileen committed Jun 4, 2017
1 parent b081ae3 commit a61e6e7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/server/settings/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ module.exports = {

// Default behaviour is to try to resolve the value and return that
try {
// CASE: if a string contains a number e.g. "1", JSON.parse will auto convert into integer
if (settingsCache[key].value.match(/^\d+$/)) {
return settingsCache[key].value;
}

return JSON.parse(settingsCache[key].value);
} catch (err) {
return settingsCache[key].value;
Expand Down
21 changes: 21 additions & 0 deletions core/test/unit/settings/cache_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var rewire = require('rewire'),
should = require('should'),
cache = rewire('../../../server/settings/cache');

should.equal(true, true);

describe('UNIT: settings cache', function () {
it('does not auto convert string into number', function () {
cache.set('key1', {value: '1'});
(typeof cache.get('key1')).should.eql('string');
});

it('stringified JSON get\'s parsed', function () {
cache.set('key2', {value: '{"a":"1","b":"hallo","c":{"d":[]},"e":2}'});
(typeof cache.get('key2')).should.eql('object');
cache.get('key2').a.should.eql('1');
cache.get('key2').b.should.eql('hallo');
cache.get('key2').c.should.eql({d: []});
cache.get('key2').e.should.eql(2);
});
});

0 comments on commit a61e6e7

Please sign in to comment.