fix: preserve draft state when creating variables via varInfoToolbar (#6152)

* fix: preserve draft state when creating variables via varInfoToolbar
This commit is contained in:
Pooja
2025-11-21 15:47:23 +05:30
committed by GitHub
parent cc3d6a961a
commit fb420fcea4
6 changed files with 294 additions and 264 deletions

View File

@@ -189,17 +189,36 @@ export const renderVarInfo = (token, options) => {
// Detect variable scope
scopeInfo = getVariableScope(variableName, collection, item);
// If variable doesn't exist in any scope, default to creating it at request level
// If variable doesn't exist in any scope, determine scope based on context
if (!scopeInfo) {
if (item) {
// Create as request variable if we have an item context
// Determine if item is a folder or request
const isFolder = item.type === 'folder';
if (isFolder) {
// We're in folder settings - create as folder variable
scopeInfo = {
type: 'folder',
value: '', // Empty value for new variable
data: { folder: item, variable: null } // variable is null since it doesn't exist yet
};
} else {
// We're in a request - create as request variable
scopeInfo = {
type: 'request',
value: '', // Empty value for new variable
data: { item, variable: null } // variable is null since it doesn't exist yet
};
}
} else if (collection) {
// No item context but we have collection - create as collection variable
scopeInfo = {
type: 'request',
value: '', // Empty value for new variable
data: { item, variable: null } // variable is null since it doesn't exist yet
type: 'collection',
value: '',
data: { collection, variable: null }
};
} else {
// If no item context, show as undefined
// No context at all, show as undefined
scopeInfo = {
type: 'undefined',
value: '',

View File

@@ -1280,15 +1280,21 @@ const mergeVars = (collection, requestTreePath = []) => {
}
});
for (let i of requestTreePath) {
if (!i) {
continue;
}
if (i.type === 'folder') {
let vars = get(i, 'root.request.vars.req', []);
// Check draft first, then fall back to root
const folderRoot = i.draft || i.root;
let vars = get(folderRoot, 'request.vars.req', []);
vars.forEach((_var) => {
if (_var.enabled) {
folderVariables[_var.name] = _var.value;
}
});
} else {
let vars = get(i, 'request.vars.req', []);
let vars = i.draft ? get(i, 'draft.request.vars.req', []) : get(i, 'request.vars.req', []);
vars.forEach((_var) => {
if (_var.enabled) {
requestVariables[_var.name] = _var.value;
@@ -1521,8 +1527,9 @@ export const getVariableScope = (variableName, collection, item) => {
}
// 1. Check Request Variables (highest priority)
if (item && item.request && item.request.vars && item.request.vars.req) {
const requestVar = item.request.vars.req.find((v) => v.name === variableName && v.enabled);
if (item) {
const requestVars = item.draft ? get(item, 'draft.request.vars.req', []) : get(item, 'request.vars.req', []);
const requestVar = requestVars.find((v) => v.name === variableName && v.enabled);
if (requestVar) {
return {
type: 'request',
@@ -1536,8 +1543,14 @@ export const getVariableScope = (variableName, collection, item) => {
const requestTreePath = getTreePathFromCollectionToItem(collection, item);
for (let i = requestTreePath.length - 1; i >= 0; i--) {
const pathItem = requestTreePath[i];
if (!pathItem) {
continue;
}
if (pathItem.type === 'folder') {
const folderVars = get(pathItem, 'root.request.vars.req', []);
// Check draft first, then fall back to root
const folderRoot = pathItem.draft || pathItem.root;
const folderVars = get(folderRoot, 'request.vars.req', []);
const folderVar = folderVars.find((v) => v.name === variableName && v.enabled);
if (folderVar) {
return {
@@ -1565,7 +1578,9 @@ export const getVariableScope = (variableName, collection, item) => {
}
// 4. Check Collection Variables
const collectionVars = get(collection, 'root.request.vars.req', []);
// Check draft first, then fall back to root
const collectionRoot = (collection.draft && collection.draft.root) || collection.root || {};
const collectionVars = get(collectionRoot, 'request.vars.req', []);
const collectionVar = collectionVars.find((v) => v.name === variableName && v.enabled);
if (collectionVar) {
return {