Create default environments on openapi import using server url (#3267)

* added option to create environments using server urls

* Update openapi-collection.js

---------

Co-authored-by: Anusree Subash <anusree@usebruno.com>
Co-authored-by: Anoop M D <anoop.md1421@gmail.com>
This commit is contained in:
anusreesubash 2024-10-14 12:18:26 +05:30 committed by GitHub
parent 43cb2b82f3
commit 40fad99803
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -373,7 +373,7 @@ const parseOpenApiCollection = (data) => {
// Currently parsing of openapi spec is "do your best", that is // Currently parsing of openapi spec is "do your best", that is
// allows "invalid" openapi spec // allows "invalid" openapi spec
// assumes v3 if not defined. v2 no supported yet // Assumes v3 if not defined. v2 is not supported yet
if (collectionData.openapi && !collectionData.openapi.startsWith('3')) { if (collectionData.openapi && !collectionData.openapi.startsWith('3')) {
reject(new BrunoError('Only OpenAPI v3 is supported currently.')); reject(new BrunoError('Only OpenAPI v3 is supported currently.'));
return; return;
@ -382,7 +382,28 @@ const parseOpenApiCollection = (data) => {
// TODO what if info.title not defined? // TODO what if info.title not defined?
brunoCollection.name = collectionData.info.title; brunoCollection.name = collectionData.info.title;
let servers = collectionData.servers || []; let servers = collectionData.servers || [];
let baseUrl = servers[0] ? getDefaultUrl(servers[0]) : '';
// Create environments based on the servers
servers.forEach((server, index) => {
let baseUrl = getDefaultUrl(server);
let environmentName = server.description ? server.description : `Environment ${index + 1}`;
brunoCollection.environments.push({
uid: uuid(),
name: environmentName,
variables: [
{
uid: uuid(),
name: 'baseUrl',
value: baseUrl,
type: 'text',
enabled: true,
secret: false
},
]
});
});
let securityConfig = getSecurity(collectionData); let securityConfig = getSecurity(collectionData);
let allRequests = Object.entries(collectionData.paths) let allRequests = Object.entries(collectionData.paths)
@ -399,7 +420,7 @@ const parseOpenApiCollection = (data) => {
path: path.replace(/{([^}]+)}/g, ':$1'), // Replace placeholders enclosed in curly braces with colons path: path.replace(/{([^}]+)}/g, ':$1'), // Replace placeholders enclosed in curly braces with colons
operationObject: operationObject, operationObject: operationObject,
global: { global: {
server: baseUrl, server: '{{baseUrl}}',
security: securityConfig security: securityConfig
} }
}; };