diff --git a/public/app/features/dashboard-scene/serialization/sceneVariablesSetToVariables.ts b/public/app/features/dashboard-scene/serialization/sceneVariablesSetToVariables.ts index 4fad1e3b317..fe7b56e22b5 100644 --- a/public/app/features/dashboard-scene/serialization/sceneVariablesSetToVariables.ts +++ b/public/app/features/dashboard-scene/serialization/sceneVariablesSetToVariables.ts @@ -554,7 +554,7 @@ export function sceneVariablesSetToSchemaV2Variables( ...validateFiltersOrigin(variable.state.originFilters), ...validateFiltersOrigin(variable.state.filters), ], - defaultKeys: variable.state.defaultKeys || [], //FIXME what is the default value? + defaultKeys: variable.state.defaultKeys || [], allowCustomValue: variable.state.allowCustomValue ?? true, }, }; diff --git a/public/app/features/dashboard-scene/serialization/testfiles/nested_dashboard.json b/public/app/features/dashboard-scene/serialization/testfiles/nested_dashboard.json index 4322333ceb1..8a39daf6155 100644 --- a/public/app/features/dashboard-scene/serialization/testfiles/nested_dashboard.json +++ b/public/app/features/dashboard-scene/serialization/testfiles/nested_dashboard.json @@ -1137,7 +1137,13 @@ "datasource": { "name": "esmce00tbim8" }, "group": "opensearch", "spec": { - "allowCustomValue": true + "name": "adhocVar", + "allowCustomValue": true, + "filters": [], + "baseFilters": [], + "defaultKeys": [], + "hide": "dontHide", + "skipUrlSync": false } }, { diff --git a/public/app/features/dashboard-scene/serialization/transformSaveModelSchemaV2ToScene.test.ts b/public/app/features/dashboard-scene/serialization/transformSaveModelSchemaV2ToScene.test.ts index f054a9f7cab..aae2599b906 100644 --- a/public/app/features/dashboard-scene/serialization/transformSaveModelSchemaV2ToScene.test.ts +++ b/public/app/features/dashboard-scene/serialization/transformSaveModelSchemaV2ToScene.test.ts @@ -365,6 +365,35 @@ describe('transformSaveModelSchemaV2ToScene', () => { expect(getQueryRunnerFor(vizPanels[0])?.state.datasource?.uid).toBe(MIXED_DATASOURCE_NAME); }); + describe('adhoc variables', () => { + it('should convert empty defaultKeys array to undefined', () => { + const dashboard = cloneDeep(defaultDashboard); + const adhocVar = dashboard.spec.variables.find((v) => v.kind === 'AdhocVariable') as AdhocVariableKind; + adhocVar.spec.defaultKeys = []; + + const scene = transformSaveModelSchemaV2ToScene(dashboard); + + const adhocVariable = scene.state.$variables?.getByName('adhocVar') as AdHocFiltersVariable; + expect(adhocVariable).toBeInstanceOf(AdHocFiltersVariable); + + expect(adhocVariable.state.defaultKeys).toBeUndefined(); + }); + + it('should preserve non-empty defaultKeys array', () => { + const dashboard = cloneDeep(defaultDashboard); + + const adhocVar = dashboard.spec.variables.find((v) => v.kind === 'AdhocVariable') as AdhocVariableKind; + expect(adhocVar.spec.defaultKeys.length).toBeGreaterThan(0); + + const scene = transformSaveModelSchemaV2ToScene(dashboard); + + const adhocVariable = scene.state.$variables?.getByName('adhocVar') as AdHocFiltersVariable; + expect(adhocVariable).toBeInstanceOf(AdHocFiltersVariable); + + expect(adhocVariable.state.defaultKeys).toEqual(adhocVar.spec.defaultKeys); + }); + }); + describe('When creating a snapshot dashboard scene', () => { it('should initialize a dashboard scene with SnapshotVariables', () => { const snapshot: DashboardWithAccessInfo = { @@ -401,6 +430,29 @@ describe('transformSaveModelSchemaV2ToScene', () => { expect(intervalSnapshot.state.text).toBe('1m'); expect(intervalSnapshot.state.isReadOnly).toBe(true); }); + + it('should convert empty defaultKeys array to undefined for adhoc variables', () => { + const snapshot: DashboardWithAccessInfo = cloneDeep({ + ...defaultDashboard, + metadata: { + ...defaultDashboard.metadata, + annotations: { + ...defaultDashboard.metadata.annotations, + [AnnoKeyDashboardIsSnapshot]: 'true', + }, + }, + }); + + const adhocVar = snapshot.spec.variables.find((v) => v.kind === 'AdhocVariable') as AdhocVariableKind; + adhocVar.spec.defaultKeys = []; + + const scene = transformSaveModelSchemaV2ToScene(snapshot); + + const adhocVariable = scene.state.$variables?.getByName('adhocVar') as AdHocFiltersVariable; + expect(adhocVariable).toBeInstanceOf(AdHocFiltersVariable); + + expect(adhocVariable.state.defaultKeys).toBeUndefined(); + }); }); describe('meta', () => { diff --git a/public/app/features/dashboard-scene/serialization/transformSaveModelSchemaV2ToScene.ts b/public/app/features/dashboard-scene/serialization/transformSaveModelSchemaV2ToScene.ts index 09cae3ef7da..ff3927d3814 100644 --- a/public/app/features/dashboard-scene/serialization/transformSaveModelSchemaV2ToScene.ts +++ b/public/app/features/dashboard-scene/serialization/transformSaveModelSchemaV2ToScene.ts @@ -329,7 +329,7 @@ function createSceneVariableFromVariableModel(variable: TypedVariableModelV2): S applyMode: 'auto', filters: variable.spec.filters ?? [], baseFilters: variable.spec.baseFilters ?? [], - defaultKeys: variable.spec.defaultKeys, + defaultKeys: variable.spec.defaultKeys.length ? variable.spec.defaultKeys : undefined, useQueriesAsFilterForOptions: true, drilldownRecommendationsEnabled: config.featureToggles.drilldownRecommendations, layout: config.featureToggles.newFiltersUI ? 'combobox' : undefined, @@ -538,7 +538,7 @@ export function createVariablesForSnapshot(dashboard: DashboardV2Spec): SceneVar applyMode: 'auto', filters: v.spec.filters ?? [], baseFilters: v.spec.baseFilters ?? [], - defaultKeys: v.spec.defaultKeys, + defaultKeys: v.spec.defaultKeys?.length ? v.spec.defaultKeys : undefined, useQueriesAsFilterForOptions: true, layout: config.featureToggles.newFiltersUI ? 'combobox' : undefined, supportsMultiValueOperators: Boolean(