From 8dfb44e6d0971cb5daf32da157b74388e354bbd7 Mon Sep 17 00:00:00 2001 From: Hailong Cui Date: Wed, 12 Apr 2023 11:03:30 +0800 Subject: [PATCH] Fix command with query parameter rejected in DevTools (#3813) Signed-off-by: Hailong Cui Signed-off-by: David Sinclair --- CHANGELOG.md | 1 + .../server/routes/api/console/proxy/create_handler.ts | 8 +++++--- .../routes/api/console/proxy/tests/query_string.test.ts | 8 ++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 029df22507a4..8263f9380e25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -226,6 +226,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [BUG] Fix suggestion list cutoff issue ([#2607](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2607)) - [TSVB] Fixes undefined serial diff aggregation documentation link ([#3503](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3503)) - [Console] Fix dev tool console autocomplete not loading issue ([#3775](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3775)) +- [Console] Fix dev tool console run command with query parameter error ([#3813](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3813)) ### 🚞 Infrastructure diff --git a/src/plugins/console/server/routes/api/console/proxy/create_handler.ts b/src/plugins/console/server/routes/api/console/proxy/create_handler.ts index f46acbf6cb71..0a401ded813b 100644 --- a/src/plugins/console/server/routes/api/console/proxy/create_handler.ts +++ b/src/plugins/console/server/routes/api/console/proxy/create_handler.ts @@ -70,13 +70,15 @@ function getProxyHeaders(req: OpenSearchDashboardsRequest) { } function toUrlPath(path: string) { - let urlPath = `/${trimStart(path, '/')}`; + const FAKE_BASE = 'http://localhost'; + const urlWithFakeBase = new URL(`${FAKE_BASE}/${trimStart(path, '/')}`); // Appending pretty here to have OpenSearch do the JSON formatting, as doing // in JS can lead to data loss (7.0 will get munged into 7, thus losing indication of // measurement precision) - if (!urlPath.includes('?pretty')) { - urlPath += '?pretty=true'; + if (!urlWithFakeBase.searchParams.get('pretty')) { + urlWithFakeBase.searchParams.append('pretty', 'true'); } + const urlPath = urlWithFakeBase.href.replace(urlWithFakeBase.origin, ''); return urlPath; } diff --git a/src/plugins/console/server/routes/api/console/proxy/tests/query_string.test.ts b/src/plugins/console/server/routes/api/console/proxy/tests/query_string.test.ts index 105bce3bfad2..9aefea1182cc 100644 --- a/src/plugins/console/server/routes/api/console/proxy/tests/query_string.test.ts +++ b/src/plugins/console/server/routes/api/console/proxy/tests/query_string.test.ts @@ -82,6 +82,14 @@ describe('Console Proxy Route', () => { expect(args.path).toBe('/index/id?pretty=true'); }); }); + + describe(`contains query parameter`, () => { + it('adds slash to path before sending request', async () => { + await request('GET', '_cat/tasks?v'); + const [[args]] = opensearchClient.asCurrentUser.transport.request.mock.calls; + expect(args.path).toBe('/_cat/tasks?v=&pretty=true'); + }); + }); }); }); });