Skip to content

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

Fix crash when editing column settings in the embedding SDK (#76795)

Sébastien06ed2904718e
diff --git a/e2e/test-component/scenarios/embedding-sdk/sdk-question-native.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/sdk-question-native.cy.spec.tsx
index 4f317369902..d693d3fa4d2 100644
--- a/e2e/test-component/scenarios/embedding-sdk/sdk-question-native.cy.spec.tsx
+++ b/e2e/test-component/scenarios/embedding-sdk/sdk-question-native.cy.spec.tsx
@@ -141,6 +141,41 @@ describe("scenarios > embedding-sdk > interactive-question > native", () => {
});
});
+ describe("editing column settings", () => {
+ beforeEach(() => {
+ setup({
+ question: {
+ name: "Orders native question",
+ native: { query: "SELECT * FROM ORDERS" },
+ },
+ });
+ });
+
+ it("edits a column title without crashing (metabase#76455)", () => {
+ mountInteractiveQuestion({});
+
+ cy.wait("@cardQuery");
+
+ getSdkRoot().within(() => {
+ cy.findByText("TAX").should("be.visible");
+
+ H.openVizSettingsSidebar();
+ cy.findByTestId("chartsettings-sidebar")
+ .findByTestId("TAX-settings-button")
+ .click();
+ });
+
+ getSdkRoot().findByLabelText("Column title").clear().type("Renamed tax");
+
+ getSdkRoot().within(() => {
+ // Editing the column title used to throw a full-screen "Unexpected
+ // Application Error!" and leave the title unchanged (metabase#76455).
+ cy.findByText("Unexpected Application Error!").should("not.exist");
+ cy.findAllByText("Renamed tax").should("have.length.at.least", 1);
+ });
+ });
+ });
+
describe("editable parameters for a native question", () => {
beforeEach(() => {
setup({
diff --git a/frontend/src/metabase/embedding-sdk/lib/links.ts b/frontend/src/metabase/embedding-sdk/lib/links.ts
index d99b267a955..cef7aba6219 100644
--- a/frontend/src/metabase/embedding-sdk/lib/links.ts
+++ b/frontend/src/metabase/embedding-sdk/lib/links.ts
@@ -36,7 +36,7 @@ function mapColumnSettings(
const entries = Object.entries(columnSettings);
const hasInternal = entries.some(([, settings]) =>
- isInternalLinkClickBehavior(settings.click_behavior),
+ isInternalLinkClickBehavior(settings?.click_behavior),
);
if (!hasInternal) {
@@ -45,7 +45,7 @@ function mapColumnSettings(
return Object.fromEntries(
entries.map(([key, settings]) => {
- if (!isInternalLinkClickBehavior(settings.click_behavior)) {
+ if (!isInternalLinkClickBehavior(settings?.click_behavior)) {
return [key, settings];
}
diff --git a/frontend/src/metabase/embedding-sdk/lib/links.unit.spec.ts b/frontend/src/metabase/embedding-sdk/lib/links.unit.spec.ts
index 13d2b41cad4..9d7205e20ca 100644
--- a/frontend/src/metabase/embedding-sdk/lib/links.unit.spec.ts
+++ b/frontend/src/metabase/embedding-sdk/lib/links.unit.spec.ts
@@ -1,3 +1,5 @@
+import type { VisualizationSettings } from "metabase-types/api";
+
import { removeInternalClickBehaviors } from "./links";
describe("removeInternalClickBehaviors", () => {
@@ -54,4 +56,26 @@ describe("removeInternalClickBehaviors", () => {
}
`);
});
+
+ it("does not crash on undefined column settings entries (EMB-1940)", () => {
+ const settings = {
+ column_settings: {
+ // columns without stored settings can end up as `undefined` entries
+ '["name","TOTAL"]': undefined,
+ '["name","SUBTOTAL"]': {
+ click_behavior: {
+ type: "link",
+ linkType: "dashboard",
+ targetId: 1,
+ },
+ },
+ },
+ } as unknown as VisualizationSettings;
+
+ const result = removeInternalClickBehaviors(settings);
+
+ expect(
+ result.column_settings?.['["name","SUBTOTAL"]'].click_behavior,
+ ).toBeUndefined();
+ });
});
diff --git a/frontend/src/metabase/visualizations/components/settings/ChartSettingNestedSettings.jsx b/frontend/src/metabase/visualizations/components/settings/ChartSettingNestedSettings.jsx
index 457ccb43f81..169e806c309 100644
--- a/frontend/src/metabase/visualizations/components/settings/ChartSettingNestedSettings.jsx
+++ b/frontend/src/metabase/visualizations/components/settings/ChartSettingNestedSettings.jsx
@@ -66,7 +66,9 @@ export const chartSettingNestedSettings =
objectSettings,
changedSettings,
);
- } else {
+ } else if (objectSettings != null) {
+ // Objects without stored settings must not be added as `undefined`
+ // entries, which would corrupt the settings map (EMB-1940).
newSettings[currentKey] = objectSettings;
}
return newSettings;
diff --git a/frontend/src/metabase/visualizations/components/settings/ChartSettingNestedSettings.unit.spec.tsx b/frontend/src/metabase/visualizations/components/settings/ChartSettingNestedSettings.unit.spec.tsx
new file mode 100644
index 00000000000..bd8fa7292c9
--- /dev/null
+++ b/frontend/src/metabase/visualizations/components/settings/ChartSettingNestedSettings.unit.spec.tsx
@@ -0,0 +1,61 @@
+import userEvent from "@testing-library/user-event";
+
+import { render, screen } from "__support__/ui";
+
+import { chartSettingNestedSettings } from "./ChartSettingNestedSettings";
+
+type TestObject = { name: string };
+
+const getObjectKey = (object: TestObject) =>
+ JSON.stringify(["name", object.name]);
+
+const getObjectSettings = (
+ settings: Record<string, unknown> | undefined,
+ object: TestObject,
+) => settings?.[getObjectKey(object)];
+
+const ComposedComponent = ({ object, onChangeObjectSettings }: any) => (
+ <button
+ onClick={() =>
+ onChangeObjectSettings(object, { column_title: "New title" })
+ }
+ >
+ change
+ </button>
+);
+
+const WrappedComponent = chartSettingNestedSettings({
+ getObjectKey,
+ getObjectSettings,
+ getSettingsWidgetsForObject: () => [],
+})(ComposedComponent);
+
+describe("chartSettingNestedSettings", () => {
+ it("does not write undefined entries for objects without stored settings (EMB-1940)", async () => {
+ const onChange = jest.fn();
+ const objects: TestObject[] = [{ name: "A" }, { name: "B" }];
+
+ render(
+ <WrappedComponent
+ series={[]}
+ objects={objects}
+ // only object B has stored settings
+ value={{ '["name","B"]': { column_title: "Old title" } }}
+ initialKey={getObjectKey(objects[1])}
+ onChange={onChange}
+ />,
+ );
+
+ await userEvent.click(screen.getByRole("button", { name: "change" }));
+
+ expect(onChange).toHaveBeenCalledTimes(1);
+ const newSettings = onChange.mock.calls[0][0];
+
+ // object A (no stored settings) must not appear as an undefined entry
+ expect(newSettings).not.toHaveProperty('["name","A"]');
+ expect(Object.values(newSettings)).not.toContain(undefined);
+ expect(newSettings).toEqual({
+ '["name","B"]': { column_title: "New title" },
+ });
+ });
+});