Skip to content

Commit

Permalink
[Workspace]Fix maximum call stack error in use case service (opensear…
Browse files Browse the repository at this point in the history
…ch-project#7817)

* Fix maximum call stack error in UseCaseService

Signed-off-by: Lin Wang <wonglam@amazon.com>

* Changeset file for PR opensearch-project#7817 created/updated

* Improve UT coverage

Signed-off-by: Lin Wang <wonglam@amazon.com>

* Refactor compare with sort

Signed-off-by: Lin Wang <wonglam@amazon.com>

* Refactor compareFeatures

Signed-off-by: Lin Wang <wonglam@amazon.com>

* Add ut for multi same features

Signed-off-by: Lin Wang <wonglam@amazon.com>

---------

Signed-off-by: Lin Wang <wonglam@amazon.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
wanglam and opensearch-changeset-bot[bot] authored Aug 24, 2024
1 parent 42ded5c commit 452b65d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/7817.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- [Workspace] maximum call stack error in use case service ([#7817](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7817))
7 changes: 6 additions & 1 deletion src/plugins/workspace/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ export type Services = CoreStart & {
navigationUI?: NavigationPublicPluginStart['ui'];
};

export interface WorkspaceUseCaseFeature {
id: string;
title?: string;
}

export interface WorkspaceUseCase {
id: string;
title: string;
description: string;
features: Array<{ id: string; title?: string }>;
features: WorkspaceUseCaseFeature[];
systematic?: boolean;
order?: number;
}
32 changes: 32 additions & 0 deletions src/plugins/workspace/public/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,38 @@ describe('workspace utils: isEqualWorkspaceUseCase', () => {
})
).toEqual(false);
});
it('should return false for duplicate features', () => {
expect(
isEqualWorkspaceUseCase(
{ ...useCaseMock, features: [useCaseMock.features[0], useCaseMock.features[0]] },
{
...useCaseMock,
features: [
useCaseMock.features[0],
{
id: 'another',
title: 'Another',
},
],
}
)
).toEqual(false);
});
it('should return true for multi same features', () => {
const anotherFeature = {
id: 'another',
title: 'Another',
};
expect(
isEqualWorkspaceUseCase(
{ ...useCaseMock, features: [useCaseMock.features[0], anotherFeature] },
{
...useCaseMock,
features: [useCaseMock.features[0], anotherFeature],
}
)
).toEqual(true);
});
it('should return true when all properties equal', () => {
expect(
isEqualWorkspaceUseCase(useCaseMock, {
Expand Down
23 changes: 14 additions & 9 deletions src/plugins/workspace/public/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
WorkspaceAvailability,
} from '../../../core/public';
import { DEFAULT_SELECTED_FEATURES_IDS, WORKSPACE_DETAIL_APP_ID } from '../common/constants';
import { WorkspaceUseCase } from './types';
import { WorkspaceUseCase, WorkspaceUseCaseFeature } from './types';
import { formatUrlWithWorkspaceId } from '../../../core/public/utils';
import { SigV4ServiceName } from '../../../plugins/data_source/common/data_sources';

Expand Down Expand Up @@ -260,6 +260,18 @@ export const convertNavGroupToWorkspaceUseCase = ({
order,
});

const compareFeatures = (
features1: WorkspaceUseCaseFeature[],
features2: WorkspaceUseCaseFeature[]
) => {
const featuresSerializer = (features: WorkspaceUseCaseFeature[]) =>
features
.map(({ id, title }) => `${id}-${title}`)
.sort()
.join();
return featuresSerializer(features1) === featuresSerializer(features2);
};

export const isEqualWorkspaceUseCase = (a: WorkspaceUseCase, b: WorkspaceUseCase) => {
if (a.id !== b.id) {
return false;
Expand All @@ -276,14 +288,7 @@ export const isEqualWorkspaceUseCase = (a: WorkspaceUseCase, b: WorkspaceUseCase
if (a.order !== b.order) {
return false;
}
if (
a.features.length !== b.features.length ||
a.features.some((aFeature) =>
b.features.some(
(bFeature) => aFeature.id !== bFeature.id || aFeature.title !== bFeature.title
)
)
) {
if (a.features.length !== b.features.length || !compareFeatures(a.features, b.features)) {
return false;
}
return true;
Expand Down

0 comments on commit 452b65d

Please sign in to comment.