Skip to content

Commit 7eccae0

Browse files
authored
Add more tests for the ndjson parser behavior (#47774)
When parsing JSON objects with duplicated keys it was not clear how our parser behaves. These tests are to clarify the behavior which should help with the future debugging efforts.
1 parent 6db9d86 commit 7eccae0

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

libbeat/reader/readjson/json_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ func TestUnmarshal(t *testing.T) {
8989
"b": float64(-1),
9090
},
9191
},
92+
{
93+
Name: "Key collision",
94+
Input: `{"log.level":"info","log":{"source":"connectors-py-default"},"log":{"logger":"agent_component.cli"}}`,
95+
Output: map[string]interface{}{
96+
"log.level": "info",
97+
"log": map[string]interface{}{
98+
"logger": "agent_component.cli",
99+
},
100+
},
101+
},
92102
}
93103

94104
for _, test := range tests {
@@ -351,6 +361,51 @@ func TestMergeJSONFields(t *testing.T) {
351361
JSONConfig: Config{ExpandKeys: true, KeysUnderRoot: true},
352362
ExpectedItems: mapstr.M{"a": mapstr.M{"b": mapstr.M{"c": "c", "d": "d"}}},
353363
},
364+
"key collision with expanded keys": {
365+
Data: mapstr.M{
366+
"log.level": "info",
367+
"log": mapstr.M{
368+
"logger": "agent_component.cli",
369+
},
370+
},
371+
JSONConfig: Config{ExpandKeys: true},
372+
ExpectedItems: mapstr.M{
373+
"log.level": "info",
374+
"log": mapstr.M{
375+
"logger": "agent_component.cli",
376+
},
377+
},
378+
},
379+
"key collision without expanded keys": {
380+
Data: mapstr.M{
381+
"log.level": "info",
382+
"log": mapstr.M{
383+
"logger": "agent_component.cli",
384+
},
385+
},
386+
JSONConfig: Config{ExpandKeys: false},
387+
ExpectedItems: mapstr.M{
388+
"log.level": "info",
389+
"log": mapstr.M{
390+
"logger": "agent_component.cli",
391+
},
392+
},
393+
},
394+
"key collision with overwrite": {
395+
Data: mapstr.M{
396+
"log.level": "info",
397+
"log": mapstr.M{
398+
"logger": "agent_component.cli",
399+
},
400+
},
401+
JSONConfig: Config{OverwriteKeys: true, AddErrorKey: true, IgnoreDecodingError: true},
402+
ExpectedItems: mapstr.M{
403+
"log.level": "info",
404+
"log": mapstr.M{
405+
"logger": "agent_component.cli",
406+
},
407+
},
408+
},
354409
}
355410

356411
for name, test := range tests {

0 commit comments

Comments
 (0)