Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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();
});
});
29 changes: 17 additions & 12 deletions src/platform/plugins/shared/dashboard/server/api/scope_tooling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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;

Expand All @@ -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 {
Expand Down
Loading