diff --git a/core/server/settings/cache.js b/core/server/settings/cache.js index 10bdaf3fc3..9911b974ea 100644 --- a/core/server/settings/cache.js +++ b/core/server/settings/cache.js @@ -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; diff --git a/core/test/unit/settings/cache_spec.js b/core/test/unit/settings/cache_spec.js new file mode 100644 index 0000000000..b59c1499ff --- /dev/null +++ b/core/test/unit/settings/cache_spec.js @@ -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); + }); +});