Fix for recursive rendering

This commit is contained in:
Gregory Schier 2018-07-25 16:58:01 -04:00
parent c5e6326cbf
commit 40d6f610c4
6 changed files with 619 additions and 4802 deletions

View File

@ -4,5 +4,6 @@
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "none"
"trailingComma": "none",
"printWidth": 100
}

View File

@ -1,4 +1,4 @@
FROM ubuntu:14.04
FROM ubuntu:16.04
# Install core deps
RUN apt-get update && apt-get upgrade -y
@ -7,6 +7,7 @@ RUN apt-get install -y \
autoconf \
libtool \
pkg-config \
snapcraft \
wget
# Install Node and app-related dependencies

View File

@ -15,9 +15,7 @@ describe('render()', () => {
it('renders custom tag: uuid', async () => {
const rendered = await renderUtils.render('Hello {% uuid %}!');
expect(rendered).toMatch(
/Hello [a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}!/
);
expect(rendered).toMatch(/Hello [a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}!/);
});
it('renders custom tag: timestamp', async () => {
@ -86,8 +84,7 @@ describe('buildRenderContext()', () => {
// This is longer than 3 because it multiplies every time (1 -> 2 -> 4 -> 8)
expect(context).toEqual({
recursive:
'{{ recursive }}/hello/hello/hello/hello/hello/hello/hello/hello'
recursive: '{{ recursive }}/hello/hello/hello/hello/hello/hello/hello/hello'
});
});
@ -133,6 +130,36 @@ describe('buildRenderContext()', () => {
});
});
it('does the thing', async () => {
const root = {
type: models.environment.type,
data: { url: 'insomnia.rest' }
};
const sub = {
type: models.environment.type,
data: { url: '{{ url }}/sub' }
};
const ancestors = [
{
// Folder Environment
type: models.requestGroup.type,
environment: {
url: '{{ url }}/{{ name }}',
name: 'folder'
}
}
];
const context = await renderUtils.buildRenderContext(ancestors, root, sub);
expect(context).toEqual({
url: 'insomnia.rest/sub/folder',
name: 'folder'
});
});
it('render up to 3 recursion levels', async () => {
const ancestors = [
{

View File

@ -67,17 +67,22 @@ export async function buildRenderContext(
* original base_url of google.com would be lost.
*/
if (typeof renderContext[key] === 'string') {
const rendered = await render(
const isSelfRecursive = envObject[key].match(`{{ ?${key}[ |][^}]*}}`);
if (isSelfRecursive) {
// If we're overwriting a variable that contains itself, make sure we
// render it first
renderContext[key] = await render(
envObject[key],
{ [key]: renderContext[key] }, // Only render with key being overwritten
renderContext, // Only render with key being overwritten
null,
KEEP_ON_ERROR,
'Environment'
);
// Only use rendered version if rendering actually yielded something different.
renderContext[key] =
rendered !== envObject[key] ? rendered : envObject[key];
} else {
// Otherwise it's just a regular replacement
renderContext[key] = envObject[key];
}
} else {
renderContext[key] = envObject[key];
}
@ -123,11 +128,7 @@ export async function render<T>(
// Make a deep copy so no one gets mad :)
const newObj = clone(obj);
async function next(
x: any,
path: string,
first: boolean = false
): Promise<any> {
async function next(x: any, path: string, first: boolean = false): Promise<any> {
if (blacklistPathRegex && path.match(blacklistPathRegex)) {
return x;
}
@ -222,12 +223,7 @@ export async function getRenderContext(
baseContext.getPurpose = () => purpose;
// Generate the context we need to render
return buildRenderContext(
ancestors,
rootEnvironment,
subEnvironment,
baseContext
);
return buildRenderContext(ancestors, rootEnvironment, subEnvironment, baseContext);
}
export async function getRenderedRequestAndContext(
@ -244,12 +240,7 @@ export async function getRenderedRequestAndContext(
const parentId = workspace ? workspace._id : 'n/a';
const cookieJar = await models.cookieJar.getOrCreateForParentId(parentId);
const renderContext = await getRenderContext(
request,
environmentId,
ancestors,
purpose
);
const renderContext = await getRenderContext(request, environmentId, ancestors, purpose);
// HACK: Switch '#}' to '# }' to prevent Nunjucks from barfing
// https://github.com/getinsomnia/insomnia/issues/895
@ -272,25 +263,18 @@ export async function getRenderedRequestAndContext(
const renderedCookieJar = renderResult._cookieJar;
// Remove disabled params
renderedRequest.parameters = renderedRequest.parameters.filter(
p => !p.disabled
);
renderedRequest.parameters = renderedRequest.parameters.filter(p => !p.disabled);
// Remove disabled headers
renderedRequest.headers = renderedRequest.headers.filter(p => !p.disabled);
// Remove disabled body params
if (renderedRequest.body && Array.isArray(renderedRequest.body.params)) {
renderedRequest.body.params = renderedRequest.body.params.filter(
p => !p.disabled
);
renderedRequest.body.params = renderedRequest.body.params.filter(p => !p.disabled);
}
// Remove disabled authentication
if (
renderedRequest.authentication &&
renderedRequest.authentication.disabled
) {
if (renderedRequest.authentication && renderedRequest.authentication.disabled) {
renderedRequest.authentication = {};
}
@ -320,8 +304,7 @@ export async function getRenderedRequestAndContext(
name: renderedRequest.name,
parameters: renderedRequest.parameters,
parentId: renderedRequest.parentId,
settingDisableRenderRequestBody:
renderedRequest.settingDisableRenderRequestBody,
settingDisableRenderRequestBody: renderedRequest.settingDisableRenderRequestBody,
settingEncodeUrl: renderedRequest.settingEncodeUrl,
settingSendCookies: renderedRequest.settingSendCookies,
settingStoreCookies: renderedRequest.settingStoreCookies,
@ -338,11 +321,7 @@ export async function getRenderedRequest(
environmentId: string,
purpose?: string
): Promise<RenderedRequest> {
const result = await getRenderedRequestAndContext(
request,
environmentId,
purpose
);
const result = await getRenderedRequestAndContext(request, environmentId, purpose);
return result.request;
}

File diff suppressed because it is too large Load Diff

View File

@ -169,7 +169,7 @@
"cross-env": "^2.0.0",
"css-loader": "^0.28.11",
"electron": "^1.8.7",
"electron-builder": "^20.10.0",
"electron-builder": "^20.14.7",
"electron-builder-lib": "^20.10.0",
"electron-builder-squirrel-windows": "^20.10.0",
"electron-download-tf": "^4.3.4",