Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore Properties that cannot be represented by a String #880

Merged
merged 1 commit into from
Jan 24, 2023
Merged
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 @@ -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();
}
}
}