From fd8929daaece594bf10316da7a192a1cdcd3d150 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 3 Mar 2025 14:29:17 +0000 Subject: [PATCH] Fix tests, reinstate old workaround. --- .../server/src/api/routes/tests/table.spec.ts | 5 +--- packages/server/src/utilities/schema.ts | 24 +++++++++++++++++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index 29b576d16a..6f790df9ad 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -1246,10 +1246,7 @@ if (descriptions.length) { }) describe.each([ - [ - RowExportFormat.CSV, - (val: any) => JSON.stringify(val).replace(/"/g, "'"), - ], + [RowExportFormat.CSV, (val: any) => JSON.stringify(val)], [RowExportFormat.JSON, (val: any) => val], ])("import validation (%s)", (_, userParser) => { const basicSchema: TableSchema = { diff --git a/packages/server/src/utilities/schema.ts b/packages/server/src/utilities/schema.ts index eca74e32b7..8309014a61 100644 --- a/packages/server/src/utilities/schema.ts +++ b/packages/server/src/utilities/schema.ts @@ -270,6 +270,26 @@ function parseJsonExport(value: any) { if (typeof value !== "string") { return value } - const parsed = JSON.parse(value) - return parsed as T + + try { + const parsed = JSON.parse(value) + + return parsed as T + } catch (e: any) { + if ( + e.message.startsWith("Expected property name or '}' in JSON at position ") + ) { + // In order to store JSON within CSVs what we used to do is replace double + // quotes with single quotes. This results in invalid JSON, so the line + // below is a workaround to parse it. However, this method of storing JSON + // was never valid, and we don't do it anymore. However, people may have + // exported data and stored it, hoping to be able to restore it later, so + // we leave this in place to support that. + const parsed = JSON.parse(value.replace(/'/g, '"')) + return parsed as T + } + + // It is not valid JSON + throw e + } }