Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions libbeat/reader/readjson/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@
"b": float64(-1),
},
},
{
Name: "Key collision",
Input: `{"log.level":"info","log":{"source":"connectors-py-default"},"log":{"logger":"agent_component.cli"}}`,
Output: map[string]interface{}{
"log.level": "info",
"log": map[string]interface{}{
"logger": "agent_component.cli",
},
},
},
}

for _, test := range tests {
Expand Down Expand Up @@ -197,7 +207,7 @@
}

func TestMergeJSONFields(t *testing.T) {
type io struct {

Check failure on line 210 in libbeat/reader/readjson/json_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

type io is unused (unused)
}

text := "hello"
Expand Down Expand Up @@ -351,13 +361,58 @@
JSONConfig: Config{ExpandKeys: true, KeysUnderRoot: true},
ExpectedItems: mapstr.M{"a": mapstr.M{"b": mapstr.M{"c": "c", "d": "d"}}},
},
"key collision with expanded keys": {
Data: mapstr.M{
"log.level": "info",
"log": mapstr.M{
"logger": "agent_component.cli",
},
},
Copy link

@straistaru straistaru Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the input Data have to be something like the case above with 2 "log" keys (which is not possible in a mapstr.M)? Here we have "log" and "log.level" keys.

{
    "log.level": "info",
    "log": {
        "source": "connectors-py-default"
    },
    "log": {
        "logger": "agent_component.cli"
    }
}

So, I am not sure these test the actual test case we want to test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@straistaru when we expand (de-dot) keys we also face a collision. This is to demonstrate the current behavior of what happens in this case.

JSONConfig: Config{ExpandKeys: true},
ExpectedItems: mapstr.M{
"log.level": "info",
"log": mapstr.M{
"logger": "agent_component.cli",
},
},
},
"key collision without expanded keys": {
Data: mapstr.M{
"log.level": "info",
"log": mapstr.M{
"logger": "agent_component.cli",
},
},
JSONConfig: Config{ExpandKeys: false},
ExpectedItems: mapstr.M{
"log.level": "info",
"log": mapstr.M{
"logger": "agent_component.cli",
},
},
},
"key collision with overwrite": {
Data: mapstr.M{
"log.level": "info",
"log": mapstr.M{
"logger": "agent_component.cli",
},
},
JSONConfig: Config{OverwriteKeys: true, AddErrorKey: true, IgnoreDecodingError: true},
ExpectedItems: mapstr.M{
"log.level": "info",
"log": mapstr.M{
"logger": "agent_component.cli",
},
},
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
var jsonFields mapstr.M
if fields, ok := test.Data["json"]; ok {
jsonFields = fields.(mapstr.M)

Check failure on line 415 in libbeat/reader/readjson/json_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Error return value is not checked (errcheck)
}

id, ts := MergeJSONFields(test.Data, jsonFields, test.Text, test.JSONConfig)
Expand Down
Loading