diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte index 96397490f8..271a82f16b 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte @@ -387,7 +387,7 @@ editableColumn.relationshipType = RelationshipType.MANY_TO_MANY } else if (editableColumn.type === FieldType.FORMULA) { editableColumn.formulaType = "dynamic" - editableColumn.responseType = FIELDS.STRING.type + editableColumn.responseType = field.responseType || FIELDS.STRING.type } } @@ -784,7 +784,7 @@ ]} getOptionLabel={option => option.name} getOptionValue={option => option.type} - tooltip="Formulas by default will return a string - however if you need a native type the response can be coerced." + tooltip="Formulas by default will return a string - however if you need a another type the response can be coerced." /> diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index 9ea2b5572e..5bdd341beb 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -3308,6 +3308,14 @@ datasourceDescribe( expect(rows[0].formula).toBe(2) }) + it("should coerce handlebars to string (default)", async () => { + await updateFormulaColumn("{{ add 1 1 }}", { + responseType: FieldType.STRING, + }) + const { rows } = await config.api.row.search(table._id!) + expect(rows[0].formula).toBe("2") + }) + isInternal && it("should coerce a static handlebars formula", async () => { await updateFormulaColumn(encodeJS("return 1"), { diff --git a/packages/server/src/utilities/rowProcessor/utils.ts b/packages/server/src/utilities/rowProcessor/utils.ts index 15c0612fae..9dbeb8ebb2 100644 --- a/packages/server/src/utilities/rowProcessor/utils.ts +++ b/packages/server/src/utilities/rowProcessor/utils.ts @@ -100,8 +100,9 @@ export async function processFormulas( const result = processStringSync(formula, context) try { return responseType ? coerce(result, responseType) : result - } catch (err) { + } catch (err: any) { // if the coercion fails, we return empty row contents + span?.addTags({ coercionError: err.message }) return undefined } }),