Skip to content

Commit

Permalink
fix(schema): fixed circular reference with JSON.stringify in Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Tirke committed Mar 19, 2019
1 parent cedea45 commit 3f614f0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ const VirtualType = require('./VirtualType');

const debug = require('debug')('dynamoose:schema');

function getCircularReplacer () {
const seen = new WeakSet();
return (key, value) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
}


function Schema (obj, options) {
debug('Creating Schema', obj);
Expand Down Expand Up @@ -212,7 +225,7 @@ Schema.prototype.parseDynamo = async function (model, dynamoObj) {
}

if (model.$__) {
model.$__.originalItem = JSON.parse(JSON.stringify(model));
model.$__.originalItem = JSON.parse(JSON.stringify(model, getCircularReplacer()));
}

debug('parseDynamo: %s', model);
Expand Down
13 changes: 13 additions & 0 deletions test/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,19 @@ describe('Model', function () {
});
});

it('should delete a model with update set to true', async () => {
const cat = new Cats.Cat({'id': 1});
const model = await cat.save();
let error, res;
try {
res = await Cats.Cat.delete(model, {'update': true});
} catch (e) {
error = e;
}
should.not.exist(error);
res.id.should.eql(1);
});


// See comments on PR #306 for details on why the test below is commented out

Expand Down

0 comments on commit 3f614f0

Please sign in to comment.