Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions packages/bruno-app/src/utils/codemirror/brunoVarInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,15 @@ const updateValueDisplay = (valueDisplay, value, isSecret, isMasked, isRevealed)
}

if (typeof value === 'object') {
valueDisplay.textContent = value === null ? 'null' : JSON.stringify(value, null, 2);
if (value === null) {
valueDisplay.textContent = 'null';
} else {
try {
valueDisplay.textContent = JSON.stringify(value, null, 2);
} catch {
valueDisplay.textContent = String(value);
}
}
return;
}

Expand Down Expand Up @@ -153,8 +161,19 @@ const getCopyButton = (getVariableValue, onCopyCallback) => {
// Resolve the latest value at click time so edits/saves are reflected.
const valueToCopy = typeof getVariableValue === 'function' ? getVariableValue() : getVariableValue;

let valueStr;
if (typeof valueToCopy === 'object' && valueToCopy !== null) {
try {
valueStr = JSON.stringify(valueToCopy, null, 2);
} catch {
valueStr = String(valueToCopy);
}
} else {
valueStr = String(valueToCopy ?? '');
}

navigator.clipboard
.writeText(valueToCopy ?? '')
.writeText(valueStr)
.then(() => {
isCopied = true;
copyButton.innerHTML = CHECKMARK_ICON_SVG_TEXT;
Expand Down
22 changes: 22 additions & 0 deletions packages/bruno-app/src/utils/codemirror/brunoVarInfo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,28 @@ describe('renderVarInfo', () => {
expect(copyButton.classList.contains('copy-success')).toBe(false);
});

it('should copy plain object values as formatted JSON', async () => {
const { copyButton } = setupRender({ apiKey: { host: 'localhost', port: 8080 } });

copyButton.click();
await jest.runAllTimersAsync();

expect(navigator.clipboard.writeText).toHaveBeenCalledWith(
JSON.stringify({ host: 'localhost', port: 8080 }, null, 2)
);
});

it('should fall back to String() for circular objects and still write to clipboard', async () => {
const circular = {};
circular.self = circular;
const { copyButton } = setupRender({ apiKey: circular });

copyButton.click();
await jest.runAllTimersAsync();

expect(navigator.clipboard.writeText).toHaveBeenCalledWith(String(circular));
});

it('should log to the console when the variable value is not copied', async () => {
const { copyButton } = setupRender({ apiKey: 'cause-clipboard-error' });

Expand Down
Loading