From 5ec1b4198cffb825e49b9ed8399880f54680dbf1 Mon Sep 17 00:00:00 2001 From: Haris Rozajac <58232930+harisrozajac@users.noreply.github.com> Date: Wed, 20 Aug 2025 06:35:56 -0600 Subject: [PATCH] Dashboard Schema V2: Handle a case when fieldConfig is undefined (#109881) handle a case when fieldConfig is undefined --- .../transformSceneToSaveModelSchemaV2.ts | 80 +++++++++++-------- 1 file changed, 45 insertions(+), 35 deletions(-) diff --git a/public/app/features/dashboard-scene/serialization/transformSceneToSaveModelSchemaV2.ts b/public/app/features/dashboard-scene/serialization/transformSceneToSaveModelSchemaV2.ts index d13905c0a58..8a330cb744c 100644 --- a/public/app/features/dashboard-scene/serialization/transformSceneToSaveModelSchemaV2.ts +++ b/public/app/features/dashboard-scene/serialization/transformSceneToSaveModelSchemaV2.ts @@ -41,8 +41,8 @@ import { LibraryPanelKind, Element, DashboardCursorSync, - FieldConfig, FieldColor, + defaultFieldConfig, defaultDataQueryKind, } from '../../../../../packages/grafana-schema/src/schema/dashboard/v2'; import { DashboardDataLayerSet } from '../scene/DashboardDataLayerSet'; @@ -175,40 +175,7 @@ export function vizPanelToSchemaV2( return elementSpec; } - // Handle type conversion for color mode - const rawColor = vizPanel.state.fieldConfig.defaults.color; - let color: FieldColor | undefined; - - if (rawColor) { - const convertedMode = colorIdEnumToColorIdV2(rawColor.mode); - - if (convertedMode) { - color = { - ...rawColor, - mode: convertedMode, - }; - } - } - - // Remove null from the defaults because schema V2 doesn't support null for these fields - const decimals = vizPanel.state.fieldConfig.defaults.decimals ?? undefined; - const min = vizPanel.state.fieldConfig.defaults.min ?? undefined; - const max = vizPanel.state.fieldConfig.defaults.max ?? undefined; - - const defaults: FieldConfig = Object.fromEntries( - Object.entries({ - ...vizPanel.state.fieldConfig.defaults, - decimals, - min, - max, - color, - }).filter(([_, value]) => { - if (Array.isArray(value)) { - return value.length > 0; - } - return value !== undefined; - }) - ); + const defaults = handleFieldConfigDefaultsConversion(vizPanel); const vizFieldConfig: FieldConfigSource = { ...vizPanel.state.fieldConfig, @@ -245,6 +212,49 @@ export function vizPanelToSchemaV2( return elementSpec; } +function handleFieldConfigDefaultsConversion(vizPanel: VizPanel) { + if (!vizPanel.state.fieldConfig || !vizPanel.state.fieldConfig.defaults) { + return defaultFieldConfig(); + } + + // Handle type conversion for color mode + const rawColor = vizPanel.state.fieldConfig.defaults.color; + let color: FieldColor | undefined; + + if (rawColor) { + const convertedMode = colorIdEnumToColorIdV2(rawColor.mode); + + if (convertedMode) { + color = { + ...rawColor, + mode: convertedMode, + }; + } + } + + // Remove null from the defaults because schema V2 doesn't support null for these fields + const decimals = vizPanel.state.fieldConfig.defaults.decimals ?? undefined; + const min = vizPanel.state.fieldConfig.defaults.min ?? undefined; + const max = vizPanel.state.fieldConfig.defaults.max ?? undefined; + + const defaults = Object.fromEntries( + Object.entries({ + ...vizPanel.state.fieldConfig.defaults, + decimals, + min, + max, + color, + }).filter(([_, value]) => { + if (Array.isArray(value)) { + return value.length > 0; + } + return value !== undefined; + }) + ); + + return defaults; +} + function getPanelLinks(panel: VizPanel): DataLink[] { const vizLinks = dashboardSceneGraph.getPanelLinks(panel); if (vizLinks) {