From 10e872c6ab829ed4a69857cc45ea38910dd44639 Mon Sep 17 00:00:00 2001 From: Art051 <91856576+Art051@users.noreply.github.com> Date: Fri, 27 Jun 2025 15:05:37 +0100 Subject: [PATCH] Merge pull request #4752 from Art051/bugfix/4749-generate-code-error-with-binary-file-request Bugfix/4749 generate code error with binary file request --- .../bruno-app/src/utils/codegenerator/auth.js | 9 +++++++ .../bruno-app/src/utils/codegenerator/har.js | 24 +++++++++++++------ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/packages/bruno-app/src/utils/codegenerator/auth.js b/packages/bruno-app/src/utils/codegenerator/auth.js index 981d6cec2..d5ecb4cc5 100644 --- a/packages/bruno-app/src/utils/codegenerator/auth.js +++ b/packages/bruno-app/src/utils/codegenerator/auth.js @@ -1,6 +1,15 @@ import get from 'lodash/get'; export const getAuthHeaders = (collectionRootAuth, requestAuth) => { + + // Discovered edge case where code generation fails when you create a collection which has not been saved yet: + // Collection auth therefore null, and request inherits from collection, therefore it is also null + // TypeError: Cannot read properties of undefined (reading 'mode') + // at getAuthHeaders + if (!collectionRootAuth && !requestAuth) { + return []; + } + const auth = collectionRootAuth && ['inherit'].includes(requestAuth?.mode) ? collectionRootAuth : requestAuth; switch (auth.mode) { diff --git a/packages/bruno-app/src/utils/codegenerator/har.js b/packages/bruno-app/src/utils/codegenerator/har.js index 110f82db5..0898ada87 100644 --- a/packages/bruno-app/src/utils/codegenerator/har.js +++ b/packages/bruno-app/src/utils/codegenerator/har.js @@ -93,15 +93,25 @@ const createPostData = (body, type) => { ...(param.type === 'file' && { fileName: param.value }) })) }; - case 'file': + case 'file': { + const files = Array.isArray(body[body.mode]) ? body[body.mode] : []; + const selectedFile = files.find((param) => param.selected) || files[0]; + const filePath = selectedFile?.filePath || ''; return { - mimeType: body[body.mode].filter((param) => param.enabled)[0].contentType, - params: body[body.mode] - .filter((param) => param.selected) - .map((param) => ({ - value: param.filePath, - })) + mimeType: selectedFile?.contentType || 'application/octet-stream', + text: filePath, + params: filePath + ? [ + { + name: selectedFile?.name || 'file', + value: filePath, + fileName: filePath, + contentType: selectedFile?.contentType || 'application/octet-stream' + } + ] + : [] }; + } default: return { mimeType: contentType,