Skip to content

Commit

Permalink
Clone layerObject in Style#addLayer to prevent mutation of user data
Browse files Browse the repository at this point in the history
  • Loading branch information
Asheem Mamoowala committed Aug 1, 2017
1 parent 1fe5c95 commit ae0712b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/style/style.js
Expand Up @@ -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);
}

Expand Down
17 changes: 17 additions & 0 deletions test/unit/style/style.test.js
Expand Up @@ -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": {
Expand Down

0 comments on commit ae0712b

Please sign in to comment.