From ae0712b657f769e299625351d22ca30e2d1ab288 Mon Sep 17 00:00:00 2001 From: Asheem Mamoowala Date: Tue, 1 Aug 2017 16:43:11 -0700 Subject: [PATCH] Clone `layerObject` in `Style#addLayer` to prevent mutation of user data --- src/style/style.js | 1 + test/unit/style/style.test.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/style/style.js b/src/style/style.js index 389ff5f45c0..9acb23e46d3 100644 --- a/src/style/style.js +++ b/src/style/style.js @@ -497,6 +497,7 @@ class Style extends Evented { if (typeof layerObject.source === 'object') { this.addSource(id, layerObject.source); + layerObject = util.clone(layerObject); layerObject = (util.extend(layerObject, {source: id}): any); } diff --git a/test/unit/style/style.test.js b/test/unit/style/style.test.js index 1aa99c0ceaf..86c5a9c79bd 100644 --- a/test/unit/style/style.test.js +++ b/test/unit/style/style.test.js @@ -723,6 +723,23 @@ test('Style#addLayer', (t) => { }); }); + t.test('#4040 does not mutate source property when provided inline', (t) => { + const style = new Style(createStyleJSON()); + style.on('style.load', () => { + const source = { + "type": "geojson", + "data": { + "type": "Point", + "coordinates": [ 0, 0] + } + }; + const layer = {id: 'inline-source-layer', type: 'circle', source: source }; + style.addLayer(layer); + t.deepEqual(layer.source, source); + t.end(); + }); + }); + t.test('reloads source', (t) => { const style = new Style(util.extend(createStyleJSON(), { "sources": {