DataSourceSrv: isDataSourceRef should match type-only ds ref (#109159)

* isDataSourceRef should match type-only ds ref

* update codeowners
This commit is contained in:
Josh Hunt
2025-08-05 10:24:30 +01:00
committed by GitHub
parent cb921dc47a
commit e4be1042d0
4 changed files with 49 additions and 3 deletions
+1 -1
View File
@@ -505,7 +505,7 @@
/packages/grafana-data/src/utils/binaryOperators.ts @grafana/datapro
/packages/grafana-data/src/utils/csv* @grafana/dataviz-squad
/packages/grafana-data/src/utils/dataLinks* @grafana/dashboards-squad
/packages/grafana-data/src/utils/datasource.ts @grafana/grafana-datasources-core-services
/packages/grafana-data/src/utils/datasource* @grafana/grafana-datasources-core-services
/packages/grafana-data/src/utils/docs.ts @grafana/grafana-frontend-platform
/packages/grafana-data/src/utils/deprecationWarning* @grafana/grafana-frontend-platform
/packages/grafana-data/src/utils/featureToggles.ts @grafana/grafana-frontend-platform
@@ -0,0 +1,22 @@
import { isDataSourceRef } from './datasource';
describe('isDataSourceRef', () => {
it('returns true for DataSourceRef with only a uid', () => {
const ref = { uid: '123', type: 'prometheus' };
expect(isDataSourceRef(ref)).toBe(true);
});
it('returns true for DataSourceRef with only a type', () => {
const ref = { type: 'prometheus' };
expect(isDataSourceRef(ref)).toBe(true);
});
it('returns true for DataSourceRef with both a uid and a type', () => {
const ref = { uid: '123', type: 'prometheus' };
expect(isDataSourceRef(ref)).toBe(true);
});
it.each(['prometheus', null, undefined, {}, 123])('returns false for %s', (input) => {
expect(isDataSourceRef(input)).toBe(false);
});
});
@@ -29,8 +29,14 @@ export function getDataSourceRef(ds: DataSourceInstanceSettings): DataSourceRef
*
* @public
*/
export function isDataSourceRef(ref: DataSourceRef | string | null | undefined): ref is DataSourceRef {
return typeof ref === 'object' && typeof ref?.uid === 'string';
export function isDataSourceRef(ref: unknown): ref is DataSourceRef {
if (typeof ref !== 'object' || ref === null) {
return false;
}
const hasUid = 'uid' in ref && typeof ref.uid === 'string';
const hasType = 'type' in ref && typeof ref.type === 'string';
return hasUid || hasType;
}
/**
@@ -1846,6 +1846,16 @@ describe('DashboardModel', () => {
},
],
},
// @ts-expect-error
{
id: 7,
datasource: { type: 'prometheus', uid: 'prom-uid' },
},
// @ts-expect-error
{
id: 8,
datasource: { type: 'prometheus' },
},
],
});
});
@@ -1875,6 +1885,14 @@ describe('DashboardModel', () => {
it('should update datasources in panels collapsed rows', () => {
expect(model.panels[3].panels?.[0].datasource).toEqual({ type: 'prometheus', uid: 'prom-uid' });
});
it("should not migrate datasource if it's already a ref", () => {
expect(model.panels[4].datasource).toEqual({ type: 'prometheus', uid: 'prom-uid' });
});
it("should not migrate datasource if it's already a ref with only a type", () => {
expect(model.panels[5].datasource).toEqual({ type: 'prometheus' });
});
});
describe('when fixing query and panel data source refs out of sync due to default data source change', () => {