Skip to content

Commit b28c8b9

Browse files
preferences: fix PreferenceProvider.merge method (#12126)
Merge properties of type array when merging preferences objects.
1 parent 6091474 commit b28c8b9

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// *****************************************************************************
2+
// Copyright (C) 2023 EclipseSource and others.
3+
//
4+
// This program and the accompanying materials are made available under the
5+
// terms of the Eclipse Public License v. 2.0 which is available at
6+
// http://www.eclipse.org/legal/epl-2.0.
7+
//
8+
// This Source Code may also be made available under the following Secondary
9+
// Licenses when the conditions for such availability set forth in the Eclipse
10+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
11+
// with the GNU Classpath Exception which is available at
12+
// https://www.gnu.org/software/classpath/license.html.
13+
//
14+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
// *****************************************************************************
16+
17+
import { PreferenceProvider } from './preference-provider';
18+
const { expect } = require('chai');
19+
20+
describe('PreferenceProvider', () => {
21+
it('should preserve extra source fields on merge', () => {
22+
const result = PreferenceProvider.merge({ 'configurations': [], 'compounds': [] }, { 'configurations': [] });
23+
expect(result).deep.equals({ 'configurations': [], 'compounds': [] });
24+
});
25+
it('should preserve extra target fields on merge', () => {
26+
const result = PreferenceProvider.merge({ 'configurations': [] }, { 'configurations': [], 'compounds': [] });
27+
expect(result).deep.equals({ 'configurations': [], 'compounds': [] });
28+
});
29+
it('should merge array values', () => {
30+
const result = PreferenceProvider.merge(
31+
{ 'configurations': [{ 'name': 'test1', 'request': 'launch' }], 'compounds': [] },
32+
{ 'configurations': [{ 'name': 'test2' }] }
33+
);
34+
expect(result).deep.equals({ 'configurations': [{ 'name': 'test1', 'request': 'launch' }, { 'name': 'test2' }], 'compounds': [] });
35+
});
36+
});

packages/core/src/browser/preferences/preference-provider.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ export abstract class PreferenceProvider implements Disposable {
235235
if (JSONExt.isObject(source[key]) && JSONExt.isObject(value)) {
236236
this.merge(source[key], value);
237237
continue;
238+
} else if (JSONExt.isArray(source[key]) && JSONExt.isArray(value)) {
239+
source[key] = [...JSONExt.deepCopy(source[key] as any), ...JSONExt.deepCopy(value)];
240+
continue;
238241
}
239242
}
240243
source[key] = JSONExt.deepCopy(value);

0 commit comments

Comments
 (0)