Skip to content

Commit

Permalink
datasource: copy settings object in constructor
Browse files Browse the repository at this point in the history
Fix DataSource constructor to create a shallow copy of the settings
object provided by the caller. This prevents surprising behavior
where changes made to `ds.settings` were picked up by the provided
config object, as observed e.g. in tests of our MongoDB connector.
  • Loading branch information
bajtos committed May 10, 2019
1 parent de4718d commit ef0257e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/datasource.js
Expand Up @@ -126,6 +126,12 @@ function DataSource(name, settings, modelBuilder) {
settings = utils.parseSettings(settings);
}

// Shallow-clone the settings so that updates to `ds.settings`
// do not modify the original object provided via constructor arguments
if (settings) settings = Object.assign({}, settings);
// It's possible to provide settings object via the "name" arg too!
if (typeof name === 'object') name = Object.assign({}, name);

this.modelBuilder = modelBuilder || new ModelBuilder();
this.models = this.modelBuilder.models;
this.definitions = this.modelBuilder.definitions;
Expand Down
9 changes: 9 additions & 0 deletions test/datasource.test.js
Expand Up @@ -9,6 +9,15 @@ const should = require('./init.js');
const DataSource = require('../lib/datasource.js').DataSource;

describe('DataSource', function() {
it('clones settings to prevent surprising changes in passed args', () => {
const config = {connector: 'memory'};

const ds = new DataSource(config);
ds.settings.extra = true;

config.should.eql({connector: 'memory'});
});

it('reports helpful error when connector init throws', function() {
const throwingConnector = {
name: 'loopback-connector-throwing',
Expand Down

0 comments on commit ef0257e

Please sign in to comment.