From 68082eecf9e7529bf2bea124c402d2d781288a4a Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 13 Jun 2023 18:21:22 +0100 Subject: [PATCH] Fix for circular issue with primary display fields on SQL tables introduced in most recent update - if somehow the primary display field is set to a relationship field there was a chance of cyclic structure occurring which Koa could not convert to JSON. --- .../api/controllers/row/ExternalRequest.ts | 6 +++++- packages/server/src/integrations/utils.ts | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index b13ec22cfd..f6b75aca05 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -19,6 +19,7 @@ import { breakRowIdField, convertRowId, generateRowIdField, + getPrimaryDisplay, isRowId, isSQL, } from "../../../integrations/utils" @@ -391,7 +392,10 @@ export class ExternalRequest { } } relatedRow = processFormulas(linkedTable, relatedRow) - const relatedDisplay = display ? relatedRow[display] : undefined + let relatedDisplay + if (display) { + relatedDisplay = getPrimaryDisplay(relatedRow[display]) + } row[relationship.column][key] = { primaryDisplay: relatedDisplay || "Invalid display column", _id: relatedRow._id, diff --git a/packages/server/src/integrations/utils.ts b/packages/server/src/integrations/utils.ts index de8b318bb1..5ff9f07b89 100644 --- a/packages/server/src/integrations/utils.ts +++ b/packages/server/src/integrations/utils.ts @@ -328,3 +328,22 @@ export function finaliseExternalTables( .reduce((r, [k, v]) => ({ ...r, [k]: v }), {}) return { tables: finalTables, errors } } + +/** + * Checks if the provided input is an object, but specifically not a date type object. + * Used during coercion of types and relationship handling, dates are considered valid + * and can be used as a display field, but objects and arrays cannot. + * @param test + */ +export function getPrimaryDisplay(test: any): string | undefined { + if (test instanceof Date) { + return test.toISOString() + } + if (Array.isArray(test) && test[0] && typeof test[0] !== "object") { + return test.join(", ") + } + if (typeof test === "object") { + return undefined + } + return test as string +}