From 38e6956b19362cad07330da4e4168449b91786c1 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 17 Sep 2021 17:18:52 +0100 Subject: [PATCH] Fixes for #2634 - make sure string templates library handles conversion of raw JSON objects to stringified correctly. --- packages/server/src/api/controllers/automation.js | 14 +++++++++++++- .../src/automations/steps/outgoingWebhook.js | 10 +++++++--- packages/string-templates/src/helpers/index.js | 7 +++++++ packages/string-templates/test/basic.spec.js | 10 ++++++++++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/packages/server/src/api/controllers/automation.js b/packages/server/src/api/controllers/automation.js index 60a0928e33..841473a4ff 100644 --- a/packages/server/src/api/controllers/automation.js +++ b/packages/server/src/api/controllers/automation.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) { const appId = ctx.appId const db = new CouchDB(appId) let automation = await db.get(ctx.params.id) await setTestFlag(automation._id) + const testInput = prepareTestInput(ctx.request.body) const response = await triggers.externalTrigger( automation, { - ...ctx.request.body, + ...testInput, appId, }, { getResponses: true } diff --git a/packages/server/src/automations/steps/outgoingWebhook.js b/packages/server/src/automations/steps/outgoingWebhook.js index 6194e1052b..a509e0e9d0 100644 --- a/packages/server/src/automations/steps/outgoingWebhook.js +++ b/packages/server/src/automations/steps/outgoingWebhook.js @@ -97,12 +97,16 @@ exports.run = async function ({ inputs }) { "Content-Type": "application/json", } - if (headers && headers.length !== 0) { + if (headers) { try { - const customHeaders = JSON.parse(headers) + const customHeaders = + typeof headers === "string" ? JSON.parse(headers) : headers request.headers = { ...request.headers, ...customHeaders } } catch (err) { - console.error(err) + return { + success: false, + response: "Unable to process headers, must be a JSON object.", + } } } } diff --git a/packages/string-templates/src/helpers/index.js b/packages/string-templates/src/helpers/index.js index 1ff729d187..23a20c90c7 100644 --- a/packages/string-templates/src/helpers/index.js +++ b/packages/string-templates/src/helpers/index.js @@ -19,6 +19,13 @@ const HELPERS = [ }), // this help is applied to all statements 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 if (value == null || typeof value !== "string") { return value || "" diff --git a/packages/string-templates/test/basic.spec.js b/packages/string-templates/test/basic.spec.js index 4536a159df..2e63ce8a5f 100644 --- a/packages/string-templates/test/basic.spec.js +++ b/packages/string-templates/test/basic.spec.js @@ -81,6 +81,16 @@ describe("Test that the object processing works correctly", () => { 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 () => { let error = null try {