From aff7c405cd391fd39b5b160623414f0b8f74d1b3 Mon Sep 17 00:00:00 2001 From: lohit Date: Wed, 20 Nov 2024 03:38:59 +0530 Subject: [PATCH] fix: import openapi -- baseUrl env value should not include trailing slash (#3440) * fix: openapi baseUrl env value should remove trailing slash * feat: updates --- packages/bruno-app/jest.config.js | 16 +++++ .../src/utils/importers/openapi-collection.js | 4 +- .../importers/openapi-collection.spec.js | 67 +++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 packages/bruno-app/jest.config.js create mode 100644 packages/bruno-app/src/utils/importers/openapi-collection.spec.js diff --git a/packages/bruno-app/jest.config.js b/packages/bruno-app/jest.config.js new file mode 100644 index 000000000..5d94a67b7 --- /dev/null +++ b/packages/bruno-app/jest.config.js @@ -0,0 +1,16 @@ +module.exports = { + rootDir: '.', + moduleNameMapper: { + '^assets/(.*)$': '/src/assets/$1', + '^components/(.*)$': '/src/components/$1', + '^hooks/(.*)$': '/src/hooks/$1', + '^themes/(.*)$': '/src/themes/$1', + '^api/(.*)$': '/src/api/$1', + '^pageComponents/(.*)$': '/src/pageComponents/$1', + '^providers/(.*)$': '/src/providers/$1', + '^utils/(.*)$': '/src/utils/$1' + }, + clearMocks: true, + moduleDirectories: ['node_modules', 'src'], + testEnvironment: 'node' +}; diff --git a/packages/bruno-app/src/utils/importers/openapi-collection.js b/packages/bruno-app/src/utils/importers/openapi-collection.js index 01407878a..955429079 100644 --- a/packages/bruno-app/src/utils/importers/openapi-collection.js +++ b/packages/bruno-app/src/utils/importers/openapi-collection.js @@ -316,7 +316,7 @@ const getDefaultUrl = (serverObject) => { url = url.replace(`{${variableName}}`, sub); }); } - return url.endsWith('/') ? url : `${url}/`; + return url.endsWith('/') ? url.slice(0, -1) : url; }; const getSecurity = (apiSpec) => { @@ -353,7 +353,7 @@ const openAPIRuntimeExpressionToScript = (expression) => { return expression; }; -const parseOpenApiCollection = (data) => { +export const parseOpenApiCollection = (data) => { const brunoCollection = { name: '', uid: uuid(), diff --git a/packages/bruno-app/src/utils/importers/openapi-collection.spec.js b/packages/bruno-app/src/utils/importers/openapi-collection.spec.js new file mode 100644 index 000000000..309221356 --- /dev/null +++ b/packages/bruno-app/src/utils/importers/openapi-collection.spec.js @@ -0,0 +1,67 @@ +import { parseOpenApiCollection } from './openapi-collection'; +import { uuid } from 'utils/common'; + +jest.mock('utils/common'); + +describe('openapi importer util functions', () => { + afterEach(jest.clearAllMocks); + + it('should convert openapi object to bruno collection correctly', async () => { + const input = { + openapi: '3.0.3', + info: { + title: 'Sample API with Multiple Servers', + description: 'API spec with multiple servers.', + version: '1.0.0' + }, + servers: [ + { url: 'https://api.example.com/v1', description: 'Production Server' }, + { url: 'https://staging-api.example.com/v1', description: 'Staging Server' }, + { url: 'http://localhost:3000/v1', description: 'Local Server' } + ], + paths: { + '/users': { + get: { + summary: 'Get a list of users', + parameters: [ + { name: 'page', in: 'query', required: false, schema: { type: 'integer' } }, + { name: 'limit', in: 'query', required: false, schema: { type: 'integer' } } + ], + responses: { + '200': { description: 'A list of users' } + } + } + } + } + }; + + const expectedOutput = { + name: 'Sample API with Multiple Servers', + version: '1', + items: [ + { + name: 'Get a list of users', + type: 'http-request', + request: { + url: '{{baseUrl}}/users', + method: 'GET', + params: [ + { name: 'page', value: '', enabled: false, type: 'query' }, + { name: 'limit', value: '', enabled: false, type: 'query' } + ] + } + } + ], + environments: [ + { name: 'Production Server', variables: [{ name: 'baseUrl', value: 'https://api.example.com/v1' }] }, + { name: 'Staging Server', variables: [{ name: 'baseUrl', value: 'https://staging-api.example.com/v1' }] }, + { name: 'Local Server', variables: [{ name: 'baseUrl', value: 'http://localhost:3000/v1' }] } + ] + }; + + const result = await parseOpenApiCollection(input); + + expect(result).toMatchObject(expectedOutput); + expect(uuid).toHaveBeenCalledTimes(10); + }); +});