mirror of
https://github.com/usebruno/bruno.git
synced 2026-07-07 14:08:38 +00:00
fix(apispec): prevent crash on non-array specs and fix Windows spec listing (BRU-3556) (#8255)
This commit is contained in:
@@ -11,16 +11,6 @@ const {
|
||||
|
||||
const DEFAULT_WORKSPACE_NAME = 'My Workspace';
|
||||
|
||||
const normalizeWorkspaceConfig = (config) => {
|
||||
return {
|
||||
...config,
|
||||
name: config.info?.name,
|
||||
type: config.info?.type,
|
||||
collections: config.collections || [],
|
||||
apiSpecs: config.specs || []
|
||||
};
|
||||
};
|
||||
|
||||
const prepareWorkspaceConfigForClient = (workspaceConfig, isDefault) => {
|
||||
if (isDefault) {
|
||||
return {
|
||||
@@ -56,7 +46,7 @@ const openApiSpec = async (win, watcher, apiSpecPath, options = {}) => {
|
||||
|
||||
if (fs.existsSync(workspaceFilePath)) {
|
||||
const workspaceConfig = readWorkspaceConfig(options.workspacePath);
|
||||
const specs = workspaceConfig.specs || [];
|
||||
const specs = workspaceConfig.specs;
|
||||
|
||||
const specName = path.basename(apiSpecPath, path.extname(apiSpecPath));
|
||||
|
||||
@@ -75,10 +65,9 @@ const openApiSpec = async (win, watcher, apiSpecPath, options = {}) => {
|
||||
});
|
||||
|
||||
const updatedConfig = readWorkspaceConfig(options.workspacePath);
|
||||
const normalizedConfig = normalizeWorkspaceConfig(updatedConfig);
|
||||
const workspaceUid = getWorkspaceUid(options.workspacePath);
|
||||
const isDefault = workspaceUid === 'default';
|
||||
const configForClient = prepareWorkspaceConfigForClient(normalizedConfig, isDefault);
|
||||
const configForClient = prepareWorkspaceConfigForClient(updatedConfig, isDefault);
|
||||
win.webContents.send('main:workspace-config-updated', options.workspacePath, workspaceUid, configForClient);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ const path = require('path');
|
||||
const chokidar = require('chokidar');
|
||||
const yaml = require('js-yaml');
|
||||
const { generateUidBasedOnHash, uuid } = require('../utils/common');
|
||||
const { getWorkspaceUid } = require('../utils/workspace-config');
|
||||
const { getWorkspaceUid, normalizeWorkspaceConfig } = require('../utils/workspace-config');
|
||||
const { parseEnvironment } = require('@usebruno/filestore');
|
||||
const EnvironmentSecretsStore = require('../store/env-secrets');
|
||||
const { decryptStringSafe } = require('../utils/encryption');
|
||||
@@ -19,16 +19,6 @@ const envHasSecrets = (environment) => {
|
||||
return secrets && secrets.length > 0;
|
||||
};
|
||||
|
||||
const normalizeWorkspaceConfig = (config) => {
|
||||
return {
|
||||
...config,
|
||||
name: config.info?.name,
|
||||
type: config.info?.type,
|
||||
collections: config.collections || [],
|
||||
apiSpecs: config.specs || []
|
||||
};
|
||||
};
|
||||
|
||||
const handleWorkspaceFileChange = (win, workspacePath) => {
|
||||
try {
|
||||
const workspaceFilePath = path.join(workspacePath, 'workspace.yml');
|
||||
|
||||
@@ -214,7 +214,7 @@ const registerWorkspaceIpc = (mainWindow, workspaceWatcher) => {
|
||||
return [];
|
||||
}
|
||||
|
||||
const specs = workspaceConfig.specs || [];
|
||||
const specs = Array.isArray(workspaceConfig.specs) ? workspaceConfig.specs : [];
|
||||
|
||||
const resolvedSpecs = specs
|
||||
.map((spec) => {
|
||||
|
||||
@@ -200,12 +200,19 @@ const createWorkspaceConfig = (workspaceName) => ({
|
||||
});
|
||||
|
||||
const normalizeWorkspaceConfig = (config) => {
|
||||
// Coerce `specs` to an array once. A malformed workspace.yml (e.g. `specs`
|
||||
// authored as a map) would otherwise flow through as a non-array and crash
|
||||
// both the renderer sidebar (.map) and the write paths (.findIndex/.filter).
|
||||
const specs = Array.isArray(config.specs) ? config.specs : [];
|
||||
return {
|
||||
...config,
|
||||
name: config.info?.name,
|
||||
type: config.info?.type,
|
||||
collections: config.collections || [],
|
||||
apiSpecs: config.specs || []
|
||||
specs,
|
||||
// Distinct array (not an alias of `specs`) so a later in-place mutation of
|
||||
// one field can't silently change the other.
|
||||
apiSpecs: [...specs]
|
||||
};
|
||||
};
|
||||
|
||||
@@ -686,6 +693,7 @@ module.exports = {
|
||||
validateWorkspacePath,
|
||||
validateWorkspaceDirectory,
|
||||
createWorkspaceConfig,
|
||||
normalizeWorkspaceConfig,
|
||||
readWorkspaceConfig,
|
||||
writeWorkspaceConfig,
|
||||
validateWorkspaceConfig,
|
||||
|
||||
Reference in New Issue
Block a user