Skip to content

Commit 1feb869

Browse files
opensearch-trigger-bot[bot]github-actions[bot]opensearch-changeset-bot[bot]
authored
[Bugfix] Make UI setting client more robust when the setting key does not exist (#9927) (#9945)
* feat: add settings * Changeset file for PR #9927 created/updated * fix: bootstrap --------- (cherry picked from commit 935758f) Signed-off-by: SuZhou-Joe <suzhou@amazon.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
1 parent d525a1c commit 1feb869

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

changelogs/fragments/9927.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fix:
2+
- Make UI setting client more robust when the setting key not exists ([#9927](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/9927))

src/core/public/ui_settings/ui_settings_client.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,12 @@ describe('#set', () => {
261261
await client.set('defaultIndex', 'index', UiSettingScope.USER);
262262
expect(getAll).toHaveBeenCalled();
263263
});
264+
265+
it('should not throw error if the key does not exist', async () => {
266+
const { client } = setup();
267+
268+
await expect(client.set('not_exist', UiSettingScope.GLOBAL)).resolves.not.toThrowError();
269+
});
264270
});
265271

266272
describe('#getUserProvidedWithScope', () => {
@@ -287,6 +293,13 @@ describe('#getUserProvidedWithScope', () => {
287293
client.getUserProvidedWithScope('defaultDatasource', UiSettingScope.WORKSPACE)
288294
).resolves.toBe('ds');
289295
});
296+
297+
it('should not throw error if the key does not exist', async () => {
298+
const { client } = setup();
299+
const notExistSettingKey = 'not_exist';
300+
301+
await client.getUserProvidedWithScope(notExistSettingKey, UiSettingScope.GLOBAL);
302+
});
290303
});
291304

292305
describe('#remove', () => {
@@ -322,6 +335,11 @@ describe('#remove', () => {
322335
client.remove('defaultDatasource', UiSettingScope.USER)
323336
).rejects.toThrowErrorMatchingSnapshot();
324337
});
338+
it('should not throw error if the key does not exist', () => {
339+
const { client } = setup();
340+
341+
expect(() => client.remove('not_exist')).not.toThrowError();
342+
});
325343
});
326344

327345
describe('#isDeclared', () => {

src/core/public/ui_settings/ui_settings_client.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ You can use \`IUiSettingsClient.get("${key}", defaultValue)\`, which will just r
141141
}
142142

143143
private validateScope(key: string, scope: UiSettingScope) {
144-
const definition = this.cache[key];
144+
const definition = this.cache[key] || {};
145145
const validScopes = Array.isArray(definition.scope)
146146
? definition.scope
147147
: [definition.scope || UiSettingScope.GLOBAL];
@@ -157,7 +157,7 @@ You can use \`IUiSettingsClient.get("${key}", defaultValue)\`, which will just r
157157
.getAll()
158158
.then((response: any) => {
159159
const value = response.settings[key]?.userValue;
160-
const type = this.cache[key].type;
160+
const type = this.cache[key]?.type;
161161
return this.resolveValue(value, type);
162162
});
163163
}
@@ -277,8 +277,7 @@ You can use \`IUiSettingsClient.get("${key}", defaultValue)\`, which will just r
277277
key: string,
278278
defaults: Record<string, any>,
279279
enableUserControl: boolean,
280-
settings: Record<string, any> = {},
281-
scope: UiSettingScope | undefined
280+
settings: Record<string, any> = {}
282281
) {
283282
const hasMultipleScopes =
284283
Array.isArray(this.cache[key]?.scope) && (this.cache[key].scope?.length ?? 0) > 1;
@@ -327,10 +326,10 @@ You can use \`IUiSettingsClient.get("${key}", defaultValue)\`, which will just r
327326
? this.setBrowserStoredSettings(key, newVal)
328327
: (await this.selectedApi(scope).batchSet(key, newVal)) || {};
329328

330-
this.mergeSettingsIntoCache(key, defaults, true, settings, scope);
329+
this.mergeSettingsIntoCache(key, defaults, true, settings);
331330
} else {
332331
const { settings } = (await this.selectedApi(scope).batchSet(key, newVal)) || {};
333-
this.mergeSettingsIntoCache(key, defaults, false, settings, scope);
332+
this.mergeSettingsIntoCache(key, defaults, false, settings);
334333
}
335334
this.saved$.next({ key, newValue: newVal, oldValue: initialVal });
336335
return true;

0 commit comments

Comments
 (0)