Skip to content

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

Allow custom source for ID params (#74650)

Alexander Polyankind8841313bf25
diff --git a/e2e/test/scenarios/dashboard-filters/dashboard-filters-source.cy.spec.js b/e2e/test/scenarios/dashboard-filters/dashboard-filters-source.cy.spec.js
index 445009a9202..eba094a92d7 100644
--- a/e2e/test/scenarios/dashboard-filters/dashboard-filters-source.cy.spec.js
+++ b/e2e/test/scenarios/dashboard-filters/dashboard-filters-source.cy.spec.js
@@ -199,6 +199,40 @@ describe("scenarios > dashboard > filters", { tags: "@slow" }, () => {
cy.log("the selected value is shown remapped to its label");
H.filterWidget().findByText("Rustic Paper Wallet").should("be.visible");
});
+
+ it("should remap an id value to a string label (id/string)", () => {
+ H.createNativeQuestion(numberLabelSource, { wrapId: true });
+ H.createQuestionAndDashboard({
+ questionDetails: targetQuestion,
+ }).then(({ body: { dashboard_id } }) => {
+ H.visitDashboard(dashboard_id);
+ });
+
+ H.editDashboard();
+ H.setFilter("ID");
+
+ mapFilterToQuestion("ID");
+ H.setFilterQuestionSource({
+ question: "Number label source",
+ field: "ID",
+ labelField: "TITLE",
+ });
+ H.saveDashboard();
+
+ cy.log(
+ "the dropdown shows the product titles instead of the numeric ids",
+ );
+ H.filterWidget().click();
+ H.popover().within(() => {
+ cy.findByText("Rustic Paper Wallet").should("be.visible");
+ cy.findByText("Rustic Paper Wallet").click();
+ cy.button("Add filter").click();
+ });
+
+ cy.log("the selected value and the remapped label are shown");
+ H.filterWidget().findByText("- 1").should("be.visible");
+ H.filterWidget().findByText("Rustic Paper Wallet").should("be.visible");
+ });
});
describe("native question source", () => {
diff --git a/frontend/src/metabase-lib/v1/parameters/utils/parameter-source.ts b/frontend/src/metabase-lib/v1/parameters/utils/parameter-source.ts
index 336fbbe74b5..e9da076bc23 100644
--- a/frontend/src/metabase-lib/v1/parameters/utils/parameter-source.ts
+++ b/frontend/src/metabase-lib/v1/parameters/utils/parameter-source.ts
@@ -76,6 +76,7 @@ export const canUseCustomSource = (parameter: Parameter) => {
case "location":
case "category":
case "number":
+ case "id":
return true;
default:
return false;
diff --git a/frontend/src/metabase-lib/v1/parameters/utils/parameter-source.unit.spec.ts b/frontend/src/metabase-lib/v1/parameters/utils/parameter-source.unit.spec.ts
index 8c69bedec2b..cb171796066 100644
--- a/frontend/src/metabase-lib/v1/parameters/utils/parameter-source.unit.spec.ts
+++ b/frontend/src/metabase-lib/v1/parameters/utils/parameter-source.unit.spec.ts
@@ -6,6 +6,7 @@ import { createMockField } from "metabase-types/api/mocks";
import {
canListParameterValues,
canSearchParameterValues,
+ canUseCustomSource,
getQueryType,
} from "./parameter-source";
@@ -79,6 +80,62 @@ describe("getQueryType", () => {
});
});
+describe("canUseCustomSource", () => {
+ it.each([
+ {
+ parameter: createMockUiParameter({
+ type: "string/=",
+ sectionId: "string",
+ }),
+ expected: true,
+ },
+ {
+ parameter: createMockUiParameter({
+ type: "string/=",
+ sectionId: "location",
+ }),
+ expected: true,
+ },
+ {
+ parameter: createMockUiParameter({
+ type: "category",
+ sectionId: "category",
+ }),
+ expected: true,
+ },
+ {
+ parameter: createMockUiParameter({
+ type: "number/=",
+ sectionId: "number",
+ }),
+ expected: true,
+ },
+ {
+ parameter: createMockUiParameter({ type: "id", sectionId: "id" }),
+ expected: true,
+ },
+ {
+ parameter: createMockUiParameter({
+ type: "date/single",
+ sectionId: "date",
+ }),
+ expected: false,
+ },
+ {
+ parameter: createMockUiParameter({
+ type: "temporal-unit",
+ sectionId: "temporal-unit",
+ }),
+ expected: false,
+ },
+ ])(
+ "should return $expected for $parameter.type ($parameter.sectionId)",
+ ({ parameter, expected }) => {
+ expect(canUseCustomSource(parameter)).toBe(expected);
+ },
+ );
+});
+
describe("canListParameterValues", () => {
it("should not list when it is disabled", () => {
const parameter = createMockUiParameter({
diff --git a/frontend/src/metabase/parameters/components/ValuesSourceModal/ValuesSourceModal.unit.spec.tsx b/frontend/src/metabase/parameters/components/ValuesSourceModal/ValuesSourceModal.unit.spec.tsx
index 79d4ea2c4a2..0bf142431bc 100644
--- a/frontend/src/metabase/parameters/components/ValuesSourceModal/ValuesSourceModal.unit.spec.tsx
+++ b/frontend/src/metabase/parameters/components/ValuesSourceModal/ValuesSourceModal.unit.spec.tsx
@@ -900,6 +900,92 @@ describe("ValuesSourceModal", () => {
});
});
});
+
+ describe("id parameter", () => {
+ it("should offer only numeric columns as the value field for a numeric id parameter", async () => {
+ const { onSubmit } = await setup({
+ parameter: createMockUiParameter({
+ type: "number/=",
+ sectionId: "id",
+ values_source_type: "card",
+ values_source_config: {
+ card_id: 1,
+ },
+ }),
+ cards: [
+ createMockCard({
+ id: 1,
+ name: "Products",
+ result_metadata: [
+ createMockField({
+ id: 1,
+ name: "id",
+ display_name: "ID",
+ base_type: "type/BigInteger",
+ effective_type: "type/BigInteger",
+ semantic_type: "type/PK",
+ }),
+ createMockField({
+ id: 2,
+ name: "category",
+ display_name: "Category",
+ base_type: "type/Text",
+ effective_type: "type/Text",
+ semantic_type: "type/Category",
+ }),
+ ],
+ }),
+ ],
+ });
+
+ await userEvent.click(
+ screen.getByRole("button", { name: /Pick a column/ }),
+ );
+ expect(
+ screen.queryByRole("heading", { name: "Category" }),
+ ).not.toBeInTheDocument();
+
+ await userEvent.click(screen.getByRole("heading", { name: "ID" }));
+ await userEvent.click(screen.getByRole("button", { name: "Done" }));
+ expect(onSubmit).toHaveBeenCalledWith("card", {
+ card_id: 1,
+ value_field: ["field", "id", { "base-type": "type/BigInteger" }],
+ });
+ });
+
+ it("should show a message when a numeric id parameter has no number columns", async () => {
+ await setup({
+ parameter: createMockUiParameter({
+ type: "number/=",
+ sectionId: "id",
+ values_source_type: "card",
+ values_source_config: {
+ card_id: 1,
+ },
+ }),
+ cards: [
+ createMockCard({
+ id: 1,
+ name: "Products",
+ result_metadata: [
+ createMockField({
+ id: 2,
+ name: "category",
+ display_name: "Category",
+ base_type: "type/Text",
+ effective_type: "type/Text",
+ semantic_type: "type/Category",
+ }),
+ ],
+ }),
+ ],
+ });
+
+ expect(
+ screen.getByText(/This question doesn’t have any number columns/),
+ ).toBeInTheDocument();
+ });
+ });
});
interface SetupOpts {
diff --git a/frontend/src/metabase/parameters/components/ValuesSourceModal/ValuesSourceTypeModal.tsx b/frontend/src/metabase/parameters/components/ValuesSourceModal/ValuesSourceTypeModal.tsx
index 29cdaa0a9a6..597e5cc9a06 100644
--- a/frontend/src/metabase/parameters/components/ValuesSourceModal/ValuesSourceTypeModal.tsx
+++ b/frontend/src/metabase/parameters/components/ValuesSourceModal/ValuesSourceTypeModal.tsx
@@ -454,7 +454,8 @@ const ColumnSelect = ({
};
const getErrorMessage = (question: Question, parameter: Parameter) => {
- const parameterType = getParameterType(parameter);
+ // avoids using the sectionId to determine the parameter type
+ const parameterType = getParameterType(parameter.type);
const type = question.type();
if (parameterType === "number") {
@@ -582,7 +583,8 @@ const getColumnByReference = (
};
const getSupportedColumns = (query: Lib.Query, parameter: Parameter) => {
- const type = getParameterType(parameter);
+ // avoids using the sectionId to determine the parameter type
+ const type = getParameterType(parameter.type);
return Lib.fieldableColumns(query, 0).filter((column) => {
if (type === "number") {
return Lib.isNumeric(column);