The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Strip undeclared keys from stored and legacy queries the way the request decoder does (#82273)
Alexander Polyankin6e74e41eaffe
diff --git a/src/metabase/lib/serialize.cljc b/src/metabase/lib/serialize.cljcindex 5bb12a8b831..e596f6b42e2 100644--- a/src/metabase/lib/serialize.cljc+++ b/src/metabase/lib/serialize.cljc@@ -28,13 +28,19 @@(mr/cached ::deserializerschema(fn []- (mc/decoder schema (mtx/transformer {:name :api})))))+ (mc/decoder schema (mtx/transformer+ {:name :api}+ (mtx/strip-extra-keys-transformer))))))(defn prepare-after-deserialization"Inverse of [[prepare-for-serialization]]: run on a query `x` right after it is decoded from JSON coming in from aREST API request or the application database, to strip the internal query-processor keys that the query processor addsitself during preprocessing -- after this runs -- so stripping them here has no effect on legitimately-added keys.- Stripping logic is defined in the schemas; grep for `:decode/api` in the `metabase.lib.schema*` namespaces."+ Stripping logic is defined in the schemas; grep for `:decode/api` in the `metabase.lib.schema*` namespaces.++ Also drops, from every `{:closed true}` map the schema reaches, the keys the schema does not declare, the same way the+ request decoder in `metabase.api.macros` does: a key some older release or client once wrote into a stored query is+ ignored rather than left to fail validation when the query runs."([x](prepare-after-deserialization ::lib.schema/query x))diff --git a/test/metabase/lib/serialize_test.cljc b/test/metabase/lib/serialize_test.cljcindex 237663ad8d1..d851a5a86d0 100644--- a/test/metabase/lib/serialize_test.cljc+++ b/test/metabase/lib/serialize_test.cljc@@ -30,6 +30,30 @@(testing (str schema " via " f)(is (= m (f schema (assoc m :a/b 1)))))))+(deftest ^:parallel strip-undeclared-keys-test+ (testing "a key a closed map does not declare is dropped on the way in, the way the request decoder drops it"+ (is (= {:type :text, :name "t", :display-name "T", :id "id1"}+ (lib.serialize/prepare-after-deserialization+ ::lib.schema.template-tag/template-tag+ {:type :text, :name "t", :display-name "T", :id "id1", :bogus 1})))+ (testing "wherever it is in a query"+ (is (= {:lib/type :mbql/query+ :database 1+ :stages [{:lib/type :mbql.stage/native+ :native "SELECT 1"+ :template-tags [{:type :text, :name "t", :display-name "T", :id "id1"}]}]}+ (lib.serialize/prepare-after-deserialization+ {:lib/type :mbql/query+ :database 1+ :stages [{:lib/type :mbql.stage/native+ :native "SELECT 1"+ :template-tags [{:type :text, :name "t", :display-name "T", :id "id1", :bogus 1}]}]}))))+ (testing "but not on the way out: serialization does not judge what it did not produce"+ (is (= {:type :text, :name "t", :display-name "T", :id "id1", :bogus 1}+ (lib.serialize/prepare-for-serialization+ ::lib.schema.template-tag/template-tag+ {:type :text, :name "t", :display-name "T", :id "id1", :bogus 1}))))))+(deftest ^:parallel remove-info-test(is (= {:lib/type :mbql/query:stages [{:lib/type :mbql.stage/mbql