Skip to content

Commit

Permalink
check duplicate keys (#1270)
Browse files Browse the repository at this point in the history
* check duplicate keys (properties) #1269
  • Loading branch information
fugerit79 authored Dec 20, 2024
1 parent 220be74 commit 7d66a3a
Show file tree
Hide file tree
Showing 3 changed files with 29 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) {
ConfigLogging.log.duplicateValue(oldConfigValue.getName(), oldConfigValue.getConfigSourceName(),
oldConfigValue.getValue());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ public interface ConfigLogging extends BasicLogger {
@LogMessage(level = Logger.Level.DEBUG)
@Message(id = 1006, value = "Loaded ConfigSource %s with ordinal %d")
void loadedConfigSource(String name, int ordinal);

@LogMessage(level = Logger.Level.WARN)
@Message(id = 1007, value = "Duplicate value found for name : %s, config source name : %s, (old value : %s)")
void duplicateValue(String key, String sourceName, String oldValue);

}
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 logDuplicateValue() 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 7d66a3a

Please sign in to comment.