Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,17 @@ private void addOrReplace(MutablePropertySources propertySources, Map<String, Ob
if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME);
if (source instanceof MapPropertySource) {
target = (MapPropertySource) source;
// The existing backing map may be immutable, so copy into a mutable map and replace the
// source instead of mutating it in place. See dubbo#16268.
Map<String, Object> merged = new HashMap<>(((MapPropertySource) source).getSource());
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
if (!target.containsProperty(key)) {
target.getSource().put(key, entry.getValue());
if (!merged.containsKey(key)) {
merged.put(key, entry.getValue());
}
}
target = new MapPropertySource(PROPERTY_SOURCE_NAME, merged);
propertySources.replace(PROPERTY_SOURCE_NAME, target);
}
}
if (target == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package org.apache.dubbo.spring.boot.env;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication;
Expand All @@ -26,6 +28,7 @@
import org.springframework.core.env.PropertySource;
import org.springframework.mock.env.MockEnvironment;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

Expand Down Expand Up @@ -106,4 +109,35 @@ void testPostProcessEnvironment() {
assertNotNull(defaultPropertySource);
assertEquals("virtual", defaultPropertySource.getProperty("dubbo.protocol.threadpool"));
}

/** #16268: addOrReplace must not fail when "defaultProperties" is backed by an immutable map. */
@Test
void testPostProcessEnvironmentOnImmutableDefaultProperties() {
MockEnvironment environment = new MockEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
propertySources.addLast(
new MapPropertySource("defaultProperties", Collections.unmodifiableMap(new HashMap<String, Object>())));

instance.postProcessEnvironment(environment, springApplication);

PropertySource<?> defaultPropertySource = propertySources.get("defaultProperties");
assertNotNull(defaultPropertySource);
assertEquals("true", defaultPropertySource.getProperty("dubbo.config.multiple"));
}

/** #16268: on an immutable "defaultProperties", Dubbo's default must not override an existing key. */
@Test
void testImmutableDefaultPropertiesDoesNotOverrideExistingKey() {
MockEnvironment environment = new MockEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
Map<String, Object> existing = new HashMap<>();
existing.put("dubbo.config.multiple", "false");
propertySources.addLast(new MapPropertySource("defaultProperties", Collections.unmodifiableMap(existing)));

assertDoesNotThrow(() -> instance.postProcessEnvironment(environment, springApplication));

PropertySource<?> defaultPropertySource = propertySources.get("defaultProperties");
assertNotNull(defaultPropertySource);
assertEquals("false", defaultPropertySource.getProperty("dubbo.config.multiple"));
}
}
Loading