Skip to content

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

Validate dashboard ops against a schema the compiler owns (#85810)

Chris Truter1595c5ab47c4
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 1cdd6edda35..c3a2fe65578 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -1734,10 +1734,11 @@
metabot.toolkit
{:team "Metabot"
- :api #{metabase.metabot.toolkit.dashboard-ops}
+ :api #{metabase.metabot.toolkit.dashboard-op-schema metabase.metabot.toolkit.dashboard-ops}
:uses #{dashboards
lib
parameters
+ parameters.schema
util}}
metrics
diff --git a/src/metabase/mcp/v2/dashboard_errors.clj b/src/metabase/mcp/v2/dashboard_errors.clj
index e1362341a6e..45d38f4b8cf 100644
--- a/src/metabase/mcp/v2/dashboard_errors.clj
+++ b/src/metabase/mcp/v2/dashboard_errors.clj
@@ -14,31 +14,23 @@
(def ^:private details
"For each error code in [[dashboard-ops/errors]], a function from the error data to the message explaining it.
The message has no op prefix."
- {:missing-op
- (fn [_] (message/raw "missing `op` — see the tool description for the supported list."))
-
- :unknown-op
- (fn [_] (message/raw "unknown op — see the tool description for the supported list."))
-
- :invalid-new-id
- (fn [_] (message/raw "`id` must be a negative integer — negative ids mark rows to create."))
+ {;; MCP's registry checks the same op schema first, so MCP's own calls never reach this. It is worded for any
+ ;; caller of the toolkit that skips that check, and because every code needs a wording.
+ :invalid-op
+ (fn [{:keys [explanation]}]
+ (message/msg ["invalid op: %s — see the tool description for each op's arguments."]
+ (common/humanize-detail explanation)))
:duplicate-new-id
(fn [{:keys [id]}]
(message/msg ["id %s is already used in this batch — give each new row its own negative id."] id))
- :missing-dashcard-id
- (fn [_] (message/raw "missing `dashcard_id`."))
-
:unknown-dashcard
(fn [{:keys [dashcard-id]}] (message/msg ["no dashcard with id %s on this dashboard."] dashcard-id))
:unknown-card
(fn [{:keys [card-id]}] (message/msg ["no card with id %s that you can read."] card-id))
- :missing-tab-id
- (fn [_] (message/raw "missing `tab_id`."))
-
:unknown-tab
(fn [{:keys [tab-id]}] (message/msg ["no tab with id %s on this dashboard."] tab-id))
@@ -64,9 +56,6 @@
"Pass \"tab\" on the add op, or use \"move\" with \"tab\" for cards already placed.")]
(common/list-message dashcard-ids)))
- :missing-parameter-id
- (fn [_] (message/raw "missing `parameter_id`."))
-
:unknown-parameter
(fn [{:keys [parameter-id]}] (message/msg ["no parameter with id %s on this dashboard."] parameter-id))
@@ -100,6 +89,12 @@
"text, heading, or iframe card's own {{placeholder}}.")]
dashcard-id))
+ :mapping-card-not-on-dashcard
+ (fn [{:keys [dashcard-id card-id]}]
+ (message/msg [(str "card %s is not on dashcard %s — a mapping's card_id must be the dashcard's "
+ "card or one of its series. Omit it to filter the dashcard's card.")]
+ card-id dashcard-id))
+
:unknown-template-tag
(fn [{:keys [card-id tag tags]}]
(if (seq tags)
@@ -162,13 +157,13 @@
;; A bare raw message renders quoted, so wrap it to keep it as text.
(message/msg ["%s"] detail))))
-(defn compile-ops
- "Runs [[dashboard-ops/compile-ops]] and rethrows its errors as MCP teaching errors.
- Each teaching error keeps the compiler's error as its cause.
+(defn do-with-teaching-errors
+ "Calls `thunk`, and rethrows any error from [[dashboard-ops/errors]] it throws as an MCP teaching error.
+ Each teaching error keeps the original error as its cause.
Other exceptions pass through unchanged."
- [current ops cards]
+ [thunk]
(try
- (dashboard-ops/compile-ops current ops cards)
+ (thunk)
(catch clojure.lang.ExceptionInfo e
(throw (if-let [data (dashboard-ops/error-data e)]
(common/message-ex-info (error-message data) {:status-code 400} e)
diff --git a/src/metabase/mcp/v2/tools/dashboard.clj b/src/metabase/mcp/v2/tools/dashboard.clj
index 2095882049f..1b0b651261f 100644
--- a/src/metabase/mcp/v2/tools/dashboard.clj
+++ b/src/metabase/mcp/v2/tools/dashboard.clj
@@ -29,6 +29,7 @@
[metabase.mcp.v2.resolve :as v2.resolve]
[metabase.mcp.v2.write :as v2.write]
[metabase.metabot.scope :as metabot.scope]
+ [metabase.metabot.toolkit.dashboard-op-schema :as dashboard-op-schema]
[metabase.metabot.toolkit.dashboard-ops :as dashboard-ops]
[metabase.models.interface :as mi]
[metabase.util :as u]
@@ -45,14 +46,6 @@
(-> (v2.resolve/resolve-and-read :model/Dashboard id-or-eid)
(t2/hydrate [:dashcards :series :card] :tabs)))
-(defn- referenced-card-ids
- "Every card id the compiled payload may need metadata for: those already on the dashboard, plus
- any named by an op."
- [dash ops]
- (into (set (keep :card_id (:dashcards dash)))
- (mapcat dashboard-ops/card-refs)
- ops))
-
(defn- fetch-cards
"The `{card-id card}` map for `ids`, behind read checks.
It omits a card the caller cannot read, so an op naming one fails the compiler's `:unknown-card` check.
@@ -116,236 +109,6 @@
;;; ------------------------------------------------- Schema -------------------------------------------------------
-(def ^:private position-schema
- [:map {:closed true :description "Grid slot; omit to autoplace."}
- [:row [:int {:min 0 :description "Zero-based row."}]]
- [:col [:int {:min 0 :max 23 :description "Zero-based column; the grid is 24 wide."}]]])
-
-(def ^:private size-schema
- [:map {:closed true :description "Size in grid cells."}
- [:size_x [:int {:min 1 :max 24 :description "Width in columns (grid is 24 wide)."}]]
- [:size_y [:int {:min 1 :description "Height in rows."}]]])
-
-(def ^:private new-id-schema
- [:int {:max -1
- :description "Negative id you choose for the new row (-1, -2, …); later ops may reference it."}])
-
-(def ^:private tab-ref-schema
- [:maybe [:int {:description "Tab id; negative ids from this call work. Omit when the dashboard has no tabs."}]])
-
-(def ^:private inline-parameters-schema
- [:maybe [:sequential [:string {:description "Parameter id to render on this card, not the header."}]]])
-
-(def ^:private dashcard-ref-schema
- [:int {:description "Dashcard id; negative ids from this call work."}])
-
-(def ^:private parameter-id-schema
- [:string {:min 1 :description "Parameter id — a short string you choose and reuse in later ops."}])
-
-;; `add_parameter` and `update_parameter` write REST parameter properties straight through, so the
-;; camelCase names (`isMultiSelect`, `filteringParameters`, `sectionId`) are preserved verbatim.
-(def ^:private parameter-name-schema
- [:string {:min 1 :description "Label shown on the dashboard."}])
-
-;; `:name` is required on `add_parameter` and optional on `update_parameter`, so it is not in
-;; `parameter-fields` — each op supplies its own entry.
-(def ^:private parameter-fields
- [[:type {:optional true}
- [:maybe [:string {:description (str "Parameter type, e.g. \"string/=\", \"string/contains\", "
- "\"number/=\", \"number/between\", \"date/all-options\", "
- "\"date/relative\", \"id\", \"temporal-unit\".")}]]]
- [:sectionId {:optional true}
- [:maybe [:string {:description "Widget section, e.g. \"string\", \"number\", \"date\", \"id\"."}]]]
- [:default {:optional true}
- [:maybe [:any {:description "Default value: a scalar, or an array for multi-select."}]]]
- [:required {:optional true}
- [:maybe [:boolean {:description "When true the dashboard needs a value to view; pair with a default."}]]]
- [:isMultiSelect {:optional true}
- [:maybe [:boolean {:description "Whether the widget accepts several values."}]]]
- [:temporal_units {:optional true}
- [:maybe [:sequential [:string {:description "Allowed unit for a \"temporal-unit\" parameter, e.g. \"month\"."}]]]]
- [:values_query_type {:optional true}
- [:maybe [:enum {:description "Value picker behavior: dropdown list, search box, or free text."}
- "list" "search" "none"]]]
- [:values_source_type {:optional true}
- [:maybe [:enum {:description "Picker value source; omit to use the connected field's values."}
- "static-list" "card"]]]
- [:values_source_config {:optional true}
- [:maybe [:map {:description (str "Config for values_source_type: {\"values\": [...]} for "
- "static-list, or {\"card_id\": n, \"value_field\": target} for card.")}]]]
- [:filteringParameters {:optional true}
- [:maybe [:sequential [:string {:description "Id of a parameter whose value narrows this one (linked filter)."}]]]]])
-
-(defn- op-map
- "One closed op entry for [[op-schema]]: the `op` discriminator plus `fields`."
- [op-name description fields]
- [op-name
- (into [:map {:closed true :description description}
- [:op [:= op-name]]]
- fields)])
-
-(def ^:private op-schema
- [:multi {:dispatch :op
- :description "One editor operation; `op` selects which."}
- (op-map "add_card" "Place a saved question, model, or metric on the dashboard."
- [[:id new-id-schema]
- [:card_id [:int {:description "Numeric id of the saved question, model, or metric to place."}]]
- [:tab {:optional true} tab-ref-schema]
- [:position {:optional true} [:maybe position-schema]]
- [:size {:optional true} [:maybe size-schema]]
- [:series {:optional true}
- [:maybe [:sequential [:int {:description "Numeric id of a card to overlay on this one as an extra series."}]]]]
- [:inline_parameters {:optional true} inline-parameters-schema]])
- (op-map "add_text" "Add a markdown text card."
- [[:id new-id-schema]
- [:markdown [:string {:description "Markdown body of the text card."}]]
- [:tab {:optional true} tab-ref-schema]
- [:position {:optional true} [:maybe position-schema]]
- [:size {:optional true} [:maybe size-schema]]
- [:inline_parameters {:optional true} inline-parameters-schema]])
- (op-map "add_heading" "Add a section heading — a full-width label with no card background."
- [[:id new-id-schema]
- [:text [:string {:description "Heading text; plain text, not markdown."}]]
- [:tab {:optional true} tab-ref-schema]
- [:position {:optional true} [:maybe position-schema]]
- [:size {:optional true} [:maybe size-schema]]
- [:inline_parameters {:optional true} inline-parameters-schema]])
- (op-map "add_link" "Add a link card. Pass exactly one of `url` or `entity`."
- [[:id new-id-schema]
- [:url {:optional true} [:maybe [:string {:description "External URL to link to."}]]]
- [:entity {:optional true}
- [:maybe [:map {:closed true :description "A Metabase entity to link to, instead of `url`."}
- [:type [:enum {:description "Kind of entity being linked."}
- "dashboard" "card" "dataset" "collection" "database" "table"]]
- [:id [:int {:description "Numeric id of that entity."}]]]]]
- [:tab {:optional true} tab-ref-schema]
- [:position {:optional true} [:maybe position-schema]]
- [:size {:optional true} [:maybe size-schema]]
- [:inline_parameters {:optional true} inline-parameters-schema]])
- (op-map "add_iframe" "Embed an external page. The host must be allowed by the instance's iframe settings."
- [[:id new-id-schema]
- [:src [:string {:description "URL to embed, or a full <iframe> tag."}]]
- [:tab {:optional true} tab-ref-schema]
- [:position {:optional true} [:maybe position-schema]]
- [:size {:optional true} [:maybe size-schema]]
- [:inline_parameters {:optional true} inline-parameters-schema]])
- (op-map "add_action" "Add a button that runs a saved action."
- [[:id new-id-schema]
- [:action_id [:int {:description "Numeric id of the saved action to run."}]]
- [:label {:optional true} [:maybe [:string {:description "Button text. Defaults to the action's own name."}]]]
- [:display {:optional true}
- [:maybe [:enum {:description "\"button\" (default) runs on click; \"form\" renders the action's fields inline."}
- "button" "form"]]]
- [:tab {:optional true} tab-ref-schema]
- [:position {:optional true} [:maybe position-schema]]
- [:size {:optional true} [:maybe size-schema]]])
- (op-map "duplicate_card"
- (str "Copy a dashcard with its card, visualization settings, and parameter mappings; "
- "same size, its own slot.")
- [[:id new-id-schema]
- [:dashcard_id dashcard-ref-schema]
- [:tab {:optional true} tab-ref-schema]
- [:position {:optional true} [:maybe position-schema]]])
- (op-map "replace_card"
- (str "Point a dashcard at a different card, keeping slot and size; series, parameter "
- "mappings, and visualization settings reset, as the editor does.")
- [[:dashcard_id dashcard-ref-schema]
- [:card_id [:int {:description "Numeric id of the card to show instead."}]]])
- (op-map "move" "Move a dashcard to a different slot, a different tab, or both."
- [[:dashcard_id dashcard-ref-schema]
- [:tab {:optional true} tab-ref-schema]
- [:position {:optional true} [:maybe position-schema]]])
- (op-map "resize" "Resize a dashcard in place."
- [[:dashcard_id dashcard-ref-schema]
- [:size size-schema]])
- (op-map "remove" "Delete a dashcard from the dashboard. The underlying card is untouched."
- [[:dashcard_id dashcard-ref-schema]])
- (op-map "set_series"
- "Replace a dashcard's overlaid series with this ordered list; pass an empty list to clear them."
- [[:dashcard_id dashcard-ref-schema]
- [:card_ids [:sequential [:int {:description "Numeric id of a card to overlay, in draw order."}]]]])
- (op-map "patch_dashcard"
- (str "Merge content settings into a dashcard — visualization settings, click behavior, "
- "column settings, a link card's target. Layout/identity keys (row, col, size_x, "
- "size_y, dashboard_tab_id, card_id, action_id, series, id) are rejected; use move, "
- "resize, replace_card, or set_series.")
- [[:dashcard_id dashcard-ref-schema]
- [:patch [:map {:description (str "Dashcard properties to merge; `visualization_settings` "
- "merges key by key rather than replacing the map.")}]]])
- (op-map "add_tab" (str "Add a tab. Existing cards are adopted only when the save ends with "
- "exactly one tab; adding several at once leaves them orphaned — `move` "
- "them onto a tab in the same call.")
- [[:id new-id-schema]
- [:name [:string {:min 1 :description "Tab label."}]]])
- (op-map "rename_tab" "Rename an existing tab."
- [[:tab_id [:int {:description "Id of the tab to rename (negative ids from this call work)."}]]
- [:name [:string {:min 1 :description "New tab label."}]]])
- (op-map "move_tab" "Reorder the tab strip by moving one tab to a new position."
- [[:tab_id [:int {:description "Id of the tab to move."}]]
- [:index [:int {:min 0 :description "Zero-based position to move it to, counting the other tabs only."}]]])
- (op-map "duplicate_tab" "Copy a tab and every card on it."
- [[:id new-id-schema]
- [:tab_id [:int {:description "Id of the tab to copy."}]]])
- (op-map "remove_tab" "Delete a tab and every card on it."
- [[:tab_id [:int {:description "Id of the tab to delete."}]]])
- (op-map "add_parameter"
- (str "Add a filter or parameter widget to the dashboard. It does nothing until "
- "`wire_parameter` connects it to at least one card.")
- (into [[:parameter_id parameter-id-schema]
- [:name parameter-name-schema]]
- parameter-fields))
- (op-map "update_parameter"
- (str "Change properties of an existing parameter. Only the properties you pass change; "
- "the rest are left alone. Passing null does not clear a property — it is treated as "
- "omitted — so to remove one, name it in `clear`.")
- (into [[:parameter_id parameter-id-schema]
- [:name {:optional true} [:maybe parameter-name-schema]]
- [:clear {:optional true}
- [:maybe [:sequential
- [:enum {:description (str "Property names to remove from this parameter "
- "(default, filteringParameters, "
- "values_source_config, values_source_type, "
- "sectionId, temporal_units). Null cannot say this: "
- "strict clients fill every unset property with "
- "null, so nulls are treated as omitted.")}
- "default" "filteringParameters" "values_source_config"
- "values_source_type" "sectionId" "temporal_units"]]]]]
- parameter-fields))
- (op-map "remove_parameter"
- (str "Delete a parameter with its card mappings, inline placements, and linked-filter "
- "references; dependent subscriptions are archived and their owners emailed.")
- [[:parameter_id parameter-id-schema]])
- (op-map "move_parameter" "Reorder a parameter in the header, or move it onto a card. Pass exactly one of `index` or `dashcard_id`."
- [[:parameter_id parameter-id-schema]
- [:index {:optional true}
- [:maybe [:int {:min 0 :description "Zero-based position in the header, counting the other parameters only."}]]]
- [:dashcard_id {:optional true}
- [:maybe [:int {:description "Render the parameter on this dashcard instead of in the header."}]]]])
- (op-map "wire_parameter"
- (str "Connect a parameter to a card so the widget actually filters it. Pass exactly one "
- "of `target_field`, `target_tag`, or `target`.")
- [[:parameter_id parameter-id-schema]
- [:dashcard_id dashcard-ref-schema]
- [:target_field {:optional true}
- [:maybe [:int {:description (str "Numeric field id to filter on — the usual choice; the "
- "server derives the mapping from the card's query.")}]]]
- [:target_tag {:optional true}
- [:maybe [:string {:description (str "Template tag name on a native-SQL card; the server "
- "derives the mapping from the tag's type (field filter "
- "vs raw variable).")}]]]
- [:target {:optional true}
- [:maybe [:sequential [:any {:description (str "Raw mapping-target clause, e.g. "
- "[\"dimension\", [\"template-tag\", \"category\"]] or "
- "[\"text-tag\", \"name\"] for a {{name}} placeholder in a "
- "text/heading/iframe card. Advanced; prefer target_field.")}]]]]
- [:autowire {:optional true}
- [:maybe [:boolean {:description (str "With target_field, also map every other card exposing "
- "the same field; cards that don't are skipped silently.")}]]]])
- (op-map "unwire_parameter" "Disconnect a parameter from one card, or from every card when `dashcard_id` is omitted."
- [[:parameter_id parameter-id-schema]
- [:dashcard_id {:optional true}
- [:maybe [:int {:description "Dashcard to disconnect. Omit to disconnect the parameter everywhere."}]]]])])
-
(def ^:private dashboard-write-args-schema
[:map {:closed true}
[:method
@@ -381,7 +144,7 @@
[:ops {:optional true}
[:maybe [:sequential {:description (str "Editor operations, applied in order. The call saves them "
"atomically, together with the new dashboard on create.")}
- op-schema]]]])
+ dashboard-op-schema/op-schema]]]])
(def ^:private dashboard-write-entry
{:create-required [:name]
@@ -407,21 +170,16 @@
(assoc (dashboard-attrs body)
:collection_id (v2.resolve/resolve-collection-id-or-personal (:collection_id body))))
-(defn- compact-op
- "Strip null-valued keys from one op. Strict MCP clients send every declared property, filling
- the ones they do not set with null; the ops that distinguish absent from null — `move` and
- `duplicate_card` on `tab`, `move_parameter` on `index`/`dashcard_id`, `unwire_parameter` on
- `dashcard_id` — would otherwise read a filled-in null as a deliberate value."
- [op]
- (into {} (remove (comp nil? val)) op))
-
(defn- apply-ops!
"Compile `ops` against `dash` and save, returning the projection row. With `validate-only?` the
would-be row is built from the compiled payload instead and nothing is written."
[dash ops attrs validate-only?]
- (let [ops (mapv compact-op ops)
- cards (fetch-cards (referenced-card-ids dash ops))
- payload (normalize-parameters (dashboard-errors/compile-ops dash ops cards))]
+ (let [{:keys [payload cards]}
+ (dashboard-errors/do-with-teaching-errors
+ #(let [ops (dashboard-ops/prepare-ops ops)
+ cards (fetch-cards (dashboard-ops/referenced-card-ids dash ops))]
+ {:payload (normalize-parameters (dashboard-ops/compile-ops dash ops cards))
+ :cards cards}))]
(validate-payload! (merge attrs payload))
(if validate-only?
(dry-run-row dash attrs payload cards)
@@ -448,7 +206,8 @@
per-display default sizes, and layout conventions. validate_only: true returns the layout the ops would
produce without writing — but per-field parameter-mapping permission checks run only on the real save, so a
clean dry run can still be rejected. Returns the resulting dashboard, so no follow-up read is needed.
- Requires write permission on the dashboard and read permission on every referenced card."
+ Requires write permission on the dashboard and read permission on each card an op wires. A card an op adds or
+ makes a filter's value source needs read permission too, unless the dashboard already uses it."
{:name "dashboard_write"
:default-access :allowed
:scope metabot.scope/agent-content-write
diff --git a/src/metabase/metabot/toolkit/dashboard_op_schema.clj b/src/metabase/metabot/toolkit/dashboard_op_schema.clj
new file mode 100644
index 00000000000..439d817d711
--- /dev/null
+++ b/src/metabase/metabot/toolkit/dashboard_op_schema.clj
@@ -0,0 +1,260 @@
+(ns metabase.metabot.toolkit.dashboard-op-schema
+ "The schema of one dashboard editor op, shared by every tool that sends ops to
+ [[metabase.metabot.toolkit.dashboard-ops/compile-ops]].
+ The descriptions are written for the model, and each tool puts [[op-schema]] in its own argument schema."
+ (:require
+ [clojure.string :as str]))
+
+(set! *warn-on-reflection* true)
+
+(def ^:private position-schema
+ [:map {:closed true :description "Grid slot; omit to autoplace."}
+ [:row [:int {:min 0 :description "Zero-based row."}]]
+ [:col [:int {:min 0 :max 23 :description "Zero-based column; the grid is 24 wide."}]]])
+
+(def ^:private size-schema
+ [:map {:closed true :description "Size in grid cells."}
+ [:size_x [:int {:min 1 :max 24 :description "Width in columns (grid is 24 wide)."}]]
+ [:size_y [:int {:min 1 :description "Height in rows."}]]])
+
+(def ^:private new-id-schema
+ [:int {:max -1
+ :description "Negative id you choose for the new row (-1, -2, …); later ops may reference it."}])
+
+(def ^:private tab-ref-schema
+ [:maybe [:int {:description "Tab id; negative ids from this call work. Omit when the dashboard has no tabs."}]])
+
+(def ^:private inline-parameters-schema
+ [:maybe [:sequential [:string {:description "Parameter id to render on this card, not the header."}]]])
+
+(def ^:private dashcard-ref-schema
+ [:int {:description "Dashcard id; negative ids from this call work."}])
+
+(def ^:private parameter-id-schema
+ [:string {:min 1 :description "Parameter id — a short string you choose and reuse in later ops."}])
+
+;; `add_parameter` and `update_parameter` write REST parameter properties straight through, so the
+;; camelCase names (`isMultiSelect`, `filteringParameters`, `sectionId`) are preserved verbatim.
+(def ^:private parameter-name-schema
+ [:string {:min 1 :description "Label shown on the dashboard."}])
+
+;; `:name` is required on `add_parameter` and optional on `update_parameter`, so it is not in
+;; `parameter-fields` — each op supplies its own entry.
+(def ^:private parameter-fields
+ [[:type {:optional true}
+ [:maybe [:string {:description (str "Parameter type, e.g. \"string/=\", \"string/contains\", "
+ "\"number/=\", \"number/between\", \"date/all-options\", "
+ "\"date/relative\", \"id\", \"temporal-unit\".")}]]]
+ [:sectionId {:optional true}
+ [:maybe [:string {:description "Widget section, e.g. \"string\", \"number\", \"date\", \"id\"."}]]]
+ [:default {:optional true}
+ [:maybe [:any {:description "Default value: a scalar, or an array for multi-select."}]]]
+ [:required {:optional true}
+ [:maybe [:boolean {:description "When true the dashboard needs a value to view; pair with a default."}]]]
+ [:isMultiSelect {:optional true}
+ [:maybe [:boolean {:description "Whether the widget accepts several values."}]]]
+ [:temporal_units {:optional true}
+ [:maybe [:sequential [:string {:description "Allowed unit for a \"temporal-unit\" parameter, e.g. \"month\"."}]]]]
+ [:values_query_type {:optional true}
+ [:maybe [:enum {:description "Value picker behavior: dropdown list, search box, or free text."}
+ "list" "search" "none"]]]
+ [:values_source_type {:optional true}
+ [:maybe [:enum {:description "Picker value source; omit to use the connected field's values."}
+ "static-list" "card"]]]
+ [:values_source_config {:optional true}
+ [:maybe [:map {:description (str "Config for values_source_type: {\"values\": [...]} for "
+ "static-list, or {\"card_id\": n, \"value_field\": target} for card.")}]]]
+ [:filteringParameters {:optional true}
+ [:maybe [:sequential [:string {:description "Id of a parameter whose value narrows this one (linked filter)."}]]]]])
+
+(defn- op-map
+ "One closed op entry for [[op-schema]]: the `op` discriminator plus `fields`."
+ [op-name description fields]
+ [op-name
+ (into [:map {:closed true :description description}
+ [:op [:= op-name]]]
+ fields)])
+
+(def ^:private op-multi
+ [:multi {:dispatch :op
+ :description "One editor operation; `op` selects which."}
+ (op-map "add_card" "Place a saved question, model, or metric on the dashboard."
+ [[:id new-id-schema]
+ [:card_id [:int {:description "Numeric id of the saved question, model, or metric to place."}]]
+ [:tab {:optional true} tab-ref-schema]
+ [:position {:optional true} [:maybe position-schema]]
+ [:size {:optional true} [:maybe size-schema]]
+ [:series {:optional true}
+ [:maybe [:sequential [:int {:description "Numeric id of a card to overlay on this one as an extra series."}]]]]
+ [:inline_parameters {:optional true} inline-parameters-schema]])
+ (op-map "add_text" "Add a markdown text card."
+ [[:id new-id-schema]
+ [:markdown [:string {:description "Markdown body of the text card."}]]
+ [:tab {:optional true} tab-ref-schema]
+ [:position {:optional true} [:maybe position-schema]]
+ [:size {:optional true} [:maybe size-schema]]
+ [:inline_parameters {:optional true} inline-parameters-schema]])
+ (op-map "add_heading" "Add a section heading — a full-width label with no card background."
+ [[:id new-id-schema]
+ [:text [:string {:description "Heading text; plain text, not markdown."}]]
+ [:tab {:optional true} tab-ref-schema]
+ [:position {:optional true} [:maybe position-schema]]
+ [:size {:optional true} [:maybe size-schema]]
+ [:inline_parameters {:optional true} inline-parameters-schema]])
+ (op-map "add_link" "Add a link card. Pass exactly one of `url` or `entity`."
+ [[:id new-id-schema]
+ [:url {:optional true} [:maybe [:string {:description "External URL to link to."}]]]
+ [:entity {:optional true}
+ [:maybe [:map {:closed true :description "A Metabase entity to link to, instead of `url`."}
+ [:type [:enum {:description "Kind of entity being linked."}
+ "dashboard" "card" "dataset" "collection" "database" "table"]]
+ [:id [:int {:description "Numeric id of that entity."}]]]]]
+ [:tab {:optional true} tab-ref-schema]
+ [:position {:optional true} [:maybe position-schema]]
+ [:size {:optional true} [:maybe size-schema]]
+ [:inline_parameters {:optional true} inline-parameters-schema]])
+ (op-map "add_iframe" "Embed an external page. The host must be allowed by the instance's iframe settings."
+ [[:id new-id-schema]
+ [:src [:string {:description "URL to embed, or a full <iframe> tag."}]]
+ [:tab {:optional true} tab-ref-schema]
+ [:position {:optional true} [:maybe position-schema]]
+ [:size {:optional true} [:maybe size-schema]]
+ [:inline_parameters {:optional true} inline-parameters-schema]])
+ (op-map "add_action" "Add a button that runs a saved action."
+ [[:id new-id-schema]
+ [:action_id [:int {:description "Numeric id of the saved action to run."}]]
+ [:label {:optional true} [:maybe [:string {:description "Button text. Defaults to the action's own name."}]]]
+ [:display {:optional true}
+ [:maybe [:enum {:description "\"button\" (default) runs on click; \"form\" renders the action's fields inline."}
+ "button" "form"]]]
+ [:tab {:optional true} tab-ref-schema]
+ [:position {:optional true} [:maybe position-schema]]
+ [:size {:optional true} [:maybe size-schema]]])
+ (op-map "duplicate_card"
+ (str "Copy a dashcard with its card, visualization settings, and parameter mappings; "
+ "same size, its own slot.")
+ [[:id new-id-schema]
+ [:dashcard_id dashcard-ref-schema]
+ [:tab {:optional true} tab-ref-schema]
+ [:position {:optional true} [:maybe position-schema]]])
+ (op-map "replace_card"
+ (str "Point a dashcard at a different card, keeping slot and size; series, parameter "
+ "mappings, and visualization settings reset, as the editor does.")
+ [[:dashcard_id dashcard-ref-schema]
+ [:card_id [:int {:description "Numeric id of the card to show instead."}]]])
+ (op-map "move" "Move a dashcard to a different slot, a different tab, or both."
+ [[:dashcard_id dashcard-ref-schema]
+ [:tab {:optional true} tab-ref-schema]
+ [:position {:optional true} [:maybe position-schema]]])
+ (op-map "resize" "Resize a dashcard in place."
+ [[:dashcard_id dashcard-ref-schema]
+ [:size size-schema]])
+ (op-map "remove" "Delete a dashcard from the dashboard. The underlying card is untouched."
+ [[:dashcard_id dashcard-ref-schema]])
+ (op-map "set_series"
+ "Replace a dashcard's overlaid series with this ordered list; pass an empty list to clear them."
+ [[:dashcard_id dashcard-ref-schema]
+ [:card_ids [:sequential [:int {:description "Numeric id of a card to overlay, in draw order."}]]]])
+ (op-map "patch_dashcard"
+ (str "Merge content settings into a dashcard — visualization settings, click behavior, "
+ "column settings, a link card's target. Layout/identity keys (row, col, size_x, "
+ "size_y, dashboard_tab_id, card_id, action_id, series, id) are rejected; use move, "
+ "resize, replace_card, or set_series.")
+ [[:dashcard_id dashcard-ref-schema]
+ [:patch
+ ;; Open, so a layout or unknown key reaches the compiler, which names the op that owns it.
+ [:map {:description (str "Dashcard properties to merge; `visualization_settings` "
+ "merges key by key rather than replacing the map.")}
+ [:visualization_settings {:optional true}
+ [:maybe [:map {:description "Visualization settings to merge into the dashcard's own."}]]]
+ [:parameter_mappings {:optional true}
+ [:maybe [:sequential [:map {:closed true
+ :description "A parameter mapping, replacing the dashcard's list."}
+ [:parameter_id [:string {:min 1}]]
+ [:card_id {:optional true}
+ [:maybe [:int {:min 1
+ :description "Card it filters, of this dashcard or its series."}]]]
+ [:target [:sequential :any]]]]]]
+ [:inline_parameters {:optional true} inline-parameters-schema]]]])
+ (op-map "add_tab" (str "Add a tab. Existing cards are adopted only when the save ends with "
+ "exactly one tab; adding several at once leaves them orphaned — `move` "
+ "them onto a tab in the same call.")
+ [[:id new-id-schema]
+ [:name [:string {:min 1 :description "Tab label."}]]])
+ (op-map "rename_tab" "Rename an existing tab."
+ [[:tab_id [:int {:description "Id of the tab to rename (negative ids from this call work)."}]]
+ [:name [:string {:min 1 :description "New tab label."}]]])
+ (op-map "move_tab" "Reorder the tab strip by moving one tab to a new position."
+ [[:tab_id [:int {:description "Id of the tab to move."}]]
+ [:index [:int {:min 0 :description "Zero-based position to move it to, counting the other tabs only."}]]])
+ (op-map "duplicate_tab" "Copy a tab and every card on it."
+ [[:id new-id-schema]
+ [:tab_id [:int {:description "Id of the tab to copy."}]]])
+ (op-map "remove_tab" "Delete a tab and every card on it."
+ [[:tab_id [:int {:description "Id of the tab to delete."}]]])
+ (op-map "add_parameter"
+ (str "Add a filter or parameter widget to the dashboard. It does nothing until "
+ "`wire_parameter` connects it to at least one card.")
+ (into [[:parameter_id parameter-id-schema]
+ [:name parameter-name-schema]]
+ parameter-fields))
+ (op-map "update_parameter"
+ (str "Change properties of an existing parameter. Only the properties you pass change; "
+ "the rest are left alone. Passing null does not clear a property — it is treated as "
+ "omitted — so to remove one, name it in `clear`.")
+ (into [[:parameter_id parameter-id-schema]
+ [:name {:optional true} [:maybe parameter-name-schema]]
+ [:clear {:optional true}
+ [:maybe [:sequential
+ [:enum {:description (str "Property names to remove from this parameter "
+ "(default, filteringParameters, "
+ "values_source_config, values_source_type, "
+ "sectionId, temporal_units). Null cannot say this: "
+ "strict clients fill every unset property with "
+ "null, so nulls are treated as omitted.")}
+ "default" "filteringParameters" "values_source_config"
+ "values_source_type" "sectionId" "temporal_units"]]]]]
+ parameter-fields))
+ (op-map "remove_parameter"
+ (str "Delete a parameter with its card mappings, inline placements, and linked-filter "
+ "references; dependent subscriptions are archived and their owners emailed.")
+ [[:parameter_id parameter-id-schema]])
+ (op-map "move_parameter" "Reorder a parameter in the header, or move it onto a card. Pass exactly one of `index` or `dashcard_id`."
+ [[:parameter_id parameter-id-schema]
+ [:index {:optional true}
+ [:maybe [:int {:min 0 :description "Zero-based position in the header, counting the other parameters only."}]]]
+ [:dashcard_id {:optional true}
+ [:maybe [:int {:description "Render the parameter on this dashcard instead of in the header."}]]]])
+ (op-map "wire_parameter"
+ (str "Connect a parameter to a card so the widget actually filters it. Pass exactly one "
+ "of `target_field`, `target_tag`, or `target`.")
+ [[:parameter_id parameter-id-schema]
+ [:dashcard_id dashcard-ref-schema]
+ [:target_field {:optional true}
+ [:maybe [:int {:description (str "Numeric field id to filter on — the usual choice; the "
+ "server derives the mapping from the card's query.")}]]]
+ [:target_tag {:optional true}
+ [:maybe [:string {:description (str "Template tag name on a native-SQL card; the server "
+ "derives the mapping from the tag's type (field filter "
+ "vs raw variable).")}]]]
+ [:target {:optional true}
+ [:maybe [:sequential [:any {:description (str "Raw mapping-target clause, e.g. "
+ "[\"dimension\", [\"template-tag\", \"category\"]] or "
+ "[\"text-tag\", \"name\"] for a {{name}} placeholder in a "
+ "text/heading/iframe card. Advanced; prefer target_field.")}]]]]
+ [:autowire {:optional true}
+ [:maybe [:boolean {:description (str "With target_field, also map every other card exposing "
+ "the same field; cards that don't are skipped silently.")}]]]])
+ (op-map "unwire_parameter" "Disconnect a parameter from one card, or from every card when `dashcard_id` is omitted."
+ [[:parameter_id parameter-id-schema]
+ [:dashcard_id {:optional true}
+ [:maybe [:int {:description "Dashcard to disconnect. Omit to disconnect the parameter everywhere."}]]]])])
+
+(def op-schema
+ "One editor op: a `:multi` on `:op`, with a closed map for each op the compiler applies.
+ An op whose `op` is missing or unknown fails with a message listing the valid ones."
+ ;; Malli's default for a bad dispatch value is "invalid dispatch value", which leaves the model guessing. The list
+ ;; comes from the children, so it can't drift from them. Errors inside a known op keep their own messages.
+ (let [[tag props & children] op-multi]
+ (into [tag (assoc props :error/message (str "should be one of: " (str/join ", " (map first children))))]
+ children)))
diff --git a/src/metabase/metabot/toolkit/dashboard_ops.clj b/src/metabase/metabot/toolkit/dashboard_ops.clj
index bcda185a02b..5fa7f96f14f 100644
--- a/src/metabase/metabot/toolkit/dashboard_ops.clj
+++ b/src/metabase/metabot/toolkit/dashboard_ops.clj
@@ -18,11 +18,15 @@
The data also holds the values that explain the error, and the failing op's `:op-index` and `:op-name`.
This namespace words no errors: each tool turns that data into text for its own model."
(:require
+ [malli.error :as me]
[metabase.dashboards.autoplace :as autoplace]
[metabase.dashboards.models.dashboard-card :as dashboard-card]
[metabase.lib.core :as lib]
+ [metabase.metabot.toolkit.dashboard-op-schema :as dashboard-op-schema]
[metabase.parameters.mapping-targets :as mapping-targets]
- [metabase.util :as u]))
+ [metabase.parameters.schema :as parameters.schema]
+ [metabase.util :as u]
+ [metabase.util.malli.registry :as mr]))
(set! *warn-on-reflection* true)
@@ -30,47 +34,52 @@
(def errors
"Every error code [[compile-ops]] throws, mapped to the schema of the values that explain it.
- Values the caller sent are `:any`, since a caller without MCP's argument schema can send anything.
+ Values from an op have the types [[dashboard-op-schema/op-schema]] gives them, or `:any` where it leaves them open.
Values read from the dashboard have their types.
- Errors thrown while applying an op also carry `:op-index` (0-based) and `:op-name` (as sent, or nil).
+ Errors thrown while checking or applying an op also carry `:op-index` (0-based) and `:op-name` (as sent, or nil).
The `:cards-without-tab` check runs after the last op, so that error carries neither."
- {:missing-op [:map {:closed true}]
- :unknown-op [:map {:closed true}]
- :invalid-new-id [:map {:closed true} [:id :any]]
+ {:invalid-op [:map {:closed true} [:explanation :any]]
:duplicate-new-id [:map {:closed true} [:id :int]]
- :missing-dashcard-id [:map {:closed true}]
- :unknown-dashcard [:map {:closed true} [:dashcard-id :any]]
+ :unknown-dashcard [:map {:closed true} [:dashcard-id :int]]
:unknown-card [:map {:closed true} [:card-id :any]]
- :missing-tab-id [:map {:closed true}]
- :unknown-tab [:map {:closed true} [:tab-id :any]]
+ :unknown-tab [:map {:closed true} [:tab-id :int]]
:no-free-space [:map {:closed true} [:tab-id [:maybe :int]]]
:link-needs-url-or-entity [:map {:closed true}]
:unpatchable-key [:map {:closed true} [:key :string] [:use-op :string]]
:unknown-patch-key [:map {:closed true} [:key :string]]
- :tab-index-out-of-range [:map {:closed true} [:index :any] [:tab-count :int]]
+ :tab-index-out-of-range [:map {:closed true} [:index :int] [:tab-count :int]]
:cards-without-tab [:map {:closed true} [:dashcard-ids [:sequential :int]]]
- :missing-parameter-id [:map {:closed true}]
- :unknown-parameter [:map {:closed true} [:parameter-id :any]]
- :parameter-exists [:map {:closed true} [:parameter-id :any]]
+ :unknown-parameter [:map {:closed true} [:parameter-id :string]]
+ :parameter-exists [:map {:closed true} [:parameter-id :string]]
:set-and-cleared [:map {:closed true} [:field :string]]
:conflicting-parameter-destinations [:map {:closed true}]
:missing-parameter-destination [:map {:closed true}]
- :parameter-index-out-of-range [:map {:closed true} [:index :any] [:parameter-count :int]]
- :field-not-exposed [:map {:closed true} [:dashcard-id :int] [:field-id :any] [:parameter-id :any]]
+ :parameter-index-out-of-range [:map {:closed true} [:index :int] [:parameter-count :int]]
+ :field-not-exposed [:map {:closed true}
+ [:dashcard-id :int] [:field-id :int] [:parameter-id :string]]
:no-card-behind-dashcard [:map {:closed true} [:dashcard-id :int]]
- :unknown-template-tag [:map {:closed true} [:card-id :int] [:tag :any] [:tags [:sequential :string]]]
+ :mapping-card-not-on-dashcard [:map {:closed true} [:dashcard-id :int] [:card-id :int]]
+ :unknown-template-tag [:map {:closed true}
+ [:card-id :int] [:tag :string] [:tags [:sequential :string]]]
:unwireable-tag [:map {:closed true}
[:tag :string] [:tag-type :string] [:wireable-tags [:sequential :string]]]
:unknown-text-tag [:map {:closed true}
[:target :any] [:dashcard-id :int] [:placeholders [:sequential :string]]]
- :unresolved-target [:map {:closed true} [:target :any] [:card-id :int] [:parameter-id :any]]
+ :unresolved-target [:map {:closed true} [:target :any] [:card-id :int] [:parameter-id :string]]
:missing-wire-target [:map {:closed true}]})
(defn- fail!
- "Throw the error `code` from [[errors]], with `data` holding the values that explain it."
- ([code] (fail! code nil))
+ "Throw the error `code` from [[errors]], with `data` holding the values that explain it.
+ An unknown code, or data that breaks the code's schema, throws a plain `ex-info` instead.
+ That is a bug in the caller, and no tool words it as a teaching error."
+ ([code] (fail! code {}))
([code data]
- (assert (contains? errors code) (str "Unknown dashboard op error code " code))
+ ;; Not `assert`: an AssertionError is not an Exception, so MCP's registry would not sanitize it.
+ (when-not (contains? errors code)
+ (throw (ex-info (str "Unknown dashboard op error code " code) {:code code})))
+ (when-not (mr/validate (get errors code) data)
+ (throw (ex-info (str "Data for dashboard op error " code " does not match its schema")
+ {:code code, :data data})))
(throw (ex-info (str "Invalid dashboard op: " (name code))
(assoc data ::error code)))))
@@ -147,20 +156,17 @@
(update state coll (partial mapv f)))
(defn- check-new-id!
- "A new dashcard or tab id must be negative and unused in this batch."
+ "A new dashcard or tab id must be unused in this batch.
+ The op schema already makes it a negative integer."
[state id]
- (when-not (and (integer? id) (neg? id))
- (fail! :invalid-new-id {:id id}))
(when (some #(= id (:id %)) (concat (:dashcards state) (:tabs state)))
(fail! :duplicate-new-id {:id id})))
(defn- resolve-dashcard!
"The existing dashcard `id` names, or an error."
[state id]
- (if (nil? id)
- (fail! :missing-dashcard-id)
- (or (find-row state :dashcards id)
- (fail! :unknown-dashcard {:dashcard-id id}))))
+ (or (find-row state :dashcards id)
+ (fail! :unknown-dashcard {:dashcard-id id})))
(defn- check-tab-id!
"A dashcard's target tab must be one of this dashboard's tabs — including a tab added earlier in
@@ -256,12 +262,6 @@
{:arglists '([state op])}
(fn [_state op] (:op op)))
-(defmethod apply-op :default
- [_state op]
- (if (nil? (:op op))
- (fail! :missing-op)
- (fail! :unknown-op)))
-
(defmethod apply-op "add_card"
[state {:keys [id card_id tab series inline_parameters] :as op}]
(insert-dashcard state op
@@ -413,8 +413,13 @@
(defmethod apply-op "set_series"
[state {:keys [dashcard_id card_ids]}]
- (resolve-dashcard! state dashcard_id)
- (update-row state :dashcards dashcard_id #(assoc % :series (mapv (fn [cid] {:id cid}) card_ids))))
+ (let [dc (resolve-dashcard! state dashcard_id)
+ removed (into #{} (comp (keep :id) (remove (set card_ids))) (:series dc))]
+ ;; A mapping left on a removed series card would filter a card the dashcard no longer shows, as replace_card avoids.
+ (update-row state :dashcards dashcard_id
+ #(cond-> (assoc % :series (mapv (fn [cid] {:id cid}) card_ids))
+ (and (seq removed) (seq (:parameter_mappings %)))
+ (update :parameter_mappings (partial filterv (fn [m] (not (contains? removed (:card_id m))))))))))
(defn- coerce-target
"Keywordize the clause heads of a raw JSON `target` — `[\"dimension\", [\"template-tag\", \"x\"]]`
@@ -426,38 +431,68 @@
(into [(keyword (first x))] (map coerce-target) (rest x))
x))
+(defn- resolve-parameter!
+ [state id]
+ (or (find-row state :parameters id)
+ (fail! :unknown-parameter {:parameter-id id})))
+
+(defn- patched-mapping
+ "`mapping` as `dc` would store it: the three keys a mapping has, its target's clause heads keywordized, and its
+ card filled in from the dashcard when it names none."
+ [dc mapping]
+ ;; A patched mapping arrives as raw JSON, so its target's clause heads are strings ("dimension", "field") where the
+ ;; mapping schema wants keywords, the same coercion `wire_parameter` does. The query processor applies a mapping
+ ;; only when its card_id is the card being run, and nothing fills one in later, so a mapping without one would
+ ;; never filter anything.
+ (cond-> (-> (select-keys mapping [:parameter_id :card_id :target])
+ (update :target (comp coerce-target vec)))
+ (nil? (:card_id mapping)) (assoc :card_id (:card_id dc))))
+
+(defn- check-patched-mapping!
+ "Throws unless `mapping`, from [[patched-mapping]], is a mapping the save accepts, for a parameter on the dashboard
+ and a card `dc` shows."
+ [state dc mapping]
+ (when-let [explanation (mr/explain ::parameters.schema/parameter-mapping mapping)]
+ (fail! :invalid-op {:explanation (me/humanize explanation)}))
+ (resolve-parameter! state (:parameter_id mapping))
+ ;; The save would otherwise filter the primary card for a mapping that names a card the dashcard doesn't show.
+ (when (and (:card_id mapping)
+ (not (contains? (into #{(:card_id dc)} (keep :id) (:series dc)) (:card_id mapping))))
+ (fail! :mapping-card-not-on-dashcard {:dashcard-id (:id dc), :card-id (:card_id mapping)})))
+
(defmethod apply-op "patch_dashcard"
[state {:keys [dashcard_id patch]}]
- (resolve-dashcard! state dashcard_id)
- (doseq [k (keys patch)]
- (cond
- (contains? patch-rejected-keys k)
- (fail! :unpatchable-key {:key (name k), :use-op (get patch-rejected-keys k)})
+ (let [dc (resolve-dashcard! state dashcard_id)]
+ (doseq [k (keys patch)]
+ (cond
+ (contains? patch-rejected-keys k)
+ (fail! :unpatchable-key {:key (name k), :use-op (get patch-rejected-keys k)})
+
+ (not (contains? patchable-keys k))
+ (fail! :unknown-patch-key {:key (if (or (keyword? k) (string? k)) (name k) (pr-str k))})))
+ (let [mappings (when (contains? patch :parameter_mappings)
+ (mapv (partial patched-mapping dc) (:parameter_mappings patch)))
+ stored (into #{} (map (partial patched-mapping dc)) (:parameter_mappings dc))]
+ ;; Only new or changed mappings are checked. The patch replaces the whole list, so a stale stored mapping sent
+ ;; back unchanged would otherwise block an unrelated edit.
+ (doseq [m mappings
+ :when (not (contains? stored m))]
+ (check-patched-mapping! state dc m))
+ (update-row state :dashcards dashcard_id
+ (fn [dc]
+ (cond-> (merge dc (dissoc patch :visualization_settings :parameter_mappings))
+ (contains? patch :visualization_settings)
+ (update :visualization_settings merge (:visualization_settings patch))
- (not (contains? patchable-keys k))
- (fail! :unknown-patch-key {:key (name k)})))
- (update-row state :dashcards dashcard_id
- (fn [dc]
- (cond-> (merge dc (dissoc patch :visualization_settings))
- (contains? patch :visualization_settings)
- (update :visualization_settings merge (:visualization_settings patch))
-
- ;; A patched mapping arrives as raw JSON, so its target's clause heads are strings
- ;; ("dimension", "field") where the mapping schema wants keywords — the same coercion
- ;; `wire_parameter` does. Without it every parameter_mappings patch, a key this op
- ;; advertises as patchable, fails validation with "should be :dimension".
- (contains? patch :parameter_mappings)
- (update :parameter_mappings
- (partial mapv #(cond-> % (:target %) (update :target (comp coerce-target vec)))))))))
+ mappings
+ (assoc :parameter_mappings mappings)))))))
;;; -------------------------------------------------- Tab ops ------------------------------------------------------
(defn- resolve-tab!
[state id]
- (if (nil? id)
- (fail! :missing-tab-id)
- (or (find-row state :tabs id)
- (fail! :unknown-tab {:tab-id id}))))
+ (or (find-row state :tabs id)
+ (fail! :unknown-tab {:tab-id id})))
(defn- next-temp-id
"A negative id not yet used by any dashcard or tab in the working state. Ops that clone a whole
@@ -517,17 +552,18 @@
;;; --------------------------------------------- Parameter ops -----------------------------------------------------
-(defn- resolve-parameter!
- [state id]
- (if (nil? id)
- (fail! :missing-parameter-id)
- (or (find-row state :parameters id)
- (fail! :unknown-parameter {:parameter-id id}))))
-
(defn- card-for-dashcard
[state dashcard]
(get (::cards state) (:card_id dashcard)))
+(defn- check-card-resolved!
+ "Throws `:unknown-card` when `dashcard` has a card that is not in the caller's `cards` map.
+ That card is missing or unreadable, so an op that needs its metadata cannot run."
+ [state dashcard]
+ ;; Without this, a wire op would report a text-card or missing-column error for a card it simply cannot see.
+ (when (and (:card_id dashcard) (nil? (card-for-dashcard state dashcard)))
+ (fail! :unknown-card {:card-id (:card_id dashcard)})))
+
(defn- assoc-slug
"Derive `:slug` from a parameter's name, as the editor's `setParameterName` does. Without it a
parameter this tool creates is not addressable by slug — embedding, public links, and URL param
@@ -538,8 +574,6 @@
(defmethod apply-op "add_parameter"
[state {:keys [parameter_id] :as op}]
- (when (nil? parameter_id)
- (fail! :missing-parameter-id))
(when (find-row state :parameters parameter_id)
(fail! :parameter-exists {:parameter-id parameter_id}))
(insert-row state :parameters
@@ -603,11 +637,19 @@
:else
(fail! :missing-parameter-destination)))
+(defn- wire-card!
+ "The card behind the dashcard a wire op targets, or an error — a virtual dashcard (text, heading,
+ link, iframe) has no query, so only a `[:text-tag …]` target (handled before this) can wire it."
+ [state dashcard]
+ (check-card-resolved! state dashcard)
+ (or (card-for-dashcard state dashcard)
+ (fail! :no-card-behind-dashcard {:dashcard-id (:id dashcard)})))
+
(defn- wire-one
"Add or replace `parameter`'s mapping on `dashcard`. Returns the dashcard unchanged when its card
exposes no target for `field-id`, unless `explicit?`, when that is an error."
[state parameter dashcard field-id explicit?]
- (let [card (card-for-dashcard state dashcard)
+ (let [card (if explicit? (wire-card! state dashcard) (card-for-dashcard state dashcard))
target (when card (mapping-targets/target-for-field card parameter field-id))]
(cond
target
@@ -620,13 +662,6 @@
:else dashcard)))
-(defn- wire-card!
- "The card behind the dashcard a wire op targets, or an error — a virtual dashcard (text, heading,
- link, iframe) has no query, so only a `[:text-tag …]` target (handled before this) can wire it."
- [state dashcard]
- (or (card-for-dashcard state dashcard)
- (fail! :no-card-behind-dashcard {:dashcard-id (:id dashcard)})))
-
(defn- tag-target!
"The mapping target for `target_tag` on `dashcard`'s card — `[:dimension [:template-tag …]]` for a
field-filter or temporal-unit tag, `[:variable …]` for a raw variable, derived from the tag's actual
@@ -737,52 +772,141 @@
;;; ------------------------------------------------ Entry ---------------------------------------------------------
-(defn card-refs
- "The ids of the cards `op` references.
- Those are the cards it puts on the dashboard, and the card a parameter it adds or updates draws its values from.
- The caller resolves these into the `cards` map for [[compile-ops]], which rejects any it left out."
+(def ^:private parameter-ops
+ "The ops that leave a parameter with the properties they send."
+ #{"add_parameter" "update_parameter"})
+
+(defn- placed-card-ids
+ "The ids of the cards `op` puts on the dashboard."
[op]
(remove nil?
(case (:op op)
- "add_card" (cons (:card_id op) (:series op))
- "replace_card" [(:card_id op)]
- "set_series" (:card_ids op)
- ("add_parameter" "update_parameter") [(get-in op [:values_source_config :card_id])]
+ "add_card" (cons (:card_id op) (:series op))
+ "replace_card" [(:card_id op)]
+ "set_series" (:card_ids op)
nil)))
+(defn- source-card-id
+ "The card `parameter` takes its values from, or nil when its values come from somewhere else."
+ [parameter]
+ ;; Matches what the save read-checks: a source card counts only while the source type is card.
+ (when (= :card (some-> (:values_source_type parameter) keyword))
+ (get-in parameter [:values_source_config :card_id])))
+
+(defn referenced-card-ids
+ "The ids of the cards [[compile-ops]] may need a row for when applying `ops`, ops from [[prepare-ops]], to `dash`.
+ Those are the cards on its dashcards and the cards the ops put on it.
+ They also include the card in each config a parameter op sends or touches, since the op may make it the source.
+ The caller resolves these into the `cards` map, leaving out the ones its user cannot read."
+ [dash ops]
+ (let [id->parameter (into {} (map (juxt :id identity)) (:parameters dash))
+ op-card-ids (fn [op]
+ (concat (placed-card-ids op)
+ (when (contains? parameter-ops (:op op))
+ (for [p [op (id->parameter (:parameter_id op))]
+ :let [card-id (get-in p [:values_source_config :card_id])]
+ :when card-id]
+ card-id))))]
+ (-> (set (keep :card_id (:dashcards dash)))
+ (into (mapcat op-card-ids) ops)
+ (disj nil))))
+
+(defn- dashboard-card-ids
+ "The ids of every card `dash` references: on its dashcards, in their series, and as a parameter's card source.
+ These are the references the save treats as already stored, and leaves alone."
+ [dash]
+ (-> #{}
+ (into (mapcat (fn [dc] (cons (:card_id dc) (keep :id (:series dc))))) (:dashcards dash))
+ (into (keep source-card-id) (:parameters dash))
+ (disj nil)))
+
(defn- check-cards-resolved!
- "Every card `op` references must be in the caller's `cards` map.
- The caller leaves out the cards its user cannot read, so this is where an op naming one fails."
+ "Every card `op` puts on the dashboard must be in the caller's `cards` map.
+ So must the source card of a parameter it leaves with a card source, which may come from the config it already had.
+ A card the dashboard already referenced before the ops is exempt, as the save leaves stored references alone.
+ The caller leaves out the cards its user cannot read, so this is where an op naming one fails.
+ `state` is the state after the op."
[state op]
- (doseq [card-id (card-refs op)
+ (doseq [card-id (concat (placed-card-ids op)
+ (when (contains? parameter-ops (:op op))
+ (some-> (find-row state :parameters (:parameter_id op)) source-card-id vector)))
+ :when (not (contains? (::stored-card-ids state) card-id))
:when (not (contains? (::cards state) card-id))]
(fail! :unknown-card {:card-id card-id})))
-(defn- apply-op-at
- "Runs [[apply-op]] on `op`, the op at `idx`, then checks that the cards it references are resolved.
- Adds `:op-index` and `:op-name` to any error it throws."
- [state idx op]
+(defn- at-op
+ "Calls `f`, adding `:op-index` and `:op-name` to any error from [[errors]] it throws about `op`, the op at `idx`."
+ [idx op f]
(try
- ;; The card check runs second, so an op that is malformed in some other way reports that instead.
- (doto (apply-op state op)
- (check-cards-resolved! op))
+ (f)
(catch clojure.lang.ExceptionInfo e
(throw (if-let [data (error-data e)]
(ex-info (ex-message e) (assoc data :op-index idx :op-name (:op op)) e)
e)))))
+;; Strict tool-calling clients send every declared property, and fill the ones they do not set with null. The ops
+;; that tell absent from null (`move` and `duplicate_card` on `tab`, `move_parameter` on `index` and `dashcard_id`,
+;; `unwire_parameter` on `dashcard_id`) would otherwise read a filled-in null as a deliberate value.
+(defn- compact-op
+ "The op without its null-valued keys."
+ [op]
+ (into {} (remove (comp nil? val)) op))
+
+(defn- prepared-op
+ "The op at `idx`, checked against [[dashboard-op-schema/op-schema]] and then stripped of null values."
+ [idx op]
+ (at-op idx op #(if-let [explanation (mr/explain dashboard-op-schema/op-schema op)]
+ (fail! :invalid-op {:explanation (me/humanize explanation)})
+ ;; A patch's declared properties are nullable too, so a strict client fills them with null as well.
+ (cond-> (compact-op op)
+ (map? (:patch op)) (update :patch compact-op)))))
+
+(defn prepare-ops
+ "The ops, each checked against [[dashboard-op-schema/op-schema]] and then stripped of null values.
+ A nil `ops` means none, and prepares to an empty vector.
+ Throws `:invalid-op` when `ops` is not a sequence, or for the first op that breaks the schema.
+ Run it before anything reads the ops, such as [[referenced-card-ids]].
+ Ops it has already prepared come back as they are, so calling it again costs nothing."
+ [ops]
+ (cond
+ (::prepared (meta ops))
+ ops
+
+ (or (nil? ops) (sequential? ops))
+ (with-meta (into [] (map-indexed prepared-op) ops) {::prepared true})
+
+ :else
+ (fail! :invalid-op {:explanation ["ops must be a list"]})))
+
+(defn- apply-op-at
+ "Runs [[apply-op]] on `op`, the op at `idx`, then checks that the cards it references are resolved."
+ [state idx op]
+ ;; The card check runs second, so an op that is malformed in some other way reports that instead.
+ (at-op idx op #(doto (apply-op state op)
+ (check-cards-resolved! op))))
+
+(defn- series-ids
+ "`dc` with its `:series` reduced to `{:id}` maps."
+ [dc]
+ ;; A hydrated dashcard carries full card rows in `:series`, and the save's dashcard schema allows only their ids.
+ (cond-> dc
+ (seq (:series dc)) (update :series (partial mapv #(select-keys % [:id])))))
+
(defn compile-ops
"Fold `ops` over `current`, a dashboard hydrated with `[:dashcards :series :card] :tabs`.
Returns `{:dashcards :tabs :parameters}`, the full-replacement payload `update-dashboard!` saves.
+ Each dashcard's `:series` holds only card ids.
New rows carry the caller's negative ids.
+ It runs the ops through [[prepare-ops]] before applying any, so it also takes ops that have not been through it.
The `cards` map holds the row of each card the ops may touch, keyed by id.
- It covers the cards [[card-refs]] finds in `ops` and the cards already on the dashboard.
- It leaves out cards the user cannot read.
+ It covers the cards [[referenced-card-ids]] finds, leaving out the ones the user cannot read.
+ A card the dashboard already references needs no row, as the save leaves stored references alone.
This namespace does no I/O, so the caller resolves them.
Throws an error from [[errors]] on the first invalid op; read it with [[error-data]]."
[current ops cards]
(-> (reduce-kv apply-op-at
- (init-state current cards)
- (vec ops))
+ (assoc (init-state current cards) ::stored-card-ids (dashboard-card-ids current))
+ (prepare-ops ops))
check-tab-coverage!
- (select-keys [:dashcards :tabs :parameters])))
+ (select-keys [:dashcards :tabs :parameters])
+ (update :dashcards (partial mapv series-ids))))
diff --git a/test/metabase/mcp/v2/dashboard_errors_test.clj b/test/metabase/mcp/v2/dashboard_errors_test.clj
index ebe5f10e791..33b82332e1d 100644
--- a/test/metabase/mcp/v2/dashboard_errors_test.clj
+++ b/test/metabase/mcp/v2/dashboard_errors_test.clj
@@ -8,7 +8,6 @@
[metabase.mcp.v2.dashboard-errors :as dashboard-errors]
[metabase.mcp.v2.message :as message]
[metabase.metabot.toolkit.dashboard-ops :as dashboard-ops]
- [metabase.test :as mt]
[metabase.util :as u]))
(set! *warn-on-reflection* true)
@@ -20,10 +19,10 @@
"The teaching error text MCP returns for `ops` against `current` and `cards`, or nil when they compile.
It defaults `cards` to a stub row for every card `ops` name."
([current ops]
- (error-text current ops (u/for-map [id (mapcat dashboard-ops/card-refs ops)] [id {:id id}])))
+ (error-text current ops (u/for-map [id (dashboard-ops/referenced-card-ids nil ops)] [id {:id id}])))
([current ops cards]
(try
- (dashboard-errors/compile-ops current ops cards)
+ (dashboard-errors/do-with-teaching-errors #(dashboard-ops/compile-ops current ops cards))
nil
(catch clojure.lang.ExceptionInfo e
(ex-message e)))))
@@ -40,8 +39,6 @@
(deftest ^:parallel op-error-text-test
(testing "GHY-4544: op errors name the index and op once, with caller-sent values quoted once"
- (is (= "op 0 (\"frobnicate\"): unknown op — see the tool description for the supported list."
- (error-text empty-dash [{:op "frobnicate"}])))
(is (= "op 0 (\"add_link\"): pass exactly one of `url` or `entity`."
(error-text empty-dash [{:op "add_link" :id -1}])))
(is (= "op 0 (\"patch_dashcard\"): \"nonsense\" is not a patchable property."
@@ -50,15 +47,12 @@
(is (= "op 1 (\"add_card\"): id -1 is already used in this batch — give each new row its own negative id."
(error-text empty-dash [{:op "add_card" :id -1 :card_id 42}
{:op "add_card" :id -1 :card_id 43}]))))
- (testing "GHY-4544: an op sent without its `op` or a referenced id says so rather than naming null"
- (is (= "op 0: missing `op` — see the tool description for the supported list."
- (error-text empty-dash [{:id -1}])))
- (is (= "op 0 (\"remove\"): missing `dashcard_id`."
- (error-text empty-dash [{:op "remove"}])))
- (is (= "op 0 (\"remove_tab\"): missing `tab_id`."
- (error-text empty-dash [{:op "remove_tab"}])))
- (is (= "op 0 (\"remove_parameter\"): missing `parameter_id`."
- (error-text empty-dash [{:op "remove_parameter"}]))))
+ (testing "an op that breaks the op schema names the op and what is wrong with it"
+ (is (= (str "op 0 (\"move_tab\"): invalid op: \"index\": \"missing required key\" — "
+ "see the tool description for each op's arguments.")
+ (error-text empty-dash [{:op "move_tab" :tab_id 1}])))
+ (is (re-matches #"op 0: invalid op: \"op\": \"should be one of: add_card, .*\" — see the tool description .*"
+ (error-text empty-dash [{:id -1}]))))
(testing "an unknown dashcard, tab or parameter id names the op"
(is (= "op 0 (\"duplicate_card\"): no dashcard with id 999 on this dashboard."
(error-text empty-dash [{:op "duplicate_card" :id -1 :dashcard_id 999}])))
@@ -69,10 +63,21 @@
(testing "a card placed on an unknown tab names the op too"
(is (= "op 0 (\"add_text\"): no tab with id 77 on this dashboard."
(error-text empty-dash [{:op "add_text" :id -1 :markdown "hi" :tab 77}]))))
+ (testing "a patched mapping naming a card that is not on the dashcard says which cards it may name"
+ (is (= (str "op 0 (\"patch_dashcard\"): card 3 is not on dashcard 7 — a mapping's card_id must be the "
+ "dashcard's card or one of its series. Omit it to filter the dashcard's card.")
+ (error-text (assoc empty-dash
+ :dashcards [{:id 7 :card_id 9 :row 0 :col 0 :size_x 4 :size_y 4}]
+ :parameters [{:id "p1" :name "P1" :type "category"}])
+ [{:op "patch_dashcard"
+ :dashcard_id 7
+ :patch {:parameter_mappings [{:parameter_id "p1"
+ :card_id 3
+ :target ["dimension" ["field" 1 nil]]}]}}]))))
(testing "add_parameter names itself quoted, like every op, when the parameter exists"
(is (= "op 0 (\"add_parameter\"): parameter \"p1\" already exists — use \"update_parameter\"."
(error-text (assoc empty-dash :parameters [{:id "p1"}])
- [{:op "add_parameter" :parameter_id "p1"}]))))
+ [{:op "add_parameter" :parameter_id "p1" :name "P"}]))))
(testing "a card the caller could not resolve reads as one the user cannot read"
(is (= "op 0 (\"add_card\"): no card with id 42 that you can read."
(error-text empty-dash [{:op "add_card" :id -1 :card_id 42}] {}))))
@@ -128,14 +133,14 @@
(deftest ^:parallel teaching-error-keeps-its-cause-test
(testing "the teaching error is a 400 whose cause is the compiler's own error"
- (let [e (try (dashboard-errors/compile-ops empty-dash [{:op "frobnicate"}] {})
+ (let [e (try (dashboard-errors/do-with-teaching-errors
+ #(dashboard-ops/compile-ops empty-dash [{:op "frobnicate"}] {}))
(catch clojure.lang.ExceptionInfo e e))]
(is (=? {:status-code 400} (ex-data e)))
- (is (=? {::dashboard-ops/error :unknown-op} (dashboard-ops/error-data (ex-cause e)))))))
+ (is (=? {::dashboard-ops/error :invalid-op} (dashboard-ops/error-data (ex-cause e)))))))
-(deftest other-errors-pass-through-test
- (testing "an exception that is not a compiler error is rethrown as it is"
+(deftest ^:parallel other-errors-pass-through-test
+ (testing "an exception that is not a dashboard op error is rethrown as it is"
(let [e (ex-info "boom" {:x 1})]
- (mt/with-dynamic-fn-redefs [dashboard-ops/compile-ops (fn [& _] (throw e))]
- (is (identical? e (try (dashboard-errors/compile-ops empty-dash [] {})
- (catch clojure.lang.ExceptionInfo caught caught))))))))
+ (is (identical? e (try (dashboard-errors/do-with-teaching-errors #(throw e))
+ (catch clojure.lang.ExceptionInfo caught caught)))))))
diff --git a/test/metabase/mcp/v2/tools/dashboard_test.clj b/test/metabase/mcp/v2/tools/dashboard_test.clj
index 06beb859d85..0d013e2d02d 100644
--- a/test/metabase/mcp/v2/tools/dashboard_test.clj
+++ b/test/metabase/mcp/v2/tools/dashboard_test.clj
@@ -14,6 +14,7 @@
[metabase.mcp.v2.test-util :as v2.tu]
;; Registers the tool the assertions below drive.
[metabase.mcp.v2.tools.dashboard :as tools.dashboard]
+ [metabase.metabot.toolkit.dashboard-ops :as dashboard-ops]
[metabase.permissions.core :as perms]
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.test :as mt]
@@ -90,6 +91,12 @@
(is (re-find #"\"id\" is required"
(tool-error (call-tool! :crowberto v2.tu/all-scopes "dashboard_write" (wire {:method "update"})))))))
+(deftest unknown-op-lists-the-valid-ops-test
+ (testing "an unknown op, refused by the registry before the handler runs, names the ops there are"
+ (is (re-find #"\"op\": \"should be one of: add_card, add_text, .*, unwire_parameter\""
+ (tool-error (call-tool! :crowberto v2.tu/all-scopes "dashboard_write"
+ (wire {:method "create" :name "Sales" :ops [{:op "frobnicate"}]})))))))
+
(deftest create-with-ops-in-one-call-test
(testing "GHY-4147: create accepts ops, so a dashboard and its cards land in a single call"
(mt/with-model-cleanup [:model/Dashboard]
@@ -222,7 +229,8 @@
(testing "`parameter_mappings` is advertised as patchable, and a mapping arrives as raw JSON with string
clause heads. Without the same target coercion `wire_parameter` does, every such patch failed
validation with \"should be :dimension\" — a documented key that could never be used."
- (mt/with-temp [:model/Dashboard dash {:name "Sales"}
+ (mt/with-temp [:model/Dashboard dash {:name "Sales"
+ :parameters [{:id "p1" :name "P1" :slug "p1" :type "category"}]}
:model/Card card {:name "Revenue"}
:model/DashboardCard dc {:dashboard_id (:id dash) :card_id (:id card)}]
(let [result (call-tool! :crowberto v2.tu/all-scopes "dashboard_write"
@@ -401,6 +409,42 @@
:values_source_type "card"
:values_source_config {:card_id 9999999}}]}))))))))
+(deftest dry-run-checks-a-reactivated-source-card-test
+ (testing "an op that makes a parameter's existing source card its source is checked against that card"
+ (mt/with-temp [:model/Card card {}
+ :model/Dashboard dash {:name "Sales"
+ :parameters [{:id "p"
+ :name "P"
+ :slug "p"
+ :type "string/="
+ :values_source_type "static-list"
+ :values_source_config {:card_id (:id card) :values ["a"]}}]}]
+ (is (=? {:parameters [{:id "p"}]}
+ (tool-result (call-tool! :crowberto v2.tu/all-scopes "dashboard_write"
+ (wire {:method "update"
+ :id (:id dash)
+ :validate_only true
+ :ops [{:op "update_parameter"
+ :parameter_id "p"
+ :values_source_type "card"}]}))))))))
+
+(deftest published-patch-mapping-schema-test
+ (testing "a strict client can send every key of a patched parameter mapping, card_id included"
+ (mt/with-current-user (mt/user->id :crowberto)
+ (let [schema (:inputSchema (first (filter #(= "dashboard_write" (:name %)) (registry/list-tools))))
+ mapping? (fn [x]
+ (let [props (when (map? x) (:properties x))]
+ ;; an op object (wire_parameter) has these keys too, but also `op`
+ (and (contains? props :target) (contains? props :parameter_id) (not (contains? props :op)))))
+ mappings (filter mapping? (tree-seq coll? seq schema))]
+ (is (= [{:keys #{:parameter_id :card_id :target}
+ :required #{:parameter_id :card_id :target}
+ :closed? true}]
+ (for [m mappings]
+ {:keys (set (keys (:properties m)))
+ :required (set (map keyword (:required m)))
+ :closed? (false? (:additionalProperties m))})))))))
+
(deftest archived-on-create-error-text-test
(testing "GHY-4544: `archived` on a create is a teaching error"
(is (= "\"archived\" applies to method \"update\" only — remove it from this create call."
@@ -437,6 +481,44 @@
(is (zero? (t2/count :model/DashboardCard :dashboard_id (:id dash)))
"nothing may be written before the refusal"))))
+(deftest reactivated-source-card-is-refused-for-a-non-admin-test
+ (testing "switching a parameter to the card its config already names checks that card, so a non-admin is refused one
+ they cannot read before any write"
+ (mt/with-temp [:model/Collection {secret-id :id} {}
+ :model/Card {hidden-id :id} {:name "CONFIDENTIAL" :collection_id secret-id}
+ :model/Dashboard dash {:name "Sales"
+ :parameters [{:id "p"
+ :name "P"
+ :slug "p"
+ :type "string/="
+ :values_source_type "static-list"
+ :values_source_config {:card_id hidden-id
+ :values ["a"]}}]}]
+ (perms/revoke-collection-permissions! (perms-group/all-users) secret-id)
+ (doseq [validate-only? [true false]]
+ (testing (str "validate_only " validate-only?)
+ (is (re-find #"no card with id \d+ that you can read"
+ (tool-error (call-tool! :rasta v2.tu/all-scopes "dashboard_write"
+ (wire {:method "update"
+ :id (:id dash)
+ :validate_only validate-only?
+ :ops [{:op "update_parameter"
+ :parameter_id "p"
+ :values_source_type "card"}]})))))))
+ (is (= :static-list (:values_source_type (first (t2/select-one-fn :parameters :model/Dashboard :id (:id dash)))))
+ "nothing may be written before the refusal"))))
+
+(deftest stored-card-ids-match-the-save-test
+ (testing "the compiler exempts exactly the card references the save treats as stored"
+ (let [dash {:dashcards [{:id 7 :card_id 9 :series [{:id 11}]} {:id 8 :card_id nil}]
+ :parameters [{:id "p1" :values_source_type "card" :values_source_config {:card_id 45}}
+ {:id "p2" :values_source_type :card :values_source_config {:card_id 46}}
+ {:id "p3" :values_source_type "static-list" :values_source_config {:card_id 47}}
+ {:id "p4"}]}]
+ (is (= #{9 11 45 46}
+ (:cards (#'dashboards.write/references dash))
+ (#'dashboard-ops/dashboard-card-ids dash))))))
+
(deftest create-applies-display-attributes-test
(testing "GHY-4147: width and auto_apply_filters are honored on create, not silently dropped"
(mt/with-model-cleanup [:model/Dashboard]
diff --git a/test/metabase/metabot/toolkit/dashboard_ops_test.clj b/test/metabase/metabot/toolkit/dashboard_ops_test.clj
index 6c2c47ecf23..4c6f6e5f6e5 100644
--- a/test/metabase/metabot/toolkit/dashboard_ops_test.clj
+++ b/test/metabase/metabot/toolkit/dashboard_ops_test.clj
@@ -4,6 +4,8 @@
against plain maps."
(:require
[clojure.test :refer :all]
+ [malli.core :as mc]
+ [metabase.metabot.toolkit.dashboard-op-schema :as dashboard-op-schema]
[metabase.metabot.toolkit.dashboard-ops :as dashboard-ops]
[metabase.parameters.mapping-targets :as mapping-targets]
[metabase.test.util.dynamic-redefs :as dynamic-redefs]
@@ -22,14 +24,12 @@
(defn- resolved-cards
"A bare card row for each card `ops` name, as a caller that can read them all resolves them."
[ops]
- (u/for-map [id (mapcat dashboard-ops/card-refs ops)] [id {:id id}]))
+ (u/for-map [id (dashboard-ops/referenced-card-ids nil ops)] [id {:id id}]))
-;; TODO (Chris 2026-10-29) -- a name unique to this helper would be less ambiguous. It shares its name with
-;; [[dashboard-ops/compile-ops]], but also resolves a stub row for every card the ops name.
-(defn- compile-ops
+(defn- compile-with-stub-cards
"Runs [[dashboard-ops/compile-ops]] with every card `ops` name resolved.
The optional `cards` adds or replaces card rows."
- ([current ops] (compile-ops current ops {}))
+ ([current ops] (compile-with-stub-cards current ops {}))
([current ops cards] (dashboard-ops/compile-ops current ops (merge (resolved-cards ops) cards))))
(defn- inline-parameter-owners
@@ -46,7 +46,7 @@
(deftest add-card-autoplaces-test
(testing "GHY-4147: add_card with no position lands at the top-left of an empty dashboard"
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
empty-dash
[{:op "add_card" :id -1 :card_id 42}])]
(is (= 1 (count dashcards)))
@@ -57,7 +57,7 @@
(deftest add-card-respects-explicit-position-and-size-test
(testing "GHY-4147: explicit position and size are passed through untouched"
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
empty-dash
[{:op "add_card" :id -1 :card_id 42
:position {:row 3 :col 4}
@@ -68,7 +68,7 @@
(deftest existing-dashcards-are-preserved-test
(testing "GHY-4147: the payload is a full replacement, so untouched dashcards survive verbatim"
(let [existing {:id 7 :card_id 9 :row 0 :col 0 :size_x 4 :size_y 4 :dashboard_tab_id nil}
- {:keys [dashcards]} (compile-ops
+ {:keys [dashcards]} (compile-with-stub-cards
(dash-with [existing])
[{:op "add_card" :id -1 :card_id 42}])]
(is (= #{7 -1} (set (map :id dashcards))))
@@ -77,7 +77,7 @@
(deftest autoplace-avoids-existing-cards-test
(testing "GHY-4147: autoplace does not overlap an occupied slot"
(let [existing {:id 7 :card_id 9 :row 0 :col 0 :size_x 24 :size_y 4 :dashboard_tab_id nil}
- {:keys [dashcards]} (compile-ops
+ {:keys [dashcards]} (compile-with-stub-cards
(dash-with [existing])
[{:op "add_card" :id -1 :card_id 42}])
added (first (filter #(= -1 (:id %)) dashcards))]
@@ -86,7 +86,6 @@
(defn- op-error
"The error data [[dashboard-ops/compile-ops]] throws for `ops` against `current` and `cards`, or nil when it compiles.
It defaults `cards` to a stub row for every card `ops` name.
- It also checks the data against the schema [[dashboard-ops/errors]] declares for its code.
It rethrows an exception that is not a compiler error, so the test reports it as it is."
([current ops] (op-error current ops (resolved-cards ops)))
([current ops cards]
@@ -94,10 +93,7 @@
(dashboard-ops/compile-ops current ops cards)
nil
(catch clojure.lang.ExceptionInfo e
- (let [{code ::dashboard-ops/error :as data} (or (dashboard-ops/error-data e) (throw e))]
- (is (mr/validate (get dashboard-ops/errors code) (dissoc data ::dashboard-ops/error :op-index :op-name))
- "the error data matches its schema in `errors`")
- data)))))
+ (or (dashboard-ops/error-data e) (throw e))))))
(deftest ^:parallel autoplace-with-no-free-slot-test
(testing "a tab with no free slot within autoplace's search is an error, not a card placed off the grid"
@@ -115,37 +111,104 @@
[{:op "add_card" :id -1 :card_id 42}
{:op "add_card" :id -1 :card_id 43}])))))
-(deftest ^:parallel positive-temp-id-is-rejected-test
- (testing "GHY-4147: a new dashcard's id must be negative — positives would silently target a real row"
- (is (=? {::dashboard-ops/error :invalid-new-id, :id 5}
- (op-error empty-dash [{:op "add_card" :id 5 :card_id 42}])))))
-
-(deftest ^:parallel unknown-op-is-rejected-test
- (testing "GHY-4147: an unrecognized op names the index and the op"
- (is (=? {::dashboard-ops/error :unknown-op, :op-name "frobnicate"}
- (op-error empty-dash [{:op "frobnicate"}])))))
-
-(deftest ^:parallel missing-op-or-id-test
- (testing "GHY-4544: an op sent without its `op` or a referenced id has its own error"
- (is (=? {::dashboard-ops/error :missing-op, :op-name nil}
- (op-error empty-dash [{:id -1}])))
- (is (=? {::dashboard-ops/error :missing-dashcard-id, :op-name "remove"}
- (op-error empty-dash [{:op "remove"}])))
- (is (=? {::dashboard-ops/error :missing-tab-id, :op-name "remove_tab"}
- (op-error empty-dash [{:op "remove_tab"}])))
- (is (=? {::dashboard-ops/error :missing-parameter-id, :op-name "remove_parameter"}
- (op-error empty-dash [{:op "remove_parameter"}])))
- (is (=? {::dashboard-ops/error :missing-parameter-id, :op-name "add_parameter"}
- (op-error empty-dash [{:op "add_parameter" :name "Country" :type "string/="}])))))
+(deftest ^:parallel ops-that-break-the-op-schema-test
+ (testing "an op that breaks the op schema fails with the explanation, naming the op"
+ (is (= {::dashboard-ops/error :invalid-op
+ :explanation {:index ["missing required key"]}
+ :op-index 0
+ :op-name "move_tab"}
+ (op-error empty-dash [{:op "move_tab" :tab_id 1}])))
+ (are [op field] (=? {::dashboard-ops/error :invalid-op
+ :op-index 0
+ :op-name (:op op)
+ :explanation #(contains? % field)}
+ (op-error empty-dash [op] {}))
+ ;; GHY-4147: a positive id would silently target a real row
+ {:op "add_card" :id 5 :card_id 42} :id
+ ;; an op the compiler does not know, or none at all
+ {:op "frobnicate"} :op
+ {:id -1} :op
+ ;; GHY-4544: a referenced id left out
+ {:op "remove"} :dashcard_id
+ {:op "remove_tab"} :tab_id
+ {:op "remove_parameter"} :parameter_id
+ {:op "add_parameter" :name "Country" :type "string/="} :parameter_id
+ ;; a value of the wrong type, or a key the op does not take
+ {:op "add_card" :id -1 :card_id 1 :series 5} :series
+ {:op "set_series" :dashcard_id 7 :card_ids 5} :card_ids
+ {:op "add_text" :id -1 :markdown "hi" :card_id 42} :card_id
+ ;; a patch value the compiler could not merge
+ {:op "patch_dashcard" :dashcard_id 7 :patch {:parameter_mappings 5}} :patch
+ {:op "patch_dashcard" :dashcard_id 7 :patch {:parameter_mappings [{:target 5}]}} :patch
+ {:op "patch_dashcard" :dashcard_id 7 :patch {:visualization_settings 5}} :patch
+ {:op "patch_dashcard" :dashcard_id 7 :patch {:inline_parameters 5}} :patch))
+ (testing "every op is checked before any is applied"
+ (is (=? {::dashboard-ops/error :invalid-op, :op-index 1}
+ (op-error empty-dash [{:op "remove" :dashcard_id 999} {:op "frobnicate"}]))))
+ (testing "a null on an optional key reads as the key left out"
+ (is (=? {::dashboard-ops/error :missing-parameter-destination}
+ (op-error (assoc empty-dash :parameters [{:id "p1"}])
+ [{:op "move_parameter" :parameter_id "p1" :index nil :dashcard_id nil}])))))
+
+(deftest ^:parallel prepare-ops-test
+ (testing "prepare-ops checks the ops before anything reads them, so referenced-card-ids sees only valid ones"
+ (is (=? {::dashboard-ops/error :invalid-op, :op-index 0, :explanation #(contains? % :series)}
+ (try (dashboard-ops/referenced-card-ids
+ nil (dashboard-ops/prepare-ops [{:op "add_card" :id -1 :card_id 1 :series 5}]))
+ (catch clojure.lang.ExceptionInfo e (dashboard-ops/error-data e))))))
+ (testing "ops that are not a list are rejected"
+ (is (=? {::dashboard-ops/error :invalid-op}
+ (op-error empty-dash 5 {}))))
+ (testing "a prepared op has no null-valued keys"
+ (is (= [{:op "move" :dashcard_id 7}]
+ (dashboard-ops/prepare-ops [{:op "move" :dashcard_id 7 :tab nil :position nil}]))))
+ (testing "nil means no ops, and ops it already prepared come back as they are"
+ (is (= [] (dashboard-ops/prepare-ops nil)))
+ (let [ops (dashboard-ops/prepare-ops [{:op "remove" :dashcard_id 7}])]
+ (is (identical? ops (dashboard-ops/prepare-ops ops))))))
+
+(deftest ^:parallel referenced-card-ids-test
+ (testing "the cards a compile may need: on the dashcards, placed by the ops, and in configs a parameter op touches"
+ (let [dash {:dashcards [{:id 7 :card_id 9} {:id 8}]
+ :parameters [{:id "p1" :values_source_type "static-list" :values_source_config {:card_id 45}}
+ {:id "p2" :values_source_type "card" :values_source_config {:card_id 46}}]}]
+ (is (= #{9 42 43 45 47}
+ (dashboard-ops/referenced-card-ids
+ dash
+ (dashboard-ops/prepare-ops [{:op "add_card" :id -1 :card_id 42 :series [43]}
+ {:op "update_parameter" :parameter_id "p1" :values_source_type "card"}
+ {:op "add_parameter"
+ :parameter_id "p3"
+ :name "P3"
+ :values_source_type "card"
+ :values_source_config {:card_id 47}}]))))
+ (testing "and nothing from a parameter no op touches"
+ (is (= #{9}
+ (dashboard-ops/referenced-card-ids
+ dash (dashboard-ops/prepare-ops [{:op "remove" :dashcard_id 7}]))))))))
+
+(deftest ^:parallel every-schema-op-has-a-method-test
+ (testing "the op schema and the compiler name the same ops"
+ (is (= (set (map first (mc/children (mr/resolve-schema dashboard-op-schema/op-schema))))
+ (set (keys (methods @#'dashboard-ops/apply-op)))))))
+
+;; not ^:parallel: the `!` in fail! trips the kondo deftest lint
+(deftest error-data-must-match-its-schema-test
+ (testing "throwing an error whose data breaks its schema in `errors` is a bug, caught where it is thrown"
+ (let [e (try (#'dashboard-ops/fail! :unknown-dashcard {:dashcard_id 7})
+ (catch clojure.lang.ExceptionInfo e e))]
+ (is (=? {:code :unknown-dashcard} (ex-data e)))
+ (is (re-find #"Data for dashboard op error :unknown-dashcard does not match its schema" (ex-message e))))
+ (testing "and it is a plain error, not a coded one a tool would word for the model"
+ (is (= {:code :not-a-code}
+ (ex-data (try (#'dashboard-ops/fail! :not-a-code {})
+ (catch clojure.lang.ExceptionInfo e e))))))))
(deftest ^:parallel out-of-range-index-test
- (testing "move_tab and move_parameter reject a missing or out-of-range index, giving the count"
+ (testing "move_tab and move_parameter reject an out-of-range index, giving the count"
(is (=? {::dashboard-ops/error :tab-index-out-of-range, :index 3, :tab-count 1}
(op-error (assoc empty-dash :tabs [{:id 1 :name "A"}])
[{:op "move_tab" :tab_id 1 :index 3}])))
- (is (=? {::dashboard-ops/error :tab-index-out-of-range, :index nil, :tab-count 1}
- (op-error (assoc empty-dash :tabs [{:id 1 :name "A"}])
- [{:op "move_tab" :tab_id 1}])))
(is (=? {::dashboard-ops/error :parameter-index-out-of-range, :index 4, :parameter-count 1}
(op-error (assoc empty-dash :parameters [{:id "p1"}])
[{:op "move_parameter" :parameter_id "p1" :index 4}])))))
@@ -164,7 +227,7 @@
(deftest add-text-test
(testing "GHY-4147: add_text produces a virtual text dashcard with no card_id"
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
empty-dash
[{:op "add_text" :id -1 :markdown "## Hello"}])
dc (first dashcards)]
@@ -174,7 +237,7 @@
(deftest add-heading-test
(testing "GHY-4147: add_heading matches the editor, including the transparent background"
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
empty-dash
[{:op "add_heading" :id -1 :text "Revenue"}])
vs (:visualization_settings (first dashcards))]
@@ -184,7 +247,7 @@
(deftest add-link-url-test
(testing "GHY-4147: add_link with an external url"
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
empty-dash
[{:op "add_link" :id -1 :url "https://example.com"}])
vs (:visualization_settings (first dashcards))]
@@ -193,7 +256,7 @@
(deftest add-link-entity-test
(testing "GHY-4147: add_link with an entity reference stores the entity, not a url"
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
empty-dash
[{:op "add_link" :id -1 :entity {:type "dashboard" :id 12}}])
vs (:visualization_settings (first dashcards))]
@@ -210,7 +273,7 @@
(deftest add-iframe-test
(testing "GHY-4147: add_iframe stores the src in visualization settings"
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
empty-dash
[{:op "add_iframe" :id -1 :src "https://example.com/embed"}])
vs (:visualization_settings (first dashcards))]
@@ -219,7 +282,7 @@
(deftest add-action-test
(testing "GHY-4147: add_action produces an action dashcard with action_id and no card_id"
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
empty-dash
[{:op "add_action" :id -1 :action_id 3 :label "Run" :display "button"}])
dc (first dashcards)]
@@ -232,7 +295,7 @@
(testing "GHY-4147: add_action writes a :virtual_card so the dashcard reads as an action"
;; `isActionDashCard` (frontend/src/metabase/actions/utils.ts) keys off
;; visualization_settings.virtual_card.display; without it the dashcard renders empty, not as a button.
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
empty-dash
[{:op "add_action" :id -1 :action_id 3 :label "Run"}])
vs (:visualization_settings (first dashcards))]
@@ -246,7 +309,7 @@
(let [existing {:id 7 :card_id 9 :row 0 :col 0 :size_x 4 :size_y 4 :dashboard_tab_id nil
:visualization_settings {:card.title "Original"}
:parameter_mappings [{:parameter_id "p1" :card_id 9 :target ["dimension" ["field" 1 nil]]}]}
- {:keys [dashcards]} (compile-ops
+ {:keys [dashcards]} (compile-with-stub-cards
(dash-with [existing])
[{:op "duplicate_card" :id -1 :dashcard_id 7}])
clone (first (filter #(= -1 (:id %)) dashcards))]
@@ -270,7 +333,7 @@
:visualization_settings
{:visualization "table"
:columnValuesMapping {:COLUMN_1 [{:sourceId "card:7" :originalName "NAME" :name "COLUMN_1"}]}}}
- {:keys [dashcards]} (compile-ops
+ {:keys [dashcards]} (compile-with-stub-cards
(dash-with [existing])
[{:op "duplicate_card" :id -1 :dashcard_id 7}])
clone (first (filter #(= -1 (:id %)) dashcards))]
@@ -287,7 +350,7 @@
:visualization_settings {}
:inline_parameters ["p1"]
:parameter_mappings [mapping]}
- {:keys [dashcards]} (compile-ops
+ {:keys [dashcards]} (compile-with-stub-cards
(assoc empty-dash
:dashcards [existing]
:parameters [{:id "p1" :name "Cat" :slug "cat" :type "string/="}])
@@ -315,7 +378,7 @@
{:id 8 :card_id 9 :row 4 :col 0 :size_x 4 :size_y 4
:dashboard_tab_id 5 :visualization_settings {}
:inline_parameters ["p2"]}]}
- {:keys [dashcards]} (compile-ops
+ {:keys [dashcards]} (compile-with-stub-cards
current
[{:op "duplicate_tab" :id -1 :tab_id 5}])
clones (filterv #(= -1 (:dashboard_tab_id %)) dashcards)]
@@ -325,6 +388,11 @@
(is (= {"p1" [7] "p2" [8]} (inline-parameter-owners dashcards))
"each parameter is still owned by exactly the dashcard it was placed on"))))
+(defn- p1-dash
+ "A dashboard holding `dashcard` and a parameter `p1`."
+ [dashcard]
+ (assoc (dash-with [dashcard]) :parameters [{:id "p1" :name "P1" :type "category"}]))
+
(def ^:private a-dashcard
{:id 7 :card_id 9 :row 2 :col 3 :size_x 4 :size_y 4 :dashboard_tab_id nil
:visualization_settings {:card.title "Old"}
@@ -333,7 +401,7 @@
(deftest replace-card-resets-content-test
(testing "GHY-4147: replace_card keeps the dashcard id and resets series, mappings, and viz settings"
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
(dash-with [a-dashcard])
[{:op "replace_card" :dashcard_id 7 :card_id 99}])
dc (first dashcards)]
@@ -347,7 +415,7 @@
(deftest move-and-resize-test
(testing "GHY-4147: move relocates, resize resizes, neither disturbs the other"
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
(dash-with [a-dashcard])
[{:op "move" :dashcard_id 7 :position {:row 0 :col 0}}
{:op "resize" :dashcard_id 7 :size {:size_x 8 :size_y 2}}])
@@ -359,7 +427,7 @@
moved card is not its own sibling — counting it means the search can never return the
slot it already occupies, so a card alone on a tab is displaced by its own footprint"
(let [wide {:id 7 :card_id 9 :row 0 :col 0 :size_x 24 :size_y 4 :dashboard_tab_id nil}
- {:keys [dashcards]} (compile-ops
+ {:keys [dashcards]} (compile-with-stub-cards
(dash-with [wide])
[{:op "move" :dashcard_id 7}])
dc (first dashcards)]
@@ -367,21 +435,21 @@
(deftest remove-test
(testing "GHY-4147: remove drops the dashcard from the payload, which deletes it on save"
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
(dash-with [a-dashcard])
[{:op "remove" :dashcard_id 7}])]
(is (= [] dashcards)))))
(deftest set-series-test
(testing "GHY-4147: set_series is an ordered full replace"
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
(dash-with [a-dashcard])
[{:op "set_series" :dashcard_id 7 :card_ids [21 20]}])]
(is (= [{:id 21} {:id 20}] (:series (first dashcards)))))))
(deftest patch-dashcard-merges-content-test
(testing "GHY-4147: patch_dashcard merges into visualization_settings rather than replacing them"
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
(dash-with [a-dashcard])
[{:op "patch_dashcard" :dashcard_id 7
:patch {:visualization_settings {:card.description "New"}}}])
@@ -389,6 +457,105 @@
(is (= "Old" (:card.title vs)))
(is (= "New" (:card.description vs))))))
+(deftest patch-dashcard-from-a-strict-client-test
+ (testing "a patch whose other properties are null, as strict clients send them, leaves those properties alone"
+ (let [mapping {:parameter_id "p1" :card_id 9 :target [:dimension [:field 1 nil]]}
+ dc (-> (compile-with-stub-cards
+ (p1-dash (assoc a-dashcard :parameter_mappings [mapping] :inline_parameters ["p1"]))
+ [{:op "patch_dashcard"
+ :dashcard_id 7
+ :patch {:visualization_settings {:card.description "New"}
+ :parameter_mappings nil
+ :inline_parameters nil}}])
+ :dashcards
+ first)]
+ (is (=? {:visualization_settings {:card.description "New"}
+ :parameter_mappings [mapping]
+ :inline_parameters ["p1"]}
+ dc))))
+ (testing "a patched mapping keeps the series card it names"
+ (is (=? [{:parameter_id "p1", :card_id 11, :target [:dimension [:field 1 nil]]}]
+ (-> (compile-with-stub-cards
+ (p1-dash a-dashcard)
+ [{:op "patch_dashcard"
+ :dashcard_id 7
+ :patch {:parameter_mappings [{:parameter_id "p1"
+ :card_id 11
+ :target ["dimension" ["field" 1 nil]]}]}}])
+ :dashcards
+ first
+ :parameter_mappings))))
+ (testing "a patched mapping naming a card that is not on the dashcard is refused"
+ (is (=? {::dashboard-ops/error :mapping-card-not-on-dashcard, :dashcard-id 7, :card-id 3, :op-index 0}
+ (op-error (p1-dash a-dashcard)
+ [{:op "patch_dashcard"
+ :dashcard_id 7
+ :patch {:parameter_mappings [{:parameter_id "p1"
+ :card_id 3
+ :target ["dimension" ["field" 1 nil]]}]}}]))))
+ (testing "a patched mapping without a parameter or a target is refused by the op schema"
+ (doseq [k [:parameter_id :target]]
+ (testing k
+ (is (=? {::dashboard-ops/error :invalid-op, :explanation {:patch {:parameter_mappings [{k some?}]}}}
+ (op-error (p1-dash a-dashcard)
+ [{:op "patch_dashcard"
+ :dashcard_id 7
+ :patch {:parameter_mappings [(dissoc {:parameter_id "p1"
+ :target ["dimension" ["field" 1 nil]]}
+ k)]}}])))))))
+
+(defn- patch-mappings
+ "The op that patches dashcard 7's mappings to `mappings`."
+ [mappings]
+ {:op "patch_dashcard" :dashcard_id 7 :patch {:parameter_mappings mappings}})
+
+(deftest patch-dashcard-mapping-checks-test
+ (testing "a patched mapping without a card_id gets the dashcard's card, which the query processor matches on"
+ (is (=? {:dashcards [{:parameter_mappings [{:parameter_id "p1", :card_id 9}]}]}
+ (compile-with-stub-cards (p1-dash a-dashcard)
+ [(patch-mappings [{:parameter_id "p1" :target ["dimension" ["field" 1 nil]]}])]))))
+ (testing "a new mapping must name a parameter on the dashboard"
+ (is (=? {::dashboard-ops/error :unknown-parameter, :parameter-id "nope", :op-index 0}
+ (op-error (p1-dash a-dashcard)
+ [(patch-mappings [{:parameter_id "nope" :target ["dimension" ["field" 1 nil]]}])]))))
+ (testing "a new mapping the save would refuse fails in the op, with its index"
+ (is (=? {::dashboard-ops/error :invalid-op, :op-index 0, :explanation {:target some?}}
+ (op-error (p1-dash a-dashcard)
+ [(patch-mappings [{:parameter_id "p1" :target ["nonsense"]}])]))))
+ (testing "a mapping takes only its three keys"
+ (is (=? {::dashboard-ops/error :invalid-op, :op-index 0}
+ (op-error (p1-dash a-dashcard)
+ [(patch-mappings [{:parameter_id "p1" :target ["dimension" ["field" 1 nil]] :extra 1}])]))))
+ (testing "a stored mapping sent back unchanged is not checked again, so a stale one doesn't block the patch"
+ (let [stale {:parameter_id "gone" :card_id 9 :target [:dimension [:field 1 nil]]}]
+ (is (=? {:dashcards [{:parameter_mappings [stale]}]}
+ (compile-with-stub-cards (p1-dash (assoc a-dashcard :parameter_mappings [stale]))
+ [(patch-mappings [{:parameter_id "gone"
+ :card_id 9
+ :target ["dimension" ["field" 1 nil]]}])]))))))
+
+(deftest set-series-drops-mappings-to-removed-series-test
+ (testing "set_series drops the mappings on series cards it removes, and keeps the rest"
+ (let [mappings [{:parameter_id "p1" :card_id 9 :target [:dimension [:field 1 nil]]}
+ {:parameter_id "p1" :card_id 11 :target [:dimension [:field 2 nil]]}
+ {:parameter_id "p1" :card_id 12 :target [:dimension [:field 3 nil]]}]
+ dc (assoc a-dashcard :series [{:id 11} {:id 12}] :parameter_mappings mappings)]
+ (is (=? {:dashcards [{:series [{:id 12}]
+ :parameter_mappings [{:card_id 9} {:card_id 12}]}]}
+ (compile-with-stub-cards (p1-dash dc) [{:op "set_series" :dashcard_id 7 :card_ids [12]}])))
+ (is (=? {:dashcards [{:series [], :parameter_mappings [{:card_id 9}]}]}
+ (compile-with-stub-cards (p1-dash dc) [{:op "set_series" :dashcard_id 7 :card_ids []}]))))))
+
+(deftest payload-series-are-ids-test
+ (testing "the payload's series carry only card ids, which is all the save's dashcard schema allows"
+ (is (= [{:id 11}]
+ (-> (compile-with-stub-cards
+ (dash-with [(assoc a-dashcard :series [{:id 11 :name "Overlay" :display "bar"}])])
+ [{:op "resize" :dashcard_id 7 :size {:size_x 6 :size_y 4}}])
+ :dashcards
+ first
+ :series)))))
+
(deftest patch-dashcard-rejects-layout-keys-test
(testing "GHY-4147: layout and identity keys in a patch are rejected, naming the op that owns them"
(doseq [[k use-op] {:row "move" :size_x "resize" :card_id "replace_card"}]
@@ -406,11 +573,19 @@
(is (=? {::dashboard-ops/error :unknown-patch-key, :key "bogus", :op-index 1}
(op-error empty-dash
[{:op "add_card" :id -1 :card_id 42}
- {:op "patch_dashcard" :dashcard_id -1 :patch {:bogus 1}}]))))))
+ {:op "patch_dashcard" :dashcard_id -1 :patch {:bogus 1}}])))))
+ (testing "a key that is not a keyword is rejected too, rather than crashing"
+ (is (=? {::dashboard-ops/error :unknown-patch-key, :key "5"}
+ (op-error (dash-with [a-dashcard])
+ [{:op "patch_dashcard" :dashcard_id 7 :patch {5 "x"}}]))))
+ (testing "a string key is named as it is, not quoted twice"
+ (is (=? {::dashboard-ops/error :unknown-patch-key, :key "foo"}
+ (op-error (dash-with [a-dashcard])
+ [{:op "patch_dashcard" :dashcard_id 7 :patch {"foo" "x"}}])))))
(deftest ops-apply-in-order-test
(testing "GHY-4147: a later op sees the effect of an earlier one — add then move the same new card"
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
empty-dash
[{:op "add_card" :id -1 :card_id 42}
{:op "move" :dashcard_id -1 :position {:row 9 :col 1}}])]
@@ -418,7 +593,7 @@
(deftest add-tab-and-place-a-card-in-it-test
(testing "GHY-4147: a card can reference a tab created earlier in the same batch by its negative id"
- (let [{:keys [tabs dashcards]} (compile-ops
+ (let [{:keys [tabs dashcards]} (compile-with-stub-cards
empty-dash
[{:op "add_tab" :id -1 :name "Q3"}
{:op "add_card" :id -2 :card_id 42 :tab -1}])]
@@ -427,7 +602,7 @@
(deftest rename-tab-test
(testing "GHY-4147: rename_tab changes only the name"
- (let [{:keys [tabs]} (compile-ops
+ (let [{:keys [tabs]} (compile-with-stub-cards
(assoc empty-dash :tabs [{:id 5 :name "Old" :position 0}])
[{:op "rename_tab" :tab_id 5 :name "New"}])]
(is (= "New" (:name (first tabs))))
@@ -435,7 +610,7 @@
(deftest move-tab-reorders-test
(testing "GHY-4147: move_tab reorders the vector; update-dashboard! derives :position from the index"
- (let [{:keys [tabs]} (compile-ops
+ (let [{:keys [tabs]} (compile-with-stub-cards
(assoc empty-dash :tabs [{:id 5 :name "A"} {:id 6 :name "B"} {:id 7 :name "C"}])
[{:op "move_tab" :tab_id 7 :index 0}])]
(is (= [7 5 6] (mapv :id tabs))))))
@@ -447,7 +622,7 @@
:parameters []
:dashcards [{:id 7 :card_id 9 :row 0 :col 0 :size_x 4 :size_y 4
:dashboard_tab_id 5 :visualization_settings {}}]}
- {:keys [tabs dashcards]} (compile-ops
+ {:keys [tabs dashcards]} (compile-with-stub-cards
current
[{:op "duplicate_tab" :id -1 :tab_id 5}])]
(is (= [5 -1] (mapv :id tabs)))
@@ -467,7 +642,7 @@
:dashboard_tab_id 5 :visualization_settings {}}
{:id 8 :card_id 9 :row 4 :col 0 :size_x 4 :size_y 4
:dashboard_tab_id 5 :visualization_settings {}}]}
- {:keys [dashcards]} (compile-ops
+ {:keys [dashcards]} (compile-with-stub-cards
current
[{:op "duplicate_tab" :id -1 :tab_id 5}
{:op "add_card" :id -10 :card_id 9 :tab -1}]
@@ -483,7 +658,7 @@
:parameters []
:dashcards [{:id 7 :card_id 9 :dashboard_tab_id 5 :row 0 :col 0 :size_x 4 :size_y 4}
{:id 8 :card_id 9 :dashboard_tab_id 6 :row 0 :col 0 :size_x 4 :size_y 4}]}
- {:keys [tabs dashcards]} (compile-ops current [{:op "remove_tab" :tab_id 5}])]
+ {:keys [tabs dashcards]} (compile-with-stub-cards current [{:op "remove_tab" :tab_id 5}])]
(is (= [6] (mapv :id tabs)))
(is (= [8] (mapv :id dashcards))))))
@@ -498,7 +673,7 @@
(deftest single-tab-coverage-is-not-enforced-test
(testing "GHY-4147: with exactly one tab, a tabless card is allowed — update-dashboard! back-fills
its dashboard_tab_id itself (api.clj), so the compiler must not reject it early"
- (is (some? (compile-ops
+ (is (some? (compile-with-stub-cards
empty-dash
[{:op "add_tab" :id -1 :name "Q3"}
{:op "add_card" :id -2 :card_id 42}])))))
@@ -542,7 +717,7 @@
(deftest add-parameter-test
(testing "GHY-4147: add_parameter appends a parameter carrying the caller's id, REST names intact"
- (let [{:keys [parameters]} (compile-ops
+ (let [{:keys [parameters]} (compile-with-stub-cards
empty-dash
[{:op "add_parameter" :parameter_id "p_date" :name "Date"
:type "date/all-options" :isMultiSelect false}]
@@ -553,18 +728,12 @@
(deftest add-parameter-derives-slug-from-name-test
(testing "GHY-4147: a created parameter gets the URL slug the editor would give it, so it is
addressable by slug in embedding, public links, and URL param sync"
- (let [{:keys [parameters]} (compile-ops
+ (let [{:keys [parameters]} (compile-with-stub-cards
empty-dash
[{:op "add_parameter" :parameter_id "p1" :name "Order Total"
:type "number/>="}]
{})]
- (is (= "order_total" (:slug (first parameters)))))
- (testing "a nameless parameter gets no slug rather than a bogus one"
- (let [{:keys [parameters]} (compile-ops
- empty-dash
- [{:op "add_parameter" :parameter_id "p1" :type "string/="}]
- {})]
- (is (not (contains? (first parameters) :slug)))))))
+ (is (= "order_total" (:slug (first parameters)))))))
(deftest add-parameter-rejects-duplicate-id-test
(testing "GHY-4147: reusing an existing parameter id is an error"
@@ -574,7 +743,7 @@
(deftest update-parameter-merges-test
(testing "GHY-4147: update_parameter merges into the existing parameter, re-slugging on a rename"
- (let [{:keys [parameters]} (compile-ops
+ (let [{:keys [parameters]} (compile-with-stub-cards
(assoc empty-dash :parameters [{:id "p1" :name "X" :type "string/="}])
[{:op "update_parameter" :parameter_id "p1" :name "Y"}]
{})]
@@ -583,7 +752,7 @@
(deftest update-parameter-preserves-slug-when-not-renaming-test
(testing "GHY-4147: an update that does not touch the name leaves the slug alone, so an edit to
an editor-created parameter cannot silently break its existing URLs"
- (let [{:keys [parameters]} (compile-ops
+ (let [{:keys [parameters]} (compile-with-stub-cards
(assoc empty-dash
:parameters [{:id "p1" :name "Order-Date" :slug "order-date"
:type "date/all-options"}])
@@ -602,7 +771,7 @@
:parameter_mappings [{:parameter_id "p1" :card_id 9 :target [:dimension [:field 1 nil]]}
{:parameter_id "p2" :card_id 9
:target [:dimension [:field 2 nil]]}]}]}
- {:keys [parameters dashcards]} (compile-ops
+ {:keys [parameters dashcards]} (compile-with-stub-cards
current [{:op "remove_parameter" :parameter_id "p1"}] {})
dc (first dashcards)]
(is (= ["p2"] (mapv :id parameters)))
@@ -612,7 +781,7 @@
(deftest move-parameter-reorders-header-test
(testing "GHY-4147: move_parameter with an index reorders the header"
- (let [{:keys [parameters]} (compile-ops
+ (let [{:keys [parameters]} (compile-with-stub-cards
(assoc empty-dash :parameters [{:id "a"} {:id "b"} {:id "c"}])
[{:op "move_parameter" :parameter_id "c" :index 0}]
{})]
@@ -631,7 +800,7 @@
(testing "GHY-4147: move_parameter with a dashcard_id makes it an inline filter on that card"
(let [current {:id 1 :tabs [] :parameters [{:id "p1"}]
:dashcards [{:id 7 :card_id 9 :row 0 :col 0 :size_x 4 :size_y 4}]}
- {:keys [dashcards]} (compile-ops
+ {:keys [dashcards]} (compile-with-stub-cards
current [{:op "move_parameter" :parameter_id "p1" :dashcard_id 7}] {})]
(is (= ["p1"] (:inline_parameters (first dashcards)))))))
@@ -643,7 +812,7 @@
:inline_parameters ["p1"]}
{:id 8 :card_id 9 :row 4 :col 0 :size_x 4 :size_y 4}]}
op {:op "move_parameter" :parameter_id "p1" :dashcard_id 8}
- {:keys [dashcards]} (compile-ops current [op op] {})]
+ {:keys [dashcards]} (compile-with-stub-cards current [op op] {})]
(is (= [] (:inline_parameters (first dashcards))))
(is (= ["p1"] (:inline_parameters (second dashcards)))))))
@@ -656,7 +825,7 @@
current {:id 1 :tabs [] :parameters [{:id "p1"}]
:dashcards [{:id 7 :card_id 9 :row 0 :col 0 :size_x 4 :size_y 4}
bystander legacy]}
- {:keys [dashcards]} (compile-ops
+ {:keys [dashcards]} (compile-with-stub-cards
current
[{:op "move_parameter" :parameter_id "p1" :dashcard_id 7}] {})]
(is (= bystander (nth dashcards 1)))
@@ -673,7 +842,7 @@
:parameter_mappings [{:parameter_id "p1" :card_id 9
:target [:dimension [:field 1 nil]]}]}
bystander legacy]}
- {:keys [dashcards]} (compile-ops
+ {:keys [dashcards]} (compile-with-stub-cards
current
[{:op "unwire_parameter" :parameter_id "p1"}] {})]
(is (= [] (:parameter_mappings (first dashcards))))
@@ -688,7 +857,7 @@
{:keys [dashcards]} (dynamic-redefs/with-dynamic-fn-redefs [mapping-targets/target-for-field
(fn [_card _param field-id]
[:dimension [:field field-id nil]])]
- (compile-ops
+ (compile-with-stub-cards
current
[{:op "wire_parameter" :parameter_id "p1" :dashcard_id 7 :target_field 55}]
{9 a-card}))]
@@ -704,7 +873,7 @@
{:keys [dashcards]} (dynamic-redefs/with-dynamic-fn-redefs [mapping-targets/target-for-field
(fn [_card _param field-id]
[:dimension [:field field-id nil]])]
- (compile-ops
+ (compile-with-stub-cards
current
[{:op "wire_parameter" :parameter_id "p1" :dashcard_id 7 :target_field 55}
{:op "wire_parameter" :parameter_id "p1" :dashcard_id 7 :target_field 66}]
@@ -742,7 +911,7 @@
(fn [card _param field-id]
(when (= 9 (:id card))
[:dimension [:field field-id nil]]))]
- (compile-ops
+ (compile-with-stub-cards
current
[{:op "wire_parameter" :parameter_id "p1" :dashcard_id 7
:target_field 55 :autowire true}]
@@ -760,12 +929,12 @@
:parameter_mappings [{:parameter_id "p1" :card_id 10
:target [:dimension [:field 2 nil]]}]}]}]
(testing "one card"
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
current [{:op "unwire_parameter" :parameter_id "p1" :dashcard_id 7}] {})]
(is (= [] (:parameter_mappings (first dashcards))))
(is (= 1 (count (:parameter_mappings (second dashcards)))))))
(testing "all cards"
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
current [{:op "unwire_parameter" :parameter_id "p1"}] {})]
(is (every? (comp empty? :parameter_mappings) dashcards)))))))
@@ -795,7 +964,7 @@
(defn- wire-native
[op]
- (-> (compile-ops native-current [op] {9 native-card})
+ (-> (compile-with-stub-cards native-current [op] {9 native-card})
:dashcards first :parameter_mappings))
(defn- wire-native-error
@@ -827,11 +996,36 @@
:tag-type "snippet"
:wireable-tags #(some #{"cat"} %)}
(wire-native-error {:op "wire_parameter" :parameter_id "p1" :dashcard_id 7 :target_tag "snippet: base"}))))
- (testing "a dashcard with no card behind it has nothing to wire"
- (is (=? {::dashboard-ops/error :no-card-behind-dashcard, :dashcard-id 7}
- (op-error (assoc native-current :dashcards [{:id 7 :row 0 :col 0 :size_x 4 :size_y 4
- :visualization_settings {:virtual_card {:display "text"}}}])
- [{:op "wire_parameter" :parameter_id "p1" :dashcard_id 7 :target_tag "cat"}])))))
+ (testing "a dashcard with no card behind it has nothing to wire, by tag or by field"
+ (let [text-card {:id 7 :row 0 :col 0 :size_x 4 :size_y 4
+ :visualization_settings {:virtual_card {:display "text"}}}]
+ (doseq [target [{:target_tag "cat"} {:target_field 18}]]
+ (testing (pr-str target)
+ (is (=? {::dashboard-ops/error :no-card-behind-dashcard, :dashcard-id 7}
+ (op-error (assoc native-current :dashcards [text-card])
+ [(merge {:op "wire_parameter" :parameter_id "p1" :dashcard_id 7} target)]))))))))
+
+(deftest wire-parameter-unresolved-card-test
+ (testing "wiring a dashcard whose card is missing or unreadable names that card, for every kind of target"
+ (doseq [target [{:target_field 18}
+ {:target_tag "cat"}
+ {:target ["dimension" ["template-tag" "cat"]]}]]
+ (testing (pr-str target)
+ (is (=? {::dashboard-ops/error :unknown-card, :card-id 9, :op-name "wire_parameter"}
+ (op-error native-current
+ [(merge {:op "wire_parameter" :parameter_id "p1" :dashcard_id 7} target)]
+ {}))))))
+ (testing "autowire still skips another dashcard whose card is unresolved"
+ (let [current (update native-current :dashcards conj
+ {:id 8 :card_id 10 :row 4 :col 0 :size_x 4 :size_y 4 :parameter_mappings []})]
+ (is (=? {:dashcards [{:id 7 :parameter_mappings [{:parameter_id "p1"}]}
+ {:id 8 :parameter_mappings []}]}
+ (dynamic-redefs/with-dynamic-fn-redefs [mapping-targets/target-for-field
+ (fn [_card _param field-id] [:dimension [:field field-id nil]])]
+ (compile-with-stub-cards
+ current
+ [{:op "wire_parameter" :parameter_id "p1" :dashcard_id 7 :target_field 18 :autowire true}]
+ {9 native-card})))))))
(deftest wire-parameter-raw-target-coerced-and-validated-test
(testing "a raw target arrives as JSON strings, is keywordized, and saves when it resolves"
@@ -856,7 +1050,7 @@
:text "Sales in {{region}} for {{period}}."}}
current (assoc native-current :dashcards [text-dashcard])]
(testing "a text-tag target binds a {{placeholder}} in a text card's own markdown — no card needed"
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
current
[{:op "wire_parameter" :parameter_id "p1" :dashcard_id 7
:target ["text-tag" "region"]}]
@@ -864,7 +1058,7 @@
(is (= [{:parameter_id "p1" :card_id nil :target [:text-tag "region"]}]
(:parameter_mappings (first dashcards))))))
(testing "an iframe card's embed carries placeholders too"
- (let [{:keys [dashcards]} (compile-ops
+ (let [{:keys [dashcards]} (compile-with-stub-cards
(assoc current :dashcards
[{:id 7 :row 0 :col 0 :size_x 12 :size_y 8
:visualization_settings {:virtual_card {:display "iframe"}
@@ -931,7 +1125,10 @@
(frequencies (:tabs (#'dashboard-ops/move-row rows-state :tabs 11 0))))))
(testing "nil when the index falls outside [0, count-after-removal], so the op owns its error"
(is (nil? (#'dashboard-ops/move-row rows-state :tabs 11 -1)))
- (is (nil? (#'dashboard-ops/move-row rows-state :tabs 11 3)))))
+ (is (nil? (#'dashboard-ops/move-row rows-state :tabs 11 3))))
+ (testing "nil for an index that is not an integer, whoever calls it"
+ (is (nil? (#'dashboard-ops/move-row rows-state :tabs 11 nil)))
+ (is (nil? (#'dashboard-ops/move-row rows-state :tabs 11 "1")))))
(deftest upsert-mapping-test
(let [dc {:id 7 :card_id 9
@@ -1000,7 +1197,7 @@
(#'dashboard-ops/drop-inline-parameter "p1")
(#'dashboard-ops/drop-mapping "p1"))))]
(is (= via-primitives
- (compile-ops current [{:op "remove_parameter" :parameter_id "p1"}] {}))))))
+ (compile-with-stub-cards current [{:op "remove_parameter" :parameter_id "p1"}] {}))))))
(deftest ^:parallel unresolved-card-test
(testing "an op naming a card the caller did not resolve fails, naming that card, wherever the op names it"
@@ -1024,13 +1221,45 @@
[{:op "update_parameter" :parameter_id "p1"
:values_source_type "card" :values_source_config {:card_id 46}}]
{}))))
- (testing "ops are checked in order, so an earlier op's error is reported first"
- (is (=? {::dashboard-ops/error :unknown-op, :op-index 0}
- (op-error empty-dash [{:op "frobnicate"} {:op "add_card" :id -1 :card_id 42}] {}))))
+ (testing "ops are applied in order, so an earlier op's error is reported first"
+ (is (=? {::dashboard-ops/error :unknown-dashcard, :op-index 0}
+ (op-error empty-dash [{:op "remove" :dashcard_id 999} {:op "add_card" :id -1 :card_id 42}] {}))))
(testing "an op's own error is reported before an unresolved card it names"
- (is (=? {::dashboard-ops/error :unknown-op}
- (op-error empty-dash [{:op "frobnicate" :card_id 42}] {})))
(is (=? {::dashboard-ops/error :unknown-dashcard, :dashcard-id 12345}
(op-error empty-dash [{:op "replace_card" :dashcard_id 12345 :card_id 42}] {}))))
- (testing "a card key the op does not use is not checked"
- (is (nil? (op-error empty-dash [{:op "add_text" :id -1 :markdown "hi" :card_id 42}] {})))))
+ (let [dash (assoc empty-dash :parameters [{:id "p1"
+ :values_source_type "static-list"
+ :values_source_config {:card_id 45 :values ["a"]}}])]
+ (testing "a source card the parameter already had is checked once an op makes it the source"
+ (is (=? {::dashboard-ops/error :unknown-card, :card-id 45, :op-name "update_parameter"}
+ (op-error dash [{:op "update_parameter" :parameter_id "p1" :values_source_type "card"}] {})))
+ (is (nil? (op-error dash [{:op "update_parameter" :parameter_id "p1" :values_source_type "card"}]
+ {45 {:id 45}}))))
+ (testing "a card in the config of a parameter whose source is not a card goes unchecked, as on save"
+ (is (nil? (op-error dash [{:op "update_parameter"
+ :parameter_id "p1"
+ :values_source_config {:card_id 46 :values ["b"]}}]
+ {})))))
+ (testing "a source card the parameter already had is not checked again by an op that leaves it alone, as on save"
+ (is (nil? (op-error (assoc empty-dash :parameters [{:id "p1"
+ :values_source_type "card"
+ :values_source_config {:card_id 45}}])
+ [{:op "update_parameter" :parameter_id "p1" :name "Renamed"}]
+ {}))))
+ (testing "a card the dashboard already references may be named again without a row, as on save"
+ (let [dash (assoc empty-dash
+ :dashcards [{:id 7 :card_id 9 :row 0 :col 0 :size_x 4 :size_y 4 :series [{:id 11}]}]
+ :parameters [{:id "p1" :values_source_type "card" :values_source_config {:card_id 45}}
+ {:id "p2"}])]
+ (is (nil? (op-error dash
+ [{:op "set_series" :dashcard_id 7 :card_ids [11]}
+ {:op "add_card" :id -1 :card_id 9}
+ {:op "update_parameter"
+ :parameter_id "p2"
+ :values_source_type "card"
+ :values_source_config {:card_id 45}}
+ {:op "update_parameter" :parameter_id "p1" :values_source_type "static-list"}
+ {:op "update_parameter" :parameter_id "p1" :values_source_type "card"}]
+ {})))
+ (is (=? {::dashboard-ops/error :unknown-card, :card-id 12}
+ (op-error dash [{:op "set_series" :dashcard_id 7 :card_ids [11 12]}] {}))))))