From 64bdef23ec1e8bca050b09b938b5ad1c998628b8 Mon Sep 17 00:00:00 2001 From: Abhishek S Lal Date: Wed, 1 Apr 2026 21:34:23 +0530 Subject: [PATCH] fix: include examples when writing collection items in CLI OpenAPI import (#7613) * feat: add support for examples in collection items - Enhanced the processCollectionItems function to include examples from imported collection items. - Added a new test case to verify that examples are correctly written to the output file during collection creation. * fix: coerce response status to number in collection creation tests Updated the test for createCollectionFromBrunoObject to ensure the response status is compared as a number, improving type consistency in assertions. --- packages/bruno-cli/src/utils/collection.js | 3 +- ...reate-collection-from-bruno-object.spec.js | 57 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/packages/bruno-cli/src/utils/collection.js b/packages/bruno-cli/src/utils/collection.js index d708b9f4b..5f6e8d9fc 100644 --- a/packages/bruno-cli/src/utils/collection.js +++ b/packages/bruno-cli/src/utils/collection.js @@ -641,7 +641,8 @@ const processCollectionItems = async (items = [], currentPath, options = {}) => assertions: item.request?.assertions || [], tests: item.request?.tests || '', docs: item.request?.docs || '' - } + }, + examples: item.examples || [] }; // Convert to YML format and write to file diff --git a/packages/bruno-cli/tests/utils/collection/create-collection-from-bruno-object.spec.js b/packages/bruno-cli/tests/utils/collection/create-collection-from-bruno-object.spec.js index 332a87b42..a2e315a9f 100644 --- a/packages/bruno-cli/tests/utils/collection/create-collection-from-bruno-object.spec.js +++ b/packages/bruno-cli/tests/utils/collection/create-collection-from-bruno-object.spec.js @@ -124,6 +124,63 @@ describe('createCollectionFromBrunoObject', () => { expect(nestedRequest).toHaveProperty('request.method', 'GET'); }); + it('writes examples from imported collection items', async () => { + createOutputDir(); + + await createCollectionFromBrunoObject( + { + name: 'examples-collection', + items: [ + { + type: 'http-request', + name: 'Get Users', + filename: 'get-users.bru', + seq: 1, + request: { + method: 'GET', + url: 'https://api.example.com/users' + }, + examples: [ + { + uid: 'ex1', + name: 'Success Response', + type: 'http-request', + request: { + url: 'https://api.example.com/users', + method: 'GET', + headers: [], + params: [], + body: { mode: 'none' } + }, + response: { + status: 200, + statusText: 'OK', + headers: [{ uid: 'h1', name: 'Content-Type', value: 'application/json', enabled: true }], + body: { + type: 'json', + content: JSON.stringify([{ id: 1, name: 'John' }], null, 2) + } + } + } + ] + } + ] + }, + outputDir, + { format: 'bru' } + ); + + const httpPath = path.join(outputDir, 'get-users.bru'); + expect(fs.existsSync(httpPath)).toBe(true); + + const parsed = parseBruRequestFromPath(httpPath); + expect(parsed.examples).toBeDefined(); + expect(parsed.examples).toHaveLength(1); + expect(parsed.examples[0].name).toBe('Success Response'); + expect(Number(parsed.examples[0].response.status)).toBe(200); + expect(parsed.examples[0].response.body.content).toContain('John'); + }); + it('throws for unsupported item types', async () => { createOutputDir();