Browse Source
Merge pull request #2639 from Budibase/fix/2634
Handling objects better in string templates (automations)
gh-pages
Michael Drury
5 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with
37 additions and
4 deletions
-
packages/server/src/api/controllers/automation.js
-
packages/server/src/automations/steps/outgoingWebhook.js
-
packages/string-templates/src/helpers/index.js
-
packages/string-templates/test/basic.spec.js
|
|
@ -189,15 +189,27 @@ exports.trigger = async function (ctx) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function prepareTestInput(input) { |
|
|
|
|
|
// prepare the test parameters
|
|
|
|
|
|
if (input.id && input.row) { |
|
|
|
|
|
input.row._id = input.id |
|
|
|
|
|
} |
|
|
|
|
|
if (input.revision && input.row) { |
|
|
|
|
|
input.row._rev = input.revision |
|
|
|
|
|
} |
|
|
|
|
|
return input |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
exports.test = async function (ctx) { |
|
|
exports.test = async function (ctx) { |
|
|
const appId = ctx.appId |
|
|
const appId = ctx.appId |
|
|
const db = new CouchDB(appId) |
|
|
const db = new CouchDB(appId) |
|
|
let automation = await db.get(ctx.params.id) |
|
|
let automation = await db.get(ctx.params.id) |
|
|
await setTestFlag(automation._id) |
|
|
await setTestFlag(automation._id) |
|
|
|
|
|
const testInput = prepareTestInput(ctx.request.body) |
|
|
const response = await triggers.externalTrigger( |
|
|
const response = await triggers.externalTrigger( |
|
|
automation, |
|
|
automation, |
|
|
{ |
|
|
{ |
|
|
...ctx.request.body, |
|
|
...testInput, |
|
|
appId, |
|
|
appId, |
|
|
}, |
|
|
}, |
|
|
{ getResponses: true } |
|
|
{ getResponses: true } |
|
|
|
|
|
@ -97,12 +97,16 @@ exports.run = async function ({ inputs }) { |
|
|
"Content-Type": "application/json", |
|
|
"Content-Type": "application/json", |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (headers && headers.length !== 0) { |
|
|
if (headers) { |
|
|
try { |
|
|
try { |
|
|
const customHeaders = JSON.parse(headers) |
|
|
const customHeaders = |
|
|
|
|
|
typeof headers === "string" ? JSON.parse(headers) : headers |
|
|
request.headers = { ...request.headers, ...customHeaders } |
|
|
request.headers = { ...request.headers, ...customHeaders } |
|
|
} catch (err) { |
|
|
} catch (err) { |
|
|
console.error(err) |
|
|
return { |
|
|
|
|
|
success: false, |
|
|
|
|
|
response: "Unable to process headers, must be a JSON object.", |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
@ -19,6 +19,13 @@ const HELPERS = [ |
|
|
}), |
|
|
}), |
|
|
// this help is applied to all statements
|
|
|
// this help is applied to all statements
|
|
|
new Helper(HelperFunctionNames.ALL, value => { |
|
|
new Helper(HelperFunctionNames.ALL, value => { |
|
|
|
|
|
if ( |
|
|
|
|
|
value != null && |
|
|
|
|
|
typeof value === "object" && |
|
|
|
|
|
value.toString() === "[object Object]" |
|
|
|
|
|
) { |
|
|
|
|
|
return new SafeString(JSON.stringify(value)) |
|
|
|
|
|
} |
|
|
// null/undefined values produce bad results
|
|
|
// null/undefined values produce bad results
|
|
|
if (value == null || typeof value !== "string") { |
|
|
if (value == null || typeof value !== "string") { |
|
|
return value || "" |
|
|
return value || "" |
|
|
|
|
|
@ -81,6 +81,16 @@ describe("Test that the object processing works correctly", () => { |
|
|
expect(error).not.toBeNull() |
|
|
expect(error).not.toBeNull() |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it("check objects get converted to string JSON automatically", async () => { |
|
|
|
|
|
const row = {a: 1} |
|
|
|
|
|
const output = await processString("{{ trigger.row }}", { |
|
|
|
|
|
trigger: { |
|
|
|
|
|
row, |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
expect(JSON.parse(output)).toEqual(row) |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
it("should be able to handle null objects", async () => { |
|
|
it("should be able to handle null objects", async () => { |
|
|
let error = null |
|
|
let error = null |
|
|
try { |
|
|
try { |
|
|
|