From 2948c144726307a421688562c1ef8d10609ec8fe Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Wed, 31 Aug 2016 12:29:41 -0400 Subject: [PATCH] datastore: deep clone user objects (#1552) * datastore: deep clone user objects * add notStrictEqual test --- packages/datastore/src/entity.js | 3 +++ packages/datastore/test/entity.js | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/packages/datastore/src/entity.js b/packages/datastore/src/entity.js index 57869aabd65..095d574c1a1 100644 --- a/packages/datastore/src/entity.js +++ b/packages/datastore/src/entity.js @@ -23,6 +23,7 @@ var arrify = require('arrify'); var createErrorClass = require('create-error-class'); +var extend = require('extend'); var is = require('is'); var entity = module.exports; @@ -285,6 +286,8 @@ function encodeValue(value) { if (is.object(value)) { if (!is.empty(value)) { + value = extend(true, {}, value); + for (var prop in value) { if (value.hasOwnProperty(prop)) { value[prop] = entity.encodeValue(value[prop]); diff --git a/packages/datastore/test/entity.js b/packages/datastore/test/entity.js index feea6cae641..bcdd69a5581 100644 --- a/packages/datastore/test/entity.js +++ b/packages/datastore/test/entity.js @@ -21,6 +21,7 @@ var deepStrictEqual = require('deep-strict-equal'); assert.deepStrictEqual = assert.deepStrictEqual || function() { return assert(deepStrictEqual.apply(this, arguments)); }; +var extend = require('extend'); var Datastore = require('../'); @@ -430,6 +431,23 @@ describe('entity', function() { assert.deepEqual(entity.encodeValue(value), expectedValueProto); }); + it('should clone an object', function() { + var value = { + a: { + b: { + obj: true + } + } + }; + + var originalValue = extend(true, {}, value); + + var encodedValue = entity.encodeValue(value); + + assert.deepEqual(value, originalValue); + assert.notStrictEqual(value, encodedValue); + }); + it('should encode an empty object', function() { var value = {};