From 04ea2b99e9a0ec190c37172bd7c4d397eef99cf4 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 27 Nov 2024 18:37:05 +0000 Subject: [PATCH] Fixing issue uncovered by test case. --- .../src/api/controllers/row/staticFormula.ts | 17 ++++++++++++++++- .../server/src/api/routes/tests/row.spec.ts | 17 ++--------------- .../rowProcessor/bbReferenceProcessor.ts | 4 ++-- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/packages/server/src/api/controllers/row/staticFormula.ts b/packages/server/src/api/controllers/row/staticFormula.ts index b9751b1ce2..ff2381e61c 100644 --- a/packages/server/src/api/controllers/row/staticFormula.ts +++ b/packages/server/src/api/controllers/row/staticFormula.ts @@ -19,6 +19,17 @@ import { cloneDeep, merge } from "lodash/fp" import sdk from "../../../sdk" import * as pro from "@budibase/pro" +function mergeRows(row1: Row, row2: Row) { + const merged = merge(row1, row2) + // make sure any specifically undefined fields are removed + for (const key of Object.keys(row2)) { + if (row2[key] === undefined) { + delete merged[key] + } + } + return merged +} + /** * This function runs through a list of enriched rows, looks at the rows which * are related and then checks if they need the state of their formulas @@ -164,7 +175,11 @@ export async function finaliseRow( await db.put(row) const retrieved = await db.tryGet(row._id) - enrichedRow = merge(retrieved, enrichedRow) + if (!retrieved) { + throw new Error(`Unable to retrieve row ${row._id} after saving.`) + } + + enrichedRow = mergeRows(retrieved, enrichedRow) enrichedRow = await processFormulas(table, enrichedRow, { dynamic: false, }) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index 14be60aa8c..25c9b8abef 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -2571,14 +2571,12 @@ if (descriptions.length) { let tableId: string let o2mData: Row[] let m2mData: Row[] - let isRelationship: boolean beforeAll(async () => { const table = await config.api.table.save( defaultTable({ schema: relSchema() }) ) tableId = table._id! - isRelationship = relSchema().user.type === FieldType.LINK o2mData = [ await dataGenerator(o2mTable._id!), @@ -2755,19 +2753,8 @@ if (descriptions.length) { user: null, users: null, }) - expect(updatedRow).toEqual({ - name: "foo", - description: "bar", - tableId, - _id: row._id, - _rev: expect.any(String), - id: isInternal ? undefined : expect.any(Number), - type: isInternal ? "row" : undefined, - createdAt: isInternal ? new Date().toISOString() : undefined, - updatedAt: isInternal ? new Date().toISOString() : undefined, - users: isRelationship ? undefined : [], - user: isRelationship ? undefined : [], - }) + expect(updatedRow.user).toBeUndefined() + expect(updatedRow.users).toBeUndefined() }) it("fetch all will populate the relationships", async () => { diff --git a/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts b/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts index b02ea2ff60..6c8ecc3167 100644 --- a/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts +++ b/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts @@ -153,10 +153,10 @@ export async function processOutputBBReference( } export async function processOutputBBReferences( - value: string | null | undefined, + value: string | string[] | null | undefined, subtype: BBReferenceFieldSubType ): Promise { - if (!value) { + if (!value || (Array.isArray(value) && value.length === 0)) { return undefined } const ids =