From 583e6ce49b3b51202a68931886cf1b9c648a4ad5 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Mon, 11 Nov 2024 12:08:30 -0800 Subject: [PATCH] Guard against invalid sorting fields in report generation (#458) (#471) (cherry picked from commit 7f69dc4b96461f8f6ef813af3643a0d238dde11c) Signed-off-by: Simeon Widdis Signed-off-by: github-actions[bot] Co-authored-by: github-actions[bot] --- 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 1d537d6a..1a64eaee 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