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

[Bug Fix 1288] "Undefined" for nested variables #1637

Merged
merged 5 commits into from
Aug 16, 2019
Merged
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
34 changes: 24 additions & 10 deletions packages/insomnia-app/app/common/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,34 @@ export async function getRenderContext(
const subEnvironment = await models.environment.getById(environmentId || 'n/a');

let keySource = {};
for (let key in (rootEnvironment || {}).data) {
keySource[key] = 'root';
// Function that gets Keys and stores their Source location
function getKeySource(subObject, inKey, inSource) {
for (const key of Object.keys(subObject)) {
if (Object.prototype.toString.call(subObject[key]) === '[object Object]') {
// Type is an Object, keep on going, recursively building the full key path
getKeySource(subObject[key], inKey + key + '.', inSource);
} else if (Object.prototype.toString.call(subObject[key]) === '[object Array]') {
// Type is an Array, Loop and store the full Key and Source in keySource
for (let i = 0, length = subObject[key].length; i < length; i++) {
keySource[inKey + key + '[' + i + ']'] = inSource;
}
} else {
// For all other types, store the full Key and Source in keySource
keySource[inKey + key] = inSource;
}
}
return keySource;
}

// Get Keys from root environment
getKeySource((rootEnvironment || {}).data, '', 'root');

// Get Keys from sub environment
if (subEnvironment) {
for (const key of Object.keys(subEnvironment.data || {})) {
if (subEnvironment.name) {
keySource[key] = subEnvironment.name;
}
}
getKeySource(subEnvironment.data || {}, '', subEnvironment.name || '');
}

// Get Keys from ancestors (e.g. Folders)
if (ancestors) {
for (let idx = 0; idx < ancestors.length; idx++) {
let ancestor: any = ancestors[idx] || {};
Expand All @@ -257,9 +273,7 @@ export async function getRenderContext(
ancestor.hasOwnProperty('environment') &&
ancestor.hasOwnProperty('name')
) {
for (const key of Object.keys(ancestor.environment || {})) {
keySource[key] = ancestor.name || '';
}
getKeySource(ancestor.environment || {}, '', ancestor.name || '');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. I like that this logic is grouped together in one place now.

}
}
}
Expand Down