From c8596c1a5bbd0565486959a4ceb6ea24ef500d81 Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Thu, 21 Mar 2024 10:49:56 +0800 Subject: [PATCH 1/6] fix: add base path when parse url in http service Signed-off-by: SuZhou-Joe --- src/core/public/http/http_service.ts | 2 +- src/core/utils/workspace.test.ts | 4 ++-- src/core/utils/workspace.ts | 2 +- src/plugins/workspace/public/plugin.ts | 3 +-- src/plugins/workspace/server/plugin.ts | 5 ++++- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/core/public/http/http_service.ts b/src/core/public/http/http_service.ts index 6832703c7925..c1a538dfca1c 100644 --- a/src/core/public/http/http_service.ts +++ b/src/core/public/http/http_service.ts @@ -53,7 +53,7 @@ export class HttpService implements CoreService { public setup({ injectedMetadata, fatalErrors }: HttpDeps): HttpSetup { const opensearchDashboardsVersion = injectedMetadata.getOpenSearchDashboardsVersion(); let clientBasePath = ''; - const workspaceId = getWorkspaceIdFromUrl(window.location.href); + const workspaceId = getWorkspaceIdFromUrl(window.location.href, injectedMetadata.getBasePath()); if (workspaceId) { clientBasePath = `${WORKSPACE_PATH_PREFIX}/${workspaceId}`; } diff --git a/src/core/utils/workspace.test.ts b/src/core/utils/workspace.test.ts index a852ddcc5190..722bbca566e5 100644 --- a/src/core/utils/workspace.test.ts +++ b/src/core/utils/workspace.test.ts @@ -8,11 +8,11 @@ import { httpServiceMock } from '../public/mocks'; describe('#getWorkspaceIdFromUrl', () => { it('return workspace when there is a match', () => { - expect(getWorkspaceIdFromUrl('http://localhost/w/foo')).toEqual('foo'); + expect(getWorkspaceIdFromUrl('http://localhost/w/foo', '')).toEqual('foo'); }); it('return empty when there is not a match', () => { - expect(getWorkspaceIdFromUrl('http://localhost/w2/foo')).toEqual(''); + expect(getWorkspaceIdFromUrl('http://localhost/w2/foo', '')).toEqual(''); }); it('return workspace when there is a match with basePath provided', () => { diff --git a/src/core/utils/workspace.ts b/src/core/utils/workspace.ts index c383967483a8..f16b28423b66 100644 --- a/src/core/utils/workspace.ts +++ b/src/core/utils/workspace.ts @@ -6,7 +6,7 @@ import { WORKSPACE_PATH_PREFIX } from './constants'; import { IBasePath } from '../public'; -export const getWorkspaceIdFromUrl = (url: string, basePath?: string): string => { +export const getWorkspaceIdFromUrl = (url: string, basePath: string): string => { const regexp = new RegExp(`^${basePath || ''}\/w\/([^\/]*)`); const urlObject = new URL(url); const matchedResult = urlObject.pathname.match(regexp); diff --git a/src/plugins/workspace/public/plugin.ts b/src/plugins/workspace/public/plugin.ts index 8a5bf9b125f5..f50eccc6f226 100644 --- a/src/plugins/workspace/public/plugin.ts +++ b/src/plugins/workspace/public/plugin.ts @@ -32,8 +32,7 @@ export class WorkspacePlugin implements Plugin<{}, {}, {}> { }); } } - - private getWorkspaceIdFromURL(basePath?: string): string | null { + private getWorkspaceIdFromURL(basePath: string): string | null { return getWorkspaceIdFromUrl(window.location.href, basePath); } diff --git a/src/plugins/workspace/server/plugin.ts b/src/plugins/workspace/server/plugin.ts index f5b7da6430e0..8fc725b05102 100644 --- a/src/plugins/workspace/server/plugin.ts +++ b/src/plugins/workspace/server/plugin.ts @@ -27,7 +27,10 @@ export class WorkspacePlugin implements Plugin { - const workspaceId = getWorkspaceIdFromUrl(request.url.toString()); + const workspaceId = getWorkspaceIdFromUrl( + request.url.toString(), + '' // No need to pass basePath here because BasePathService will rewrite the url. + ); if (workspaceId) { const requestUrl = new URL(request.url.toString()); From e1fa23f20dadf38c2d50d8164d829cf73b056e11 Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Thu, 21 Mar 2024 10:57:45 +0800 Subject: [PATCH 2/6] feat: add CHANGELOG Signed-off-by: SuZhou-Joe --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cea2c205910..4068357b7701 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [BUG][MD]Expose picker using function in data source management plugin setup([#6030](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6030)) - [BUG][Multiple Datasource] Fix data source filter bug and add tests ([#6152](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6152)) - [BUG][Multiple Datasource] Fix obsolete snapshots for test within data source management plugin ([#6185](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6185)) +- [Workspace] Add base path when parse url in http service ([#6233](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6233)) ### 🚞 Infrastructure From d64f919ffcbb52b640b290bdf24bc1382f802265 Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Thu, 21 Mar 2024 11:06:14 +0800 Subject: [PATCH 3/6] feat: optimize unit test cases for parse clientBasePath from url when basePath enabled Signed-off-by: SuZhou-Joe --- src/core/public/http/http_service.test.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/core/public/http/http_service.test.ts b/src/core/public/http/http_service.test.ts index 5671064e4c52..73010e1c5a93 100644 --- a/src/core/public/http/http_service.test.ts +++ b/src/core/public/http/http_service.test.ts @@ -83,21 +83,22 @@ describe('#setup()', () => { expect(setupResult.basePath.get()).toEqual(''); }); - it('setup basePath with workspaceId provided in window.location.href', () => { + it('setup basePath with workspaceId provided in window.location.href and basePath present in injectedMetadata', () => { const windowSpy = jest.spyOn(window, 'window', 'get'); windowSpy.mockImplementation( () => ({ location: { - href: 'http://localhost/w/workspaceId/app', + href: 'http://localhost/base_path/w/workspaceId/app', }, } as any) ); const injectedMetadata = injectedMetadataServiceMock.createSetupContract(); + injectedMetadata.getBasePath.mockReturnValue('/base_path'); const fatalErrors = fatalErrorsServiceMock.createSetupContract(); const httpService = new HttpService(); const setupResult = httpService.setup({ fatalErrors, injectedMetadata }); - expect(setupResult.basePath.get()).toEqual('/w/workspaceId'); + expect(setupResult.basePath.get()).toEqual('/base_path/w/workspaceId'); windowSpy.mockRestore(); }); }); From eff9b95fd8b135cec62abb084b7d00942268440d Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Thu, 21 Mar 2024 11:07:10 +0800 Subject: [PATCH 4/6] feat: add empty line before getWorkspaceIdFromURL method Signed-off-by: SuZhou-Joe --- src/plugins/workspace/public/plugin.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/plugins/workspace/public/plugin.ts b/src/plugins/workspace/public/plugin.ts index f50eccc6f226..2d700549591f 100644 --- a/src/plugins/workspace/public/plugin.ts +++ b/src/plugins/workspace/public/plugin.ts @@ -32,6 +32,7 @@ export class WorkspacePlugin implements Plugin<{}, {}, {}> { }); } } + private getWorkspaceIdFromURL(basePath: string): string | null { return getWorkspaceIdFromUrl(window.location.href, basePath); } From b3a28d0d154b8457be81736a1cfe44f2fabc618c Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Thu, 21 Mar 2024 11:10:02 +0800 Subject: [PATCH 5/6] feat: optimize comment Signed-off-by: SuZhou-Joe --- src/plugins/workspace/server/plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/workspace/server/plugin.ts b/src/plugins/workspace/server/plugin.ts index 8fc725b05102..e846470210c3 100644 --- a/src/plugins/workspace/server/plugin.ts +++ b/src/plugins/workspace/server/plugin.ts @@ -29,7 +29,7 @@ export class WorkspacePlugin implements Plugin { const workspaceId = getWorkspaceIdFromUrl( request.url.toString(), - '' // No need to pass basePath here because BasePathService will rewrite the url. + '' // No need to pass basePath here because the request.url will be rewrite by registerOnPreRouting method in `src/core/server/http/http_server.ts` ); if (workspaceId) { From 1fe890d00b95cbe7f93731ec8cc53dc7baa06ca2 Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Thu, 21 Mar 2024 13:50:53 +0800 Subject: [PATCH 6/6] feat: optimize code Signed-off-by: SuZhou-Joe --- src/plugins/workspace/public/plugin.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/plugins/workspace/public/plugin.ts b/src/plugins/workspace/public/plugin.ts index 2d700549591f..f0c82bda90b7 100644 --- a/src/plugins/workspace/public/plugin.ts +++ b/src/plugins/workspace/public/plugin.ts @@ -33,10 +33,6 @@ export class WorkspacePlugin implements Plugin<{}, {}, {}> { } } - private getWorkspaceIdFromURL(basePath: string): string | null { - return getWorkspaceIdFromUrl(window.location.href, basePath); - } - public async setup(core: CoreSetup) { const workspaceClient = new WorkspaceClient(core.http, core.workspaces); await workspaceClient.init(); @@ -44,7 +40,10 @@ export class WorkspacePlugin implements Plugin<{}, {}, {}> { /** * Retrieve workspace id from url */ - const workspaceId = this.getWorkspaceIdFromURL(core.http.basePath.getBasePath()); + const workspaceId = getWorkspaceIdFromUrl( + window.location.href, + core.http.basePath.getBasePath() + ); if (workspaceId) { const result = await workspaceClient.enterWorkspace(workspaceId);