Skip to content

Commit

Permalink
check duplicate keys (properties) #1269
Browse files Browse the repository at this point in the history
Only for properties config source : duplicate keys found in te same config source will be logged as warning.
  • Loading branch information
fugerit79 committed Dec 16, 2024
1 parent 940c5a5 commit 1285338
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.invoke.MethodHandles;
import java.util.AbstractCollection;
import java.util.AbstractMap;
import java.util.AbstractSet;
Expand All @@ -14,7 +15,9 @@
import java.util.Set;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.jboss.logging.Logger;

import io.smallrye.config._private.ConfigLogging;
import io.smallrye.config._private.ConfigMessages;

/**
Expand Down Expand Up @@ -340,6 +343,9 @@ final class ConfigValueProperties extends HashMap<String, ConfigValue> {
private final String configSourceName;
private final int configSourceOrdinal;

private static ConfigLogging log = Logger.getMessageLogger(MethodHandles.lookup(), ConfigLogging.class,
"io.smallrye.config");

public ConfigValueProperties(final String configSourceName, final int configSourceOrdinal) {
this.configSourceName = configSourceName;
this.configSourceOrdinal = configSourceOrdinal;
Expand Down Expand Up @@ -401,14 +407,18 @@ private void load0(LineReader lr) throws IOException {
}
String key = loadConvert(lr.lineBuf, 0, keyLen, convtBuf);
String value = loadConvert(lr.lineBuf, valueStart, limit - valueStart, convtBuf);
put(key, ConfigValue.builder()
ConfigValue oldConfigValue = put(key, ConfigValue.builder()
.withName(key)
.withValue(value)
.withRawValue(value)
.withConfigSourceName(configSourceName)
.withConfigSourceOrdinal(configSourceOrdinal)
.withLineNumber(lr.lineNumber)
.build());
if (oldConfigValue != null) {
log.warnv("duplicate keys found for : {0}, source name : {1}", oldConfigValue.getName(),
oldConfigValue.getConfigSourceName());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,17 @@ void wrapValue() throws Exception {
assertEquals(2, map.get("key2").getLineNumber());
assertEquals(6, map.get("key3").getLineNumber());
}

@Test
void logDuplicateKeys() throws Exception {
ConfigValueProperties map = new ConfigValueProperties("config", 1);
String config = "key=value\n" +
"key2=value\n" +
"key2=value2\n";
map.load(new StringReader(config));

// property expected with the last value found
assertEquals("value2", map.get("key2").getValue());
}

}

0 comments on commit 1285338

Please sign in to comment.