summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/mbgl/style/style.cpp10
-rw-r--r--test/style/style.test.cpp21
2 files changed, 31 insertions, 0 deletions
diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp
index 867a03da79..44843ce8b7 100644
--- a/src/mbgl/style/style.cpp
+++ b/src/mbgl/style/style.cpp
@@ -136,6 +136,16 @@ void Style::setJSON(const std::string& json) {
}
void Style::addSource(std::unique_ptr<Source> source) {
+ //Guard against duplicate source ids
+ auto it = std::find_if(sources.begin(), sources.end(), [&](const auto& existing) {
+ return existing->getID() == source->getID();
+ });
+
+ if (it != sources.end()) {
+ std::string msg = "Source " + source->getID() + " already exists";
+ throw std::runtime_error(msg.c_str());
+ }
+
source->baseImpl->setObserver(this);
sources.emplace_back(std::move(source));
}
diff --git a/test/style/style.test.cpp b/test/style/style.test.cpp
index 41f12f6ff7..89c5c4ce6f 100644
--- a/test/style/style.test.cpp
+++ b/test/style/style.test.cpp
@@ -3,10 +3,13 @@
#include <mbgl/style/style.hpp>
#include <mbgl/style/source_impl.hpp>
+#include <mbgl/style/sources/vector_source.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/run_loop.hpp>
+#include <memory>
+
using namespace mbgl;
using namespace mbgl::style;
@@ -113,3 +116,21 @@ TEST(Style, Properties) {
ASSERT_EQ(0, style.getDefaultZoom());
ASSERT_EQ(0, style.getDefaultPitch());
}
+
+TEST(Style, DuplicateSource) {
+ util::RunLoop loop;
+
+ StubFileSource fileSource;
+ Style style { fileSource, 1.0 };
+
+ style.setJSON(util::read_file("test/fixtures/resources/style-unused-sources.json"));
+
+ style.addSource(std::make_unique<VectorSource>("sourceId", "mapbox://mapbox.mapbox-terrain-v2"));
+
+ try {
+ style.addSource(std::make_unique<VectorSource>("sourceId", "mapbox://mapbox.mapbox-terrain-v2"));
+ FAIL() << "Should not have been allowed to add a duplicate source id";
+ } catch (std::runtime_error) {
+ //Expected
+ }
+}