You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by en...@apache.org on 2026/08/27 13:13:10 UTC
(superset) 19/46: fix(dashboard-v2): constrain colorDimension to already-grouped dimensions
This is an automated email from the ASF dual-hosted git repository.
EnxDev pushed a commit to branch enxdev/poc/dashboard-v2-editing-ui-flow
in repository https://gitbox.apache.org/repos/asf/superset.git
commit b61674c7c9e3c8494354b8ec47e169bb9cdd3598
Author: Enzo Martellucci <en...@gmail.com>
AuthorDate: Wed Aug 26 12:44:47 2026 +0200
fix(dashboard-v2): constrain colorDimension to already-grouped dimensions
The picker offered every column of the bound dataset, but the backend's
_color_dimension_must_be_grouped validator only accepts a value already
present in dataBinding.dimensions — for a typical dataset that meant
most options were guaranteed to fail later, at query time, far from the
field that caused it.
Add a colorDimension-specific control (matched by field name, like the
existing datasetId control) that intersects the picker's options with
the sibling dataBinding.dimensions, and disables it with an explanatory
placeholder when nothing is grouped yet.
Impeccable critique action 3 of 4.
---
.../DashboardBuilderV2/SchemaControlPanel.test.tsx | 90 +++++++++++++++++++++-
.../DashboardBuilderV2/schemaControlRenderers.tsx | 67 ++++++++++++++++
2 files changed, 154 insertions(+), 3 deletions(-)
diff --git a/superset-frontend/src/pages/DashboardBuilderV2/SchemaControlPanel.test.tsx b/superset-frontend/src/pages/DashboardBuilderV2/SchemaControlPanel.test.tsx
index 06c271fca93..972ec86a91c 100644
--- a/superset-frontend/src/pages/DashboardBuilderV2/SchemaControlPanel.test.tsx
+++ b/superset-frontend/src/pages/DashboardBuilderV2/SchemaControlPanel.test.tsx
@@ -185,14 +185,17 @@ test('discovers series from an explicit colorDimension when set', async () => {
});
test('renders a column picker for an x-control: "column" field and writes the pick back into props', async () => {
+ // Named anything but `colorDimension`, which has its own, more specific
+ // control (`ColorDimensionControl`) — this test is about the generic
+ // `x-control: "column"` mechanism any other field can use.
postSpy.mockResolvedValue({
json: {
result: {
type: 'object',
properties: {
- colorDimension: {
+ favoriteColumn: {
type: 'string',
- title: 'Color dimension',
+ title: 'Favorite column',
'x-control': 'column',
},
},
@@ -212,9 +215,90 @@ test('renders a column picker for an x-control: "column" field and writes the pi
dataBinding: { datasetId: 1, metrics: ['count'] },
});
- await screen.findByText('Color dimension');
+ await screen.findByText('Favorite column');
await selectOption('gender');
+ await waitFor(() =>
+ expect(provider.getNode(id)?.props?.favoriteColumn).toBe('gender'),
+ );
+});
+
+// `colorDimension` can only ever be a dimension the widget already groups
+// by (the backend's `_color_dimension_must_be_grouped` validator rejects
+// anything else), so its picker intersects with the sibling
+// `dataBinding.dimensions` rather than offering every column.
+const COLOR_DIMENSION_SCHEMA = {
+ type: 'object',
+ properties: {
+ colorDimension: {
+ type: 'string',
+ title: 'Color dimension',
+ 'x-control': 'column',
+ },
+ },
+};
+
+test('colorDimension is disabled with an explanatory placeholder when nothing is grouped yet', async () => {
+ postSpy.mockResolvedValue({
+ json: { result: COLOR_DIMENSION_SCHEMA },
+ } as never);
+ getSpy.mockResolvedValue({
+ json: {
+ result: {
+ columns: [{ column_name: 'gender', type_generic: 1 }],
+ metrics: [],
+ },
+ },
+ } as never);
+
+ mount('balloons', {
+ dataBinding: { datasetId: 1, metrics: ['count'], dimensions: [] },
+ });
+
+ const picker = await screen.findByRole('combobox', {
+ name: 'Color dimension',
+ });
+ expect(picker).toBeDisabled();
+ expect(screen.getByText('Group a dimension first')).toBeInTheDocument();
+});
+
+test('colorDimension excludes dataset columns that are not grouped, and offers the ones that are', async () => {
+ postSpy.mockResolvedValue({
+ json: { result: COLOR_DIMENSION_SCHEMA },
+ } as never);
+ getSpy.mockResolvedValue({
+ json: {
+ result: {
+ // Three columns on the dataset; only `gender` is actually grouped.
+ columns: [
+ { column_name: 'name', type_generic: 1 },
+ { column_name: 'gender', type_generic: 1 },
+ { column_name: 'country', type_generic: 1 },
+ ],
+ metrics: [],
+ },
+ },
+ } as never);
+
+ const id = mount('balloons', {
+ dataBinding: {
+ datasetId: 1,
+ metrics: ['count'],
+ dimensions: ['gender'],
+ },
+ });
+
+ const picker = await screen.findByRole('combobox', {
+ name: 'Color dimension',
+ });
+ expect(picker).toBeEnabled();
+
+ await userEvent.click(picker);
+ expect(screen.getByText('gender')).toBeInTheDocument();
+ expect(screen.queryByText('name')).not.toBeInTheDocument();
+ expect(screen.queryByText('country')).not.toBeInTheDocument();
+
+ await userEvent.click(screen.getByText('gender'));
await waitFor(() =>
expect(provider.getNode(id)?.props?.colorDimension).toBe('gender'),
);
diff --git a/superset-frontend/src/pages/DashboardBuilderV2/schemaControlRenderers.tsx b/superset-frontend/src/pages/DashboardBuilderV2/schemaControlRenderers.tsx
index bde64aba558..ec696c77b2a 100644
--- a/superset-frontend/src/pages/DashboardBuilderV2/schemaControlRenderers.tsx
+++ b/superset-frontend/src/pages/DashboardBuilderV2/schemaControlRenderers.tsx
@@ -33,6 +33,9 @@
* collapsed by default, showing only the entries actually customized,
* plus a picker to add one more — rather than the upstream JsonForms
* renderer's one always-expanded group per possible entry.
+ * - `colorDimension` (matched by name, like `datasetId`) → a column
+ * picker constrained to the sibling `dataBinding.dimensions`, since the
+ * backend only accepts a colorDimension that's already grouped.
*
* `code` and `color` fall back to the field's schema `default`, since
* JsonForms does not write defaults into the data until a field is touched.
@@ -371,6 +374,16 @@ function useBoundDatasetId(props: ControlProps): number | undefined {
return dataBinding?.datasetId;
}
+/** The sibling `dataBinding.dimensions` list, read the same way. */
+function useBoundDimensions(props: ControlProps): string[] {
+ const formData = (
+ props.config as { formData?: Record<string, unknown> } | undefined
+ )?.formData;
+ const dataBinding = formData?.dataBinding as
+ { dimensions?: string[] } | undefined;
+ return dataBinding?.dimensions ?? [];
+}
+
/**
* True when a column/metric-reference control should fail open to the raw
* JSON editor (`CodeControl`) rather than render its picker: no dataset is
@@ -431,6 +444,7 @@ function ReferenceSelect({
options,
loading,
disabled,
+ placeholder,
onChange,
}: {
label: string;
@@ -439,6 +453,7 @@ function ReferenceSelect({
options: ReferenceOption[];
loading: boolean;
disabled: boolean;
+ placeholder?: string;
onChange: (next: string | undefined) => void;
}): ReactElement {
return (
@@ -450,6 +465,7 @@ function ReferenceSelect({
options={options}
loading={loading}
disabled={disabled}
+ placeholder={placeholder}
allowClear
css={{ width: '100%' }}
/>
@@ -577,6 +593,48 @@ function ColumnControl(props: ControlProps): ReactElement {
);
}
+/**
+ * The `colorDimension` field specifically: a column reference, but not to
+ * any column — the widget only colors by a dimension it already groups by
+ * (Balloons' `_color_dimension_must_be_grouped` validator rejects anything
+ * else). Offering all of a dataset's columns, most of which the backend
+ * will reject, taught nothing about which one was actually valid; this
+ * intersects the picker's options with the sibling `dataBinding.dimensions`
+ * instead, and disables it with an explanatory placeholder when there's
+ * nothing grouped yet to color by.
+ */
+function ColorDimensionControl(props: ControlProps): ReactElement {
+ const datasetId = useBoundDatasetId(props);
+ const dimensions = useBoundDimensions(props);
+ const { metadata, loading, error } = useDatasetMetadata(datasetId);
+
+ if (
+ shouldFallBackToCode(datasetId, error) ||
+ (props.data !== undefined && typeof props.data !== 'string')
+ ) {
+ return <CodeControl {...props} />;
+ }
+
+ const options = columnOptions(metadata, undefined).filter(option =>
+ dimensions.includes(option.value),
+ );
+
+ return (
+ <ReferenceSelect
+ label={props.label}
+ description={props.description}
+ value={props.data as string | undefined}
+ options={options}
+ loading={loading}
+ disabled={!props.enabled || dimensions.length === 0}
+ placeholder={
+ dimensions.length === 0 ? t('Group a dimension first') : undefined
+ }
+ onChange={value => props.handleChange(props.path, value)}
+ />
+ );
+}
+
/**
* `x-control: "column-multi"` — an ordered list of column references. Falls
* back to the raw JSON editor when no dataset is bound (or its fetch
@@ -845,6 +903,15 @@ export const schemaControlRenderers = [
tester: rankWith(1000, isDynamicSeries),
renderer: withJsonFormsControlProps(SeriesOverridesControl),
},
+ {
+ // Ranked above the generic `column` tester below so this field's own,
+ // narrower control wins the match — both would otherwise tie at 1000.
+ tester: rankWith(
+ 1001,
+ and(scopeEndIs('colorDimension'), schemaTypeIs('string')),
+ ),
+ renderer: withJsonFormsControlProps(ColorDimensionControl),
+ },
{
tester: rankWith(1000, xControlIs('column')),
renderer: withJsonFormsControlProps(ColumnControl),