Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Only validate map URLs that changed (#68030)

Nathan Voxlandf0a7607b2bfe
diff --git a/src/metabase/geojson/settings.clj b/src/metabase/geojson/settings.clj
index 7b257bc47d2..82d62aa45b0 100644
--- a/src/metabase/geojson/settings.clj
+++ b/src/metabase/geojson/settings.clj
@@ -95,13 +95,26 @@
(every? (fn [[_ {:keys [url]}]] (valid-geojson-url? url))
geojson))
+(defn- entries-with-new-or-changed-urls
+ "Returns a subset of `new-geojson` containing only entries whose URL is new or different from the stored value.
+ This allows us to skip re-validating URLs that were previously saved and haven't changed."
+ [new-geojson current-geojson]
+ (into {}
+ (filter (fn [[k {:keys [url]}]]
+ (let [current-url (get-in current-geojson [k :url])]
+ (not= url current-url)))
+ new-geojson)))
+
(defn- validate-geojson
- "Throws a 400 if the supplied `geojson` is poorly structured or has an illegal URL/path"
- [geojson]
+ "Throws a 400 if the supplied `geojson` is poorly structured or has an illegal URL/path.
+ Validates structure of all entries, but only validates URLs for entries in `entries-to-validate-urls`.
+ If `entries-to-validate-urls` is nil, validates URLs for all entries."
+ [geojson entries-to-validate-urls]
(when-not (CustomGeoJSONValidator geojson)
(throw (ex-info (tru "Invalid custom GeoJSON") {:status-code 400})))
- (or (valid-geojson-urls? geojson)
- (throw (ex-info (invalid-location-msg) {:status-code 400}))))
+ (when (seq entries-to-validate-urls)
+ (or (valid-geojson-urls? entries-to-validate-urls)
+ (throw (ex-info (invalid-location-msg) {:status-code 400})))))
(defsetting custom-geojson
(deferred-tru "JSON containing information about custom GeoJSON files for use in map visualizations instead of the default US State or World GeoJSON.")
@@ -110,9 +123,13 @@
:getter (fn [] (merge (setting/get-value-of-type :json :custom-geojson) (builtin-geojson)))
:setter (fn [new-value]
;; remove the built-in keys you can't override them and we don't want those to be subject to validation.
- (let [new-value (not-empty (reduce dissoc new-value (keys (builtin-geojson))))]
+ (let [new-value (not-empty (reduce dissoc new-value (keys (builtin-geojson))))
+ current-value (setting/get-value-of-type :json :custom-geojson)
+ ;; Only validate URLs for entries that are new or have changed URLs.
+ changed-entries (when new-value
+ (entries-with-new-or-changed-urls new-value current-value))]
(when new-value
- (validate-geojson new-value))
+ (validate-geojson new-value changed-entries))
(setting/set-value-of-type! :json :custom-geojson new-value)))
:visibility :public
:export? true
diff --git a/test/metabase/geojson/settings_test.clj b/test/metabase/geojson/settings_test.clj
index 29f8fc332fe..c5de1aabcbb 100644
--- a/test/metabase/geojson/settings_test.clj
+++ b/test/metabase/geojson/settings_test.clj
@@ -70,8 +70,8 @@
:region_key nil
:region_name nil}}]
(if should-pass?
- (is (valid? geojson) (str url))
- (is (thrown? clojure.lang.ExceptionInfo (valid? geojson)) (str url))))))))
+ (is (valid? geojson geojson) (str url))
+ (is (thrown? clojure.lang.ExceptionInfo (valid? geojson geojson)) (str url))))))))
(deftest custom-geojson-disallow-overriding-builtins-test
(testing "We shouldn't let people override the builtin GeoJSON and put weird stuff in there; ignore changes to them"
@@ -119,3 +119,39 @@
(testing "Env var value SHOULD come back with [[setting/user-readable-values-map]] -- should be READABLE."
(is (= expected-value
(get (setting/user-readable-values-map #{:public}) :custom-geojson)))))))))))
+
+(deftest unchanged-url-validation-test
+ (testing "Unchanged URLs should not be re-validated when saving other changes (#44353)"
+ (mt/with-temporary-setting-values [custom-geojson nil]
+ (let [unreachable-url "http://hostname.invalid/map.geojson"
+ existing-entry {:existing {:name "Existing Map"
+ :url unreachable-url
+ :region_key nil
+ :region_name nil}}
+ new-entry {:new-map {:name "New Map"
+ :url "https://example.com/valid.geojson"
+ :region_key nil
+ :region_name nil}}]
+ (setting/set-value-of-type! :json :custom-geojson existing-entry)
+ (testing "Adding a new entry with a valid URL should succeed even if an existing entry has an unreachable URL"
+ (geojson.settings/custom-geojson! (merge existing-entry new-entry)))
+ (testing "The new entry was saved correctly"
+ (is (= (merge (@#'geojson.settings/builtin-geojson) existing-entry new-entry)
+ (geojson.settings/custom-geojson))))
+ (testing "Updating properties other than URL on an existing entry should succeed"
+ (let [updated-existing {:existing {:name "Updated Existing Map"
+ :url unreachable-url
+ :region_key "NEW_KEY"
+ :region_name "New Name"}}]
+ (geojson.settings/custom-geojson! (merge updated-existing new-entry))
+ (is (= (merge (@#'geojson.settings/builtin-geojson) updated-existing new-entry)
+ (geojson.settings/custom-geojson)))))
+ (testing "Changing the URL of an existing entry SHOULD trigger validation"
+ (let [changed-url-entry {:existing {:name "Updated Existing Map"
+ :url "http://also.invalid/map.geojson"
+ :region_key "NEW_KEY"
+ :region_name "New Name"}}]
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid GeoJSON"
+ (geojson.settings/custom-geojson! (merge changed-url-entry new-entry))))))))))