This is an automated email from the ASF dual-hosted git repository.
vy pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/2.x by this push:
new c696ce20a3 Disallow duplicate keys in JTL templates
c696ce20a3 is described below
commit c696ce20a3720eefa813f847cc2a6ce124fe3167
Author: Volkan Yazıcı <[email protected]>
AuthorDate: Sat Aug 31 17:29:25 2024 +0200
Disallow duplicate keys in JTL templates
---
.../layout/template/json/util/JsonReaderTest.java | 18 ++++++++++++++++++
.../log4j/layout/template/json/util/JsonReader.java | 4 ++++
src/changelog/.2.x.x/change_JsonReader_dup_key.xml | 7 +++++++
3 files changed, 29 insertions(+)
diff --git
a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/JsonReaderTest.java
b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/JsonReaderTest.java
index b5bcf33715..2c0c598b7e 100644
---
a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/JsonReaderTest.java
+++
b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/JsonReaderTest.java
@@ -24,6 +24,8 @@ import java.util.LinkedHashMap;
import org.apache.logging.log4j.core.util.Integers;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
class JsonReaderTest {
@@ -234,6 +236,7 @@ class JsonReaderTest {
void test_valid_objects() {
test("{}", Collections.emptyMap());
test("{\"foo\":\"bar\"}", Collections.singletonMap("foo", "bar"));
+ test("{\"x\":{\"x\": \"x\"}}", Collections.singletonMap("x",
Collections.singletonMap("x", "x")));
}
@Test
@@ -299,6 +302,21 @@ class JsonReaderTest {
.hasMessage("was expecting an object key at index 13:
ARRAY_END");
}
+ @ParameterizedTest
+ @CsvSource(
+ value = {
+ "a|7|{\"a\":1,\"a\":2}",
+ "a|13|{\"a\":1,\"b\":2,\"a\":3}",
+ "c|24|{\"a\":1,\"b\":{\"c\":2,\"d\":3,\"c\":4}}"
+ },
+ delimiter = '|')
+ void test_conflicting_object_key_1(final String key, final int index,
final String json) {
+ Assertions.assertThatThrownBy(() -> JsonReader.read(json))
+ .as("key=`%s`, index=%d, json=`%s`", key, index, json)
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("found duplicate object key at index %d: %s",
index, key);
+ }
+
@Test
@SuppressWarnings("DoubleBraceInitialization")
public void test_nesting() {
diff --git
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/JsonReader.java
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/JsonReader.java
index 0537457871..0faf514744 100644
---
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/JsonReader.java
+++
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/JsonReader.java
@@ -282,6 +282,10 @@ public final class JsonReader {
final String message = String.format(
"was expecting an object key at index %d: %s",
readTokenStartIndex, readToken);
throw new IllegalArgumentException(message);
+ } else if (object.containsKey(key)) {
+ final String message =
+ String.format("found duplicate object key at
index %d: %s", readTokenStartIndex, key);
+ throw new IllegalArgumentException(message);
}
} else {
expectDelimiter(Delimiter.OBJECT_END, readToken);
diff --git a/src/changelog/.2.x.x/change_JsonReader_dup_key.xml
b/src/changelog/.2.x.x/change_JsonReader_dup_key.xml
new file mode 100644
index 0000000000..7059186603
--- /dev/null
+++ b/src/changelog/.2.x.x/change_JsonReader_dup_key.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns="https://logging.apache.org/xml/ns"
+ xsi:schemaLocation="https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
+ type="changed">
+ <description format="asciidoc">Disallow duplicate keys in JSON Template
Layout templates</description>
+</entry>