Skip to content

Commit

Permalink
Ignore Properties that cannot be represented by a String (#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Jan 24, 2023
1 parent 13e1602 commit dd32f7e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,22 @@ private ConfigSourceUtil() {
public static Map<String, String> propertiesToMap(Properties properties) {
Map<String, String> map = new HashMap<>();
synchronized (properties) {
for (Map.Entry<Object, Object> e : properties.entrySet()) {
map.put(String.valueOf(e.getKey()), String.valueOf(e.getValue()));
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
String key;
try {
key = String.valueOf(entry.getKey());
} catch (Exception e) {
continue;
}

String value;
try {
value = String.valueOf(entry.getValue());
} catch (Exception e) {
continue;
}

map.put(key, value);
}
}
return map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.smallrye.config.common.utils;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Map;
import java.util.Properties;
Expand All @@ -35,4 +36,20 @@ void propertiesToMap() {
assertEquals("my.value2", map.get("my.key2"));
assertEquals("2", map.get("my.key3"));
}

@Test
void unableToConvertToString() {
Properties properties = new Properties();
properties.put("foo.bar", new UnconvertableString());

Map<String, String> map = ConfigSourceUtil.propertiesToMap(properties);
assertTrue(map.isEmpty());
}

private static class UnconvertableString {
@Override
public String toString() {
throw new UnsupportedOperationException();
}
}
}

0 comments on commit dd32f7e

Please sign in to comment.