Merge pull request #5009 from sanjaikumar-bruno/fix/openAPI-import-fail-when-the-title-is-missing

fix: handle undefined title in collection name and improve error handling
This commit is contained in:
lohit
2025-07-04 18:57:26 +05:30
committed by GitHub
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 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 = `