Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: OpenAPI import: create a default environment #1240

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions packages/bruno-app/src/utils/importers/openapi-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ const readFile = (files) => {
};

const ensureUrl = (url) => {
let protUrl = url.startsWith('http') ? url : `http://${url}`;
// replace any double or triple slashes
return protUrl.replace(/([^:]\/)\/+/g, '$1');
return url.replace(/([^:]\/)\/+/g, '$1');
};

const buildEmptyJsonBody = (bodySchema) => {
Expand Down Expand Up @@ -323,7 +322,46 @@ const parseOpenApiCollection = (data) => {
// TODO what if info.title not defined?
brunoCollection.name = collectionData.info.title;
let servers = collectionData.servers || [];
let baseUrl = servers[0] ? getDefaultUrl(servers[0]) : '';

let defaultProtocol = '';
let defaultHost = '';
if (servers[0]) {
let fullUrl = getDefaultUrl(servers[0]);
let parts = fullUrl.split('://');
// if there is only one part after splitting, presume it is the host
if (parts.length > 1) {
defaultProtocol = parts[0];
defaultHost = parts[1];
} else {
defaultProtocol = 'http';
defaultHost = parts[0];
}
}

brunoCollection.environments = [
{
uid: uuid(),
name: 'default',
variables: [
{
uid: uuid(),
name: 'protocol',
value: defaultProtocol,
type: 'text',
enabled: true,
secret: false
},
{
uid: uuid(),
name: 'host',
value: defaultHost,
type: 'text',
enabled: true,
secret: false
}
]
}
];
let securityConfig = getSecurity(collectionData);

let allRequests = Object.entries(collectionData.paths)
Expand All @@ -340,7 +378,7 @@ const parseOpenApiCollection = (data) => {
path: path,
operationObject: operationObject,
global: {
server: baseUrl,
server: '{{protocol}}://{{host}}',
security: securityConfig
}
};
Expand Down