fix: handle undefined title in collection name and improve error handling

test: add unit tests for collection name handling based on OpenAPI title

fix: trim whitespace from info.title and improve default collection name handling

fix: simplify collection name assignment by using optional chaining

removed two console.log and improved the error message.

refactor: standardize single quotes in OpenAPI test cases

test: add case for empty title defaulting to Untitled Collection
This commit is contained in:
sanjai0py
2025-06-30 13:31:56 +05:30
parent 99873af281
commit a6a50f42a3
2 changed files with 113 additions and 6 deletions

View File

@@ -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;
}
};

View File

@@ -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 = `