diff --git a/src/platform/plugins/shared/dashboard/server/api/scope_tooling.test.ts b/src/platform/plugins/shared/dashboard/server/api/scope_tooling.test.ts index 7659bce086ea2..6e359278522c5 100644 --- a/src/platform/plugins/shared/dashboard/server/api/scope_tooling.test.ts +++ b/src/platform/plugins/shared/dashboard/server/api/scope_tooling.test.ts @@ -9,7 +9,6 @@ import { schema } from '@kbn/config-schema'; import { stripUnmappedKeys, throwOnUnmappedKeys } from './scope_tooling'; -import type { DashboardState } from './types'; const mockGetTransforms = jest.fn(); @@ -248,24 +247,6 @@ describe('stripUnmappedKeys', () => { } `); }); - - it('should drop pinned_panels', () => { - const dashboardState = { - pinned_panels: {} as unknown as DashboardState['pinned_panels'], - title: 'my dashboard', - }; - expect(stripUnmappedKeys(dashboardState)).toMatchInlineSnapshot(` - Object { - "data": Object { - "panels": Array [], - "title": "my dashboard", - }, - "warnings": Array [ - "Dropped unmapped key 'pinned_panels' from dashboard", - ], - } - `); - }); }); describe('throwOnUnmappedKeys', () => { @@ -343,12 +324,4 @@ describe('throwOnUnmappedKeys', () => { }; expect(() => throwOnUnmappedKeys(dashboardState)).toThrow(); }); - - it('should throw when dashboard contains pinned_panels', () => { - const dashboardState = { - pinned_panels: {} as unknown as DashboardState['pinned_panels'], - title: 'my dashboard', - }; - expect(() => throwOnUnmappedKeys(dashboardState)).toThrow(); - }); }); diff --git a/src/platform/plugins/shared/dashboard/server/api/scope_tooling.ts b/src/platform/plugins/shared/dashboard/server/api/scope_tooling.ts index fc0e72dd23844..80612d537983c 100644 --- a/src/platform/plugins/shared/dashboard/server/api/scope_tooling.ts +++ b/src/platform/plugins/shared/dashboard/server/api/scope_tooling.ts @@ -9,16 +9,24 @@ import { isDashboardSection } from '../../common'; import { embeddableService } from '../kibana_services'; -import type { DashboardPanel, DashboardState } from './types'; +import type { + DashboardPanel, + DashboardState, + DashboardPinnedPanel, + DashboardSection, +} from './types'; + +function isPinnedPanel( + panel: DashboardPanel | DashboardPinnedPanel | DashboardSection +): panel is DashboardPinnedPanel { + return !('grid' in panel); +} export function stripUnmappedKeys(dashboardState: DashboardState) { const warnings: string[] = []; const { pinned_panels, panels, ...rest } = dashboardState; - if (pinned_panels) { - warnings.push(`Dropped unmapped key 'pinned_panels' from dashboard`); - } - function isMappedPanelType(panel: DashboardPanel) { + function isMappedPanelType(panel: DashboardPanel | DashboardPinnedPanel) { const transforms = embeddableService?.getTransforms(panel.type); if (transforms?.throwOnUnmappedPanel) { try { @@ -58,9 +66,10 @@ export function stripUnmappedKeys(dashboardState: DashboardState) { }; } - const mappedPanels = (panels ?? []) + const mappedPanels = [...(panels ?? []), ...(pinned_panels ?? [])] .filter((panel) => isDashboardSection(panel) || isMappedPanelType(panel)) .map((panel) => { + if (isPinnedPanel(panel)) return panel; if (!isDashboardSection(panel)) return removeEnhancements(panel); const { panels: sectionPanels, ...restOfSection } = panel; return { @@ -79,11 +88,7 @@ export function stripUnmappedKeys(dashboardState: DashboardState) { } export function throwOnUnmappedKeys(dashboardState: DashboardState) { - if (dashboardState.pinned_panels) { - throw new Error('pinned_panels key is not supported by dashboard REST endpoints.'); - } - - function throwOnUnmappedPanelKeys(panel: DashboardPanel) { + function throwOnUnmappedPanelKeys(panel: DashboardPanel | DashboardPinnedPanel) { const transforms = embeddableService?.getTransforms(panel.type); const panelSchema = transforms?.schema; @@ -100,7 +105,7 @@ export function throwOnUnmappedKeys(dashboardState: DashboardState) { } } - dashboardState.panels?.forEach((panel) => { + [...(dashboardState.panels ?? []), ...(dashboardState.pinned_panels ?? [])].forEach((panel) => { if (isDashboardSection(panel)) { panel.panels.forEach(throwOnUnmappedPanelKeys); } else {