From 7f69dc4b96461f8f6ef813af3643a0d238dde11c Mon Sep 17 00:00:00 2001 From: Simeon Widdis Date: Mon, 4 Nov 2024 09:53:40 -0800 Subject: [PATCH] Guard against invalid sorting fields in report generation (#458) Signed-off-by: Simeon Widdis --- server/routes/utils/dataReportHelpers.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/server/routes/utils/dataReportHelpers.ts b/server/routes/utils/dataReportHelpers.ts index 2be41887..fec40ab3 100644 --- a/server/routes/utils/dataReportHelpers.ts +++ b/server/routes/utils/dataReportHelpers.ts @@ -90,11 +90,21 @@ export const buildRequestBody = ( .query(esbBoolQuery) .version(true); - if (report._source.sorting.length > 0) { - const sortings: Sort[] = report._source.sorting.map((element: string[]) => { + let sorting: string[][] = report._source.sorting; + + // We expect a list of [field, order] pairs for sorting. In some migration paths, though it's not + // clear why, this list can get unnested in the case of one sort, [["field", "asc"]] becomes + // ["field", "asc"]. The true root cause remains a mystery, so we work around it. + // See: https://github.com/opensearch-project/dashboards-reporting/issues/371 + if (sorting.length > 0 && typeof sorting[0] === "string") { + sorting = [(sorting as unknown as string[])]; + } + + if (sorting.length > 0) { + const sorts: Sort[] = sorting.map((element: string[]) => { return esb.sort(element[0], element[1]); }); - esbSearchQuery.sorts(sortings); + esbSearchQuery.sorts(sorts); } // add selected fields to query