Skip to content

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

Fix parameter tags reordering in the presence of snippet tags (#71209)

Romeo Van Snick0ab5a0f603ec
diff --git a/e2e/test/scenarios/native/native-reproductions.cy.spec.ts b/e2e/test/scenarios/native/native-reproductions.cy.spec.ts
index 147bbf41f08..ba97dabcb72 100644
--- a/e2e/test/scenarios/native/native-reproductions.cy.spec.ts
+++ b/e2e/test/scenarios/native/native-reproductions.cy.spec.ts
@@ -1016,3 +1016,43 @@ describe("issue 59075", () => {
});
});
});
+
+describe("issue 69160", () => {
+ beforeEach(() => {
+ H.restore();
+ cy.signInAsNormalUser();
+
+ H.createSnippet({
+ name: "A and B",
+ content: "{{ a }} and {{ b }}",
+ });
+ H.startNewNativeQuestion();
+ H.NativeEditor.type("{{ snippet: A and B }}", {
+ allowFastSet: true,
+ }).blur();
+ });
+
+ const options = {
+ force: true,
+ isPrimary: true,
+ button: 0,
+ };
+
+ it("should be possible to reorder parameters when there are snippets in the query (metabase#69160)", () => {
+ cy.log("reorder parameters");
+ cy.findByTestId("native-query-top-bar")
+ .findAllByRole("listitem")
+ .first()
+ .as("param");
+
+ cy.get("@param").trigger("pointerdown", 5, 5, options).wait(200);
+ cy.get("@param").trigger("pointermove", 20, 20, options).wait(200);
+ cy.get("@param").trigger("pointermove", 200, 0, options).wait(200);
+ cy.get("@param").trigger("pointerup", options).wait(200);
+
+ cy.findByTestId("native-query-top-bar")
+ .findAllByRole("textbox")
+ .first()
+ .should("have.attr", "placeholder", "B");
+ });
+});
diff --git a/frontend/src/metabase-lib/v1/queries/NativeQuery.ts b/frontend/src/metabase-lib/v1/queries/NativeQuery.ts
index 53215de8cf5..0768f6f5325 100644
--- a/frontend/src/metabase-lib/v1/queries/NativeQuery.ts
+++ b/frontend/src/metabase-lib/v1/queries/NativeQuery.ts
@@ -235,12 +235,22 @@ export default class NativeQuery {
// template tags, and the order is determined by the key order
const query = this._query();
const tags = this.templateTags();
- const oldIndex = tags.findIndex((tag) => tag.id === id);
- const newTags = [...tags];
+ // NOTE: The snippet tags are part of the snippet tags map, but they do
+ // not appear in the parameter lists. To correctly reorder the parameters
+ // and keep track of indexes, we need to pluck out the snippet tags first.
+ // We then tack them on before returning the reordered tags map.
+ // The order of snippet tags is not consequential, which allows us to do this.
+ const nonSnippetTags = tags.filter((tag) => tag.type !== "snippet");
+ const snippetTags = tags.filter((tag) => tag.type === "snippet");
+
+ const oldIndex = nonSnippetTags.findIndex((tag) => tag.id === id);
+ const newTags = [...nonSnippetTags];
newTags.splice(newIndex, 0, newTags.splice(oldIndex, 1)[0]);
+
+ const newTagsWithSnippets = [...snippetTags, ...newTags];
const newTagsMap = Object.fromEntries(
- newTags.map((tag) => [tag.name, tag]),
+ newTagsWithSnippets.map((tag) => [tag.name, tag]),
);
return this._setQuery(Lib.withTemplateTags(query, newTagsMap));