Skip to content

refactor: Replace tag-based grouping with path-based grouping in Open… #4544

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
126 changes: 82 additions & 44 deletions packages/bruno-converters/src/openapi/openapi-to-bruno.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ const transformOpenapiRequestItem = (request) => {
operationName = `${request.method} ${request.path}`;
}

// replace OpenAPI links in path by Bruno variables
let path = request.path.replace(/{([a-zA-Z]+)}/g, `{{${_operationObject.operationId}_$1}}`);

// Replace {param} with :param in the path
let path = request.path.replace(/{([a-zA-Z]+)}/g, ':$1');

const brunoRequestItem = {
uid: uuid(),
Expand Down Expand Up @@ -70,7 +71,7 @@ const transformOpenapiRequestItem = (request) => {
brunoRequestItem.request.params.push({
uid: uuid(),
name: param.name,
value: '',
value: param.schema?.default?.toString() || '',
description: param.description || '',
enabled: param.required,
type: 'query'
Expand All @@ -79,7 +80,7 @@ const transformOpenapiRequestItem = (request) => {
brunoRequestItem.request.params.push({
uid: uuid(),
name: param.name,
value: '',
value: param.schema?.default?.toString() || '',
description: param.description || '',
enabled: param.required,
type: 'path'
Expand All @@ -88,7 +89,7 @@ const transformOpenapiRequestItem = (request) => {
brunoRequestItem.request.headers.push({
uid: uuid(),
name: param.name,
value: '',
value: param.schema?.default?.toString() || '',
description: param.description || '',
enabled: param.required
});
Expand Down Expand Up @@ -251,35 +252,84 @@ const resolveRefs = (spec, components = spec?.components, cache = new Map()) =>
return resolved;
};

const groupRequestsByTags = (requests) => {
let _groups = {};
let ungrouped = [];
each(requests, (request) => {
let tags = request.operationObject.tags || [];
if (tags.length > 0) {
let tag = tags[0].trim(); // take first tag and trim whitespace

if (tag) {
if (!_groups[tag]) {
_groups[tag] = [];
}
_groups[tag].push(request);
} else {
ungrouped.push(request);
const groupRequestsByPath = (requests) => {
const pathGroups = {};

requests.forEach((request) => {
const pathSegments = request.path.split('/').filter(Boolean);
let currentPath = '';
let currentGroup = pathGroups;

pathSegments.forEach((segment, index) => {
if (segment.startsWith(':')) {
// Skip path parameters for folder names
return;
}
} else {
ungrouped.push(request);

currentPath = currentPath ? `${currentPath}/${segment}` : segment;

if (!currentGroup[currentPath]) {
currentGroup[currentPath] = {
name: segment,
path: currentPath,
items: [],
subGroups: {}
};
}

if (index === pathSegments.length - 1) {
currentGroup[currentPath].items.push(request);
}

currentGroup = currentGroup[currentPath].subGroups;
});

// If no valid segments (e.g., only path parameters), add to root
if (!pathSegments.some(segment => !segment.startsWith(':'))) {
if (!pathGroups.root) {
pathGroups.root = {
name: 'root',
path: '',
items: [],
subGroups: {}
};
}
pathGroups.root.items.push(request);
}
});

let groups = Object.keys(_groups).map((groupName) => {
return {
name: groupName,
requests: _groups[groupName]
};
});
const buildFolderStructure = (group) => {
// Transform request items
const items = group.items.map(transformOpenapiRequestItem);

// Process subfolders
const subFolders = [];
Object.values(group.subGroups).forEach(subGroup => {
// Process each subfolder recursively
const subFolderItems = buildFolderStructure(subGroup);

// Only create a folder if it has items
if (subFolderItems.length > 0) {
subFolders.push({
uid: uuid(),
name: subGroup.name,
type: 'folder',
items: subFolderItems
});
}
});

return [groups, ungrouped];
return [...items, ...subFolders];
};

const folders = Object.values(pathGroups).map(group => ({
uid: uuid(),
name: group.name,
type: 'folder',
items: buildFolderStructure(group)
}));

return folders;
};

const getDefaultUrl = (serverObject) => {
Expand Down Expand Up @@ -389,30 +439,18 @@ export const parseOpenApiCollection = (data) => {
.map(([method, operationObject]) => {
return {
method: method,
path: path.replace(/{([^}]+)}/g, ':$1'), // Replace placeholders enclosed in curly braces with colons
path: path,
operationObject: operationObject,
global: {
server: '{{baseUrl}}',
server: '{{baseUrl}}',
security: securityConfig
}
};
});
})
.reduce((acc, val) => acc.concat(val), []); // flatten

let [groups, ungroupedRequests] = groupRequestsByTags(allRequests);
let brunoFolders = groups.map((group) => {
return {
uid: uuid(),
name: group.name,
type: 'folder',
items: group.requests.map(transformOpenapiRequestItem)
};
});

let ungroupedItems = ungroupedRequests.map(transformOpenapiRequestItem);
let brunoCollectionItems = brunoFolders.concat(ungroupedItems);
brunoCollection.items = brunoCollectionItems;
brunoCollection.items = groupRequestsByPath(allRequests);
return brunoCollection;
} catch (err) {
console.error(err);
Expand Down
Loading