Skip to content

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

remove api "uses" dependency on api-routes (#67650)

dpsutton119487b1ccc1
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 7d97dc5b3dd..e9a08b6bb88 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -147,8 +147,7 @@
metabase.api.settings
metabase.api.util
metabase.api.util.handlers}
- :uses #{api-routes
- config
+ :uses #{config
events
models
settings
diff --git a/src/metabase/api/events.clj b/src/metabase/api/events.clj
deleted file mode 100644
index aa7e4b0daef..00000000000
--- a/src/metabase/api/events.clj
+++ /dev/null
@@ -1,23 +0,0 @@
-(ns metabase.api.events
- "Event handlers for API-related events."
- (:require
- [metabase.api-routes.core :as api-routes]
- [metabase.api.docs :as api.docs]
- [metabase.events.core :as events]
- [metabase.util.log :as log]
- [methodical.core :as methodical]))
-
-(set! *warn-on-reflection* true)
-
-(derive ::api-events :metabase/event)
-(derive :event/api-handler-update ::api-events)
-
-;; Should be triggered in dev mode only to update the openapi.json doc when api schemas change
-;; this then will update the metabase-types for FE via hot-reloading
-(methodical/defmethod events/publish-event! ::api-events
- [_topic _event]
- (try
- (log/debug "Requesting OpenAPI regeneration")
- (api.docs/request-spec-regeneration! api-routes/routes)
- (catch Throwable e
- (log/debug e "Failed to trigger OpenAPI regeneration"))))
diff --git a/src/metabase/api/init.clj b/src/metabase/api/init.clj
index d69c9fcfdc0..b480529971a 100644
--- a/src/metabase/api/init.clj
+++ b/src/metabase/api/init.clj
@@ -1,4 +1,3 @@
(ns metabase.api.init
(:require
- [metabase.api.events]
[metabase.api.settings]))
diff --git a/src/metabase/api/macros.clj b/src/metabase/api/macros.clj
index 3373912dbeb..24d082f78c2 100644
--- a/src/metabase/api/macros.clj
+++ b/src/metabase/api/macros.clj
@@ -713,7 +713,9 @@
;; Publish event for API handler update (e.g., for OpenAPI regeneration)
(when config/is-dev?
(try
- (events/publish-event! :event/api-handler-update {})
+ (events/publish-event! :event/api-handler-update
+ {:api.docs/request-rebuild
+ (requiring-resolve 'metabase.api.docs/request-spec-regeneration!)})
(catch Throwable e
(log/debug e "Failed to publish api-handler-update event"))))))
diff --git a/src/metabase/api_routes/core.clj b/src/metabase/api_routes/core.clj
index 5b55d2a4d61..3e59d8590c5 100644
--- a/src/metabase/api_routes/core.clj
+++ b/src/metabase/api_routes/core.clj
@@ -1,5 +1,6 @@
(ns metabase.api-routes.core
(:require
+ [metabase.api-routes.events]
[metabase.api-routes.routes]
[potemkin :as p]))
diff --git a/src/metabase/api_routes/events.clj b/src/metabase/api_routes/events.clj
new file mode 100644
index 00000000000..f38f73f124b
--- /dev/null
+++ b/src/metabase/api_routes/events.clj
@@ -0,0 +1,25 @@
+(ns metabase.api-routes.events
+ (:require
+ [metabase.api-routes.routes :as routes]
+ [metabase.events.core :as events]
+ [metabase.util.log :as log]
+ [methodical.core :as methodical]))
+
+(derive ::api-events :metabase/event)
+(derive :event/api-handler-update ::api-events)
+
+;; Should be triggered in dev mode only to update the openapi.json doc when api schemas change this then will update
+;; the metabase-types for FE via hot-reloading. This used to live in metabase.api.events. But because it needs a
+;; reference to all routes, that brought a module usage of api-routes. But this is a can of worms because
+;; api-routes (must) use all modules to expose the routes. So now this is a bit split brain: the
+;; api.docs/request-spec-regenerartion function is sent over in the event payload from the api module and the event
+;; handler is registered in this module which has access to all of the routes.
+(methodical/defmethod events/publish-event! ::api-events
+ [_topic {rebuild-fn :api.docs/request-rebuild :as _event}]
+ (try
+ (log/debug "Requesting OpenAPI regeneration")
+ (if rebuild-fn
+ (rebuild-fn routes/routes)
+ (log/debug "No build function provided to rebuild OpenAPI docs"))
+ (catch Throwable e
+ (log/debug e "Failed to trigger OpenAPI regeneration"))))
diff --git a/test/metabase/notification/models_test.clj b/test/metabase/notification/models_test.clj
index 6818aa90bdb..2b52bf93732 100644
--- a/test/metabase/notification/models_test.clj
+++ b/test/metabase/notification/models_test.clj
@@ -106,8 +106,13 @@
(deftest notification-subscription-event-name-test
(mt/with-temp [:model/Notification {n-id :id} {}]
(testing "success path"
- (let [sub-id (t2/insert-returning-pk! :model/NotificationSubscription {:type :notification-subscription/system-event
- :event_name (first (descendants :metabase/event))
+ ;; we derive other keywords into this hierarchy
+ ;; like ::api-events, :metabase.audit-app.events.audit-log/remote-sync-event, etc. This was non-deterministic
+ ;; for a long time
+ (let [random-event (first (filter (comp #{"event"} namespace)
+ (descendants :metabase/event)))
+ sub-id (t2/insert-returning-pk! :model/NotificationSubscription {:type :notification-subscription/system-event
+ :event_name random-event
:notification_id n-id})]
(is (some? (t2/select-one :model/NotificationSubscription sub-id)))))