Skip to content

Commit

Permalink
[FLINK-17789][core] DelegatingConfiguration should remove prefix inst…
Browse files Browse the repository at this point in the history
…ead of add prefix in toMap

This closes apache#12905
  • Loading branch information
pyscala committed Jul 22, 2020
1 parent e6c1529 commit 3ebf0d9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,13 @@ public Configuration clone() {
@Override
public Map<String, String> toMap() {
Map<String, String> map = backingConfig.toMap();
Map<String, String> prefixed = new HashMap<>(map.size());
Map<String, String> prefixed = new HashMap<>();
for (Map.Entry<String, String> entry : map.entrySet()) {
prefixed.put(prefix + entry.getKey(), entry.getValue());
if (entry.getKey().startsWith(prefix)) {
String keyWithoutPrefix = entry.getKey().substring(prefix.length());
prefixed.put(keyWithoutPrefix, entry.getValue());
}
}

return prefixed;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -114,4 +116,25 @@ public void testDelegationConfigurationWithPrefix() {

assertTrue(keySet.isEmpty());
}

@Test
public void testDelegationConfigurationToMapConsistentWithAddAllToProperties() {
Configuration conf = new Configuration();
conf.setString("k0", "v0");
conf.setString("prefix.k1", "v1");
conf.setString("prefix.prefix.k2", "v2");
conf.setString("k3.prefix.prefix.k3", "v3");
DelegatingConfiguration dc = new DelegatingConfiguration(conf, "prefix.");
// Collect all properties
Properties properties = new Properties();
dc.addAllToProperties(properties);
// Convert the Map<String, String> object into a Properties object
Map<String, String> map = dc.toMap();
Properties mapProperties = new Properties();
for (Map.Entry<String, String> entry : map.entrySet()) {
mapProperties.put(entry.getKey(), entry.getValue());
}
// Verification
assertEquals(properties, mapProperties);
}
}

0 comments on commit 3ebf0d9

Please sign in to comment.