From f3cb0d4baeec032507490f1b04f4c5e8653b3922 Mon Sep 17 00:00:00 2001 From: sanish chirayath Date: Wed, 5 Nov 2025 22:56:03 +0530 Subject: [PATCH] Fix/example naming (#6002) * fix: example naming * fix: request not being saved when initialized with empty url * remove check for method * fix: improve the logic for get initial name * fix test --- .../ResponsePane/ResponseBookmark/index.js | 30 ++----------------- .../Collection/CollectionItem/index.js | 17 ++--------- .../bruno-app/src/utils/collections/index.js | 24 +++++++++++++++ .../bruno-lang/v2/src/example/jsonToBru.js | 9 ++---- .../v2/tests/examples/examples.spec.js | 8 +++-- 5 files changed, 35 insertions(+), 53 deletions(-) diff --git a/packages/bruno-app/src/components/ResponsePane/ResponseBookmark/index.js b/packages/bruno-app/src/components/ResponsePane/ResponseBookmark/index.js index cf004f4d5..b00440c3a 100644 --- a/packages/bruno-app/src/components/ResponsePane/ResponseBookmark/index.js +++ b/packages/bruno-app/src/components/ResponsePane/ResponseBookmark/index.js @@ -8,6 +8,7 @@ import { uuid } from 'utils/common'; import toast from 'react-hot-toast'; import CreateExampleModal from 'components/ResponseExample/CreateExampleModal'; import { getBodyType } from 'utils/responseBodyProcessor'; +import { getInitialExampleName } from 'utils/collections/index'; import classnames from 'classnames'; import StyledWrapper from './StyledWrapper'; @@ -23,33 +24,6 @@ const ResponseBookmark = ({ item, collection, responseSize }) => { return null; } - // Generate initial name for the example - const getInitialExampleName = () => { - const baseName = 'example'; - const existingExamples = item.draft?.examples || item.examples || []; - - // Check if any existing example has the same base name - const hasSameBaseName = existingExamples.some((example) => { - const exampleName = example.name || ''; - return exampleName === baseName || exampleName.startsWith(baseName); - }); - - if (!hasSameBaseName) { - return baseName; - } - - // Find the highest existing counter - let maxCounter = 0; - existingExamples.forEach((example) => { - const exampleName = example.name || ''; - if (exampleName.startsWith(baseName)) { - maxCounter++; - } - }); - - return `${baseName} (${maxCounter})`; - }; - const handleSaveClick = () => { if (!response || response.error) { toast.error('No valid response to save as example'); @@ -147,7 +121,7 @@ const ResponseBookmark = ({ item, collection, responseSize }) => { onClose={() => setShowSaveResponseExampleModal(false)} onSave={saveAsExample} title="Save Response as Example" - initialName={getInitialExampleName()} + initialName={getInitialExampleName(item)} /> ); diff --git a/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/index.js b/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/index.js index 8e7a90973..5d5075e23 100644 --- a/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/index.js +++ b/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/index.js @@ -33,7 +33,7 @@ import ExampleItem from './ExampleItem'; import { scrollToTheActiveTab } from 'utils/tabs'; import { isTabForItemActive as isTabForItemActiveSelector, isTabForItemPresent as isTabForItemPresentSelector } from 'src/selectors/tab'; import { isEqual } from 'lodash'; -import { calculateDraggedItemNewPathname } from 'utils/collections/index'; +import { calculateDraggedItemNewPathname, getInitialExampleName } from 'utils/collections/index'; import { sortByNameThenSequence } from 'utils/common/index'; import CreateExampleModal from 'components/ResponseExample/CreateExampleModal'; @@ -346,19 +346,6 @@ const CollectionItem = ({ item, collectionUid, collectionPathname, searchText }) setCreateExampleModalOpen(false); }; - const getInitialExampleName = () => { - const baseName = 'example'; - const existingExamples = item.draft?.examples || item.examples || []; - let maxCounter = 0; - existingExamples.forEach((example) => { - const exampleName = example.name || ''; - if (exampleName.startsWith(baseName)) { - maxCounter++; - } - }); - return `${baseName} (${maxCounter})`; - }; - const folderItems = sortByNameThenSequence(filter(item.items, (i) => isItemAFolder(i))); const requestItems = sortItemsBySequence(filter(item.items, (i) => isItemARequest(i))); @@ -439,7 +426,7 @@ const CollectionItem = ({ item, collectionUid, collectionPathname, searchText }) onClose={() => setCreateExampleModalOpen(false)} onSave={handleCreateExample} title="Create Response Example" - initialName={getInitialExampleName()} + initialName={getInitialExampleName(item)} />
{ return exampleToDraft; }; + +/** + * Generate an initial name for a new response example + * @param {Object} item - The request item that will contain the example + * @returns {string} - The suggested name for the new example + */ +export const getInitialExampleName = (item) => { + const baseName = 'example'; + const existingExamples = item.draft?.examples || item.examples || []; + const existingNames = new Set(existingExamples.map((example) => example.name || '').filter(Boolean)); + + if (!existingNames.has(baseName)) { + return baseName; + } + + let counter = 1; + while (true) { + const candidateName = `${baseName} (${counter})`; + if (!existingNames.has(candidateName)) { + return candidateName; + } + counter++; + } +}; diff --git a/packages/bruno-lang/v2/src/example/jsonToBru.js b/packages/bruno-lang/v2/src/example/jsonToBru.js index 71cd9bffe..b5b877b5f 100644 --- a/packages/bruno-lang/v2/src/example/jsonToBru.js +++ b/packages/bruno-lang/v2/src/example/jsonToBru.js @@ -44,14 +44,9 @@ const jsonToExampleBru = (json) => { // Request block bru += '\nrequest: {\n'; - if (url) { - bru += ` url: ${url}\n`; - } + bru += ` url: ${url}\n`; - // Add method field right after url - if (method) { - bru += ` method: ${method}\n`; - } + bru += ` method: ${method}\n`; // Add mode field inside request block, right after method if (request && request.body && request.body.mode) { diff --git a/packages/bruno-lang/v2/tests/examples/examples.spec.js b/packages/bruno-lang/v2/tests/examples/examples.spec.js index 6181390b0..6d1c5b5aa 100644 --- a/packages/bruno-lang/v2/tests/examples/examples.spec.js +++ b/packages/bruno-lang/v2/tests/examples/examples.spec.js @@ -204,15 +204,16 @@ get { type: 'http' }, http: { - method: 'get', - url: 'https://api.example.com/test' + url: 'https://api.example.com/test', + method: 'get' }, examples: [ { name: 'Example Request', description: 'A simple example', request: { - url: 'https://api.example.com/example' + url: 'https://api.example.com/example', + method: 'get' } } ] @@ -233,6 +234,7 @@ example { request: { url: https://api.example.com/example + method: get } } `;