From d2d774d11c7abd0d0e2168deaef2766fca1c9731 Mon Sep 17 00:00:00 2001 From: Cristhian Zanforlin Lousa Date: Wed, 25 Jun 2025 16:04:51 -0300 Subject: [PATCH] fix: Add JSON sanitization to handle NaN values in streaming requests (#8736) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🔧 (api.tsx): add a helper function sanitizeJsonString to replace NaN values with null in JSON strings for proper parsing --- src/frontend/src/controllers/API/api.tsx | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/frontend/src/controllers/API/api.tsx b/src/frontend/src/controllers/API/api.tsx index dd696cf34..b38105938 100644 --- a/src/frontend/src/controllers/API/api.tsx +++ b/src/frontend/src/controllers/API/api.tsx @@ -272,6 +272,16 @@ export type StreamingRequestParams = { eventDeliveryConfig?: EventDeliveryType; }; +// Helper function to sanitize JSON strings +function sanitizeJsonString(jsonStr: string): string { + // Replace NaN with null (valid JSON) + return jsonStr + .replace(/:\s*NaN\b/g, ": null") + .replace(/\[\s*NaN\s*\]/g, "[null]") + .replace(/,\s*NaN\s*,/g, ", null,") + .replace(/,\s*NaN\s*\]/g, ", null]"); +} + async function performStreamingRequest({ method, url, @@ -323,7 +333,8 @@ async function performStreamingRequest({ const allString = current.join("") + string; let data: object; try { - data = JSON.parse(allString); + const sanitizedJson = sanitizeJsonString(allString); + data = JSON.parse(sanitizedJson); current = []; } catch (e) { current.push(string); @@ -342,7 +353,8 @@ async function performStreamingRequest({ if (current.length > 0) { const allString = current.join(""); if (allString) { - const data = JSON.parse(current.join("")); + const sanitizedJson = sanitizeJsonString(allString); + const data = JSON.parse(sanitizedJson); await onData(data); } }