diff --git a/src/ol/source/Vector.js b/src/ol/source/Vector.js index e44191b9fa..c72f777bbe 100644 --- a/src/ol/source/Vector.js +++ b/src/ol/source/Vector.js @@ -282,6 +282,9 @@ class VectorSource extends Source { * instead. A feature will not be added to the source if feature with * the same id is already there. The reason for this behavior is to avoid * feature duplication when using bbox or tile loading strategies. + * Note: this also applies if an {@link module:ol/Collection} is used for features, + * meaning that if a feature with a duplicate id is added in the collection, it will + * be removed from it right away. * @param {import("../Feature.js").default} feature Feature to add. * @api */ @@ -300,6 +303,9 @@ class VectorSource extends Source { const featureKey = getUid(feature); if (!this.addToIndex_(featureKey, feature)) { + if (this.featuresCollection_) { + this.featuresCollection_.remove(feature); + } return; } diff --git a/test/spec/ol/source/vector.test.js b/test/spec/ol/source/vector.test.js index 0223cec14f..d48d2962a3 100644 --- a/test/spec/ol/source/vector.test.js +++ b/test/spec/ol/source/vector.test.js @@ -719,6 +719,19 @@ describe('ol.source.Vector', function() { expect(source.getFeatures().length).to.be(0); }); + it('prevents adding two features with a duplicate id in the collection', function() { + source = new VectorSource({ + features: new Collection() + }); + const feature1 = new Feature(); + feature1.setId('1'); + const feature2 = new Feature(); + feature2.setId('1'); + const collection = source.getFeaturesCollection(); + collection.push(feature1); + collection.push(feature2); + expect(collection.getLength()).to.be(1); + }); }); describe('with a collection of features plus spatial index', function() {