[v11.1.x] Frontend: Use safe stringifier in parseBody (#90234)

Frontend: Use safe stringifier in parseBody (#90047)

* Frontend: Use safe stringifier in parseBody

Closes #88064

(cherry picked from commit 434f386982)
This commit is contained in:
kay delaney
2024-07-10 10:42:33 +01:00
committed by GitHub
parent 64b649e407
commit e5bbc6f17a
2 changed files with 21 additions and 2 deletions
+19
View File
@@ -10,3 +10,22 @@
export const isEmptyObject = (value: unknown): value is Record<string, never> => {
return typeof value === 'object' && value !== null && Object.keys(value).length === 0;
};
/** Stringifies an object that may contain circular references */
export function safeStringifyValue(value: unknown) {
const getCircularReplacer = () => {
const seen = new WeakSet();
return (_: string, value: object | null) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};
return JSON.stringify(value, getCircularReplacer());
}
+2 -2
View File
@@ -1,6 +1,6 @@
import { omitBy } from 'lodash';
import { deprecationWarning } from '@grafana/data';
import { deprecationWarning, safeStringifyValue } from '@grafana/data';
import { BackendSrvRequest } from '@grafana/runtime';
export const parseInitFromOptions = (options: BackendSrvRequest): RequestInit => {
@@ -93,7 +93,7 @@ export const parseBody = (options: BackendSrvRequest, isAppJson: boolean) => {
return options.data;
}
return isAppJson ? JSON.stringify(options.data) : new URLSearchParams(options.data);
return isAppJson ? safeStringifyValue(options.data) : new URLSearchParams(options.data);
};
export async function parseResponseBody<T>(