diff --git a/packages/bruno-converters/src/openapi/openapi-to-bruno.js b/packages/bruno-converters/src/openapi/openapi-to-bruno.js index 293de524b..0d177af30 100644 --- a/packages/bruno-converters/src/openapi/openapi-to-bruno.js +++ b/packages/bruno-converters/src/openapi/openapi-to-bruno.js @@ -364,8 +364,8 @@ export const parseOpenApiCollection = (data) => { return; } - // TODO what if info.title not defined? - brunoCollection.name = collectionData.info.title; + brunoCollection.name = collectionData.info?.title?.trim() || 'Untitled Collection'; + let servers = collectionData.servers || []; // Create environments based on the servers @@ -428,8 +428,10 @@ export const parseOpenApiCollection = (data) => { brunoCollection.items = brunoCollectionItems; return brunoCollection; } catch (err) { - console.error(err); - throw new Error('An error occurred while parsing the OpenAPI collection'); + if (!(err instanceof Error)) { + throw new Error('Unknown error'); + } + throw err; } }; @@ -445,8 +447,11 @@ export const openApiToBruno = (openApiSpecification) => { const validatedCollection = validateSchema(hydratedCollection); return validatedCollection } catch (err) { - console.error(err); - throw new Error('Import collection failed'); + console.error('Error converting OpenAPI to Bruno:', err); + if (!(err instanceof Error)) { + throw new Error('Unknown error'); + } + throw err; } }; diff --git a/packages/bruno-converters/tests/openapi/openapi-to-bruno/openapi-to-bruno.spec.js b/packages/bruno-converters/tests/openapi/openapi-to-bruno/openapi-to-bruno.spec.js index 7c2c60409..f690431f5 100644 --- a/packages/bruno-converters/tests/openapi/openapi-to-bruno/openapi-to-bruno.spec.js +++ b/packages/bruno-converters/tests/openapi/openapi-to-bruno/openapi-to-bruno.spec.js @@ -7,6 +7,108 @@ describe('openapi-collection', () => { expect(brunoCollection).toMatchObject(expectedOutput); }); + + it('trims whitespace from info.title and uses the trimmed value as the collection name', () => { + const openApiWithTitle = ` +openapi: '3.0.0' +info: + version: '1.0.0' + title: ' My API ' +paths: + /get: + get: + summary: 'Request' + operationId: 'getRequest' + responses: + '200': + description: 'OK' +servers: + - url: 'https://example.com' +`; + const result = openApiToBruno(openApiWithTitle); + expect(result.name).toBe('My API'); + }); + + it('defaults to Untitled Collection if info.title is an empty string or only whitespace', () => { + const openApiWithTitle = ` +openapi: '3.0.0' +info: + version: '1.0.0' + title: ' ' +paths: + /get: + get: + summary: 'Request' + operationId: 'getRequest' + responses: + '200': + description: 'OK' +servers: + - url: 'https://example.com' +`; + const result = openApiToBruno(openApiWithTitle); + expect(result.name).toBe('Untitled Collection'); + }); + + it('defaults to Untitled Collection if info.title is an empty string', () => { + const openApiWithEmptyTitle = ` +openapi: '3.0.0' +info: + version: '1.0.0' + title: '' +paths: + /get: + get: + summary: 'Request' + operationId: 'getRequest' + responses: + '200': + description: 'OK' +servers: + - url: 'https://example.com' +`; + const result = openApiToBruno(openApiWithEmptyTitle); + expect(result.name).toBe('Untitled Collection'); + }); + + it('defaults to Untitled Collection if info.title is missing', () => { + const openApiWithoutTitle = ` +openapi: '3.0.0' +info: + version: '1.0.0' +paths: + /get: + get: + summary: 'Request' + operationId: 'getRequest' + responses: + '200': + description: 'OK' +servers: + - url: 'https://example.com' +`; + const result = openApiToBruno(openApiWithoutTitle); + expect(result.name).toBe('Untitled Collection'); + }); + + it('defaults to Untitled Collection if info is missing entirely', () => { + const openApiWithMissingInfo = ` +openapi: '3.0.0' +paths: + /get: + get: + summary: 'Request' + operationId: 'getRequest' + responses: + '200': + description: 'OK' +servers: + - url: 'https://example.com' +`; + const result = openApiToBruno(openApiWithMissingInfo); + expect(result.name).toBe('Untitled Collection'); + }); + }); const openApiCollectionString = `