Fix: Inconsistent JSON formatting in preview when encoded value is a string

This commit is contained in:
ramki-bruno
2025-02-27 11:59:50 +05:30
parent 9f27ea4271
commit 4406cc573a

View File

@@ -11,26 +11,23 @@ import { useState, useMemo, useEffect } from 'react';
import { useTheme } from 'providers/Theme/index';
import { uuid } from 'utils/common/index';
const formatResponse = (data, mode, filter) => {
if (data === undefined) {
const formatResponse = (data, dataBuffer, mode, filter) => {
if (data === undefined || !dataBuffer) {
return '';
}
if (data === null) {
return data;
}
// TODO: We need a better way to get the raw response-data here instead
// of using this dataBuffer param.
// Also, we only need the raw response-data and content-type to show the preview.
const rawData = Buffer.from(dataBuffer, "base64").toString();
if (mode.includes('json')) {
let isValidJSON = false;
try {
isValidJSON = typeof JSON.parse(JSON.stringify(data)) === 'object';
JSON.parse(rawData);
} catch (error) {
console.log('Error parsing JSON: ', error.message);
}
if (!isValidJSON && typeof data === 'string') {
return data;
// If the response content-type is JSON and it fails parsing, its an invalid JSON.
// In that case, just show the response as it is in the preview.
return rawData;
}
if (filter) {
@@ -76,7 +73,7 @@ const QueryResult = ({ item, collection, data, dataBuffer, width, disableRunEven
const contentType = getContentType(headers);
const mode = getCodeMirrorModeBasedOnContentType(contentType, data);
const [filter, setFilter] = useState(null);
const formattedData = formatResponse(data, mode, filter);
const formattedData = formatResponse(data, dataBuffer, mode, filter);
const { displayedTheme } = useTheme();
const debouncedResultFilterOnChange = debounce((e) => {