Skip to content

Commit c696ce2

Browse files
committed
Disallow duplicate keys in JTL templates
1 parent 23cae42 commit c696ce2

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/JsonReaderTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.apache.logging.log4j.core.util.Integers;
2525
import org.assertj.core.api.Assertions;
2626
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.params.ParameterizedTest;
28+
import org.junit.jupiter.params.provider.CsvSource;
2729

2830
class JsonReaderTest {
2931

@@ -234,6 +236,7 @@ void test_invalid_array_end_3() {
234236
void test_valid_objects() {
235237
test("{}", Collections.emptyMap());
236238
test("{\"foo\":\"bar\"}", Collections.singletonMap("foo", "bar"));
239+
test("{\"x\":{\"x\": \"x\"}}", Collections.singletonMap("x", Collections.singletonMap("x", "x")));
237240
}
238241

239242
@Test
@@ -299,6 +302,21 @@ void test_invalid_object_key() {
299302
.hasMessage("was expecting an object key at index 13: ARRAY_END");
300303
}
301304

305+
@ParameterizedTest
306+
@CsvSource(
307+
value = {
308+
"a|7|{\"a\":1,\"a\":2}",
309+
"a|13|{\"a\":1,\"b\":2,\"a\":3}",
310+
"c|24|{\"a\":1,\"b\":{\"c\":2,\"d\":3,\"c\":4}}"
311+
},
312+
delimiter = '|')
313+
void test_conflicting_object_key_1(final String key, final int index, final String json) {
314+
Assertions.assertThatThrownBy(() -> JsonReader.read(json))
315+
.as("key=`%s`, index=%d, json=`%s`", key, index, json)
316+
.isInstanceOf(IllegalArgumentException.class)
317+
.hasMessage("found duplicate object key at index %d: %s", index, key);
318+
}
319+
302320
@Test
303321
@SuppressWarnings("DoubleBraceInitialization")
304322
public void test_nesting() {

log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/JsonReader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ private Map<String, Object> readObject() {
282282
final String message = String.format(
283283
"was expecting an object key at index %d: %s", readTokenStartIndex, readToken);
284284
throw new IllegalArgumentException(message);
285+
} else if (object.containsKey(key)) {
286+
final String message =
287+
String.format("found duplicate object key at index %d: %s", readTokenStartIndex, key);
288+
throw new IllegalArgumentException(message);
285289
}
286290
} else {
287291
expectDelimiter(Delimiter.OBJECT_END, readToken);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="changed">
6+
<description format="asciidoc">Disallow duplicate keys in JSON Template Layout templates</description>
7+
</entry>

0 commit comments

Comments
 (0)