Skip to content

Commit

Permalink
x-pack/filebeat/input/httpjson: fix handling of null or empty arrays …
Browse files Browse the repository at this point in the history
…during split (#34322)

Co-authored-by: Dan Kortschak <dan@kortschak.io>
  • Loading branch information
efd6 and kortschak committed Jan 20, 2023
1 parent de99256 commit 8ac7cf9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ https://github.com/elastic/beats/compare/v8.2.0\...main[Check the HEAD diff]

*Filebeat*

- Fix handling of null or empty arrays during split with keep parent option. {pull}34322[34322]

*Heartbeat*

Expand Down
60 changes: 60 additions & 0 deletions x-pack/filebeat/input/httpjson/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,66 @@ func TestInput(t *testing.T) {
handler: defaultHandler(http.MethodGet, "", `{"response":{"empty":[]}}`),
expected: nil,
},
{
name: "Test split on null field with ignore_empty_value keeping parent",
setupServer: newTestServer(httptest.NewServer),
baseConfig: map[string]interface{}{
"interval": 1,
"request.method": http.MethodGet,
"response.split": map[string]interface{}{
"target": "body.response.empty",
"ignore_empty_value": true,
"keep_parent": true,
},
},
handler: defaultHandler(http.MethodGet, "", `{"response":{"empty":null}}`),
expected: []string{`{"response":{"empty":null}}`},
},
{
name: "Test split on empty array with ignore_empty_value keeping parent",
setupServer: newTestServer(httptest.NewServer),
baseConfig: map[string]interface{}{
"interval": 1,
"request.method": http.MethodGet,
"response.split": map[string]interface{}{
"target": "body.response.empty",
"ignore_empty_value": true,
"keep_parent": true,
},
},
handler: defaultHandler(http.MethodGet, "", `{"response":{"empty":[]}}`),
expected: []string{`{"response":{"empty":[]}}`},
},
{
name: "Test split on null field at root with ignore_empty_value keeping parent",
setupServer: newTestServer(httptest.NewServer),
baseConfig: map[string]interface{}{
"interval": 1,
"request.method": http.MethodGet,
"response.split": map[string]interface{}{
"target": "body.response",
"ignore_empty_value": true,
"keep_parent": true,
},
},
handler: defaultHandler(http.MethodGet, "", `{"response":null,"other":"data"}`),
expected: []string{`{"other":"data","response":null}`},
},
{
name: "Test split on empty array at root with ignore_empty_value keeping parent",
setupServer: newTestServer(httptest.NewServer),
baseConfig: map[string]interface{}{
"interval": 1,
"request.method": http.MethodGet,
"response.split": map[string]interface{}{
"target": "body.response",
"ignore_empty_value": true,
"keep_parent": true,
},
},
handler: defaultHandler(http.MethodGet, "", `{"response":[],"other":"data"}`),
expected: []string{`{"other":"data","response":[]}`},
},
{
name: "Test nested split",
setupServer: newTestServer(httptest.NewServer),
Expand Down
13 changes: 13 additions & 0 deletions x-pack/filebeat/input/httpjson/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,16 @@ func (s *split) split(ctx *transformContext, root mapstr.M, ch chan<- maybeMsg)
if s.child != nil {
return s.child.split(ctx, root, ch)
}
if s.keepParent {
ch <- maybeMsg{msg: root}
}
return nil
}
if s.isRoot {
if s.keepParent {
ch <- maybeMsg{msg: root}
return errEmptyField
}
return errEmptyRootField
}
ch <- maybeMsg{msg: root}
Expand All @@ -133,6 +140,9 @@ func (s *split) split(ctx *transformContext, root mapstr.M, ch chan<- maybeMsg)
if s.child != nil {
return s.child.split(ctx, root, ch)
}
if s.keepParent {
ch <- maybeMsg{msg: root}
}
return nil
}
if s.isRoot {
Expand Down Expand Up @@ -162,6 +172,9 @@ func (s *split) split(ctx *transformContext, root mapstr.M, ch chan<- maybeMsg)
if s.child != nil {
return s.child.split(ctx, root, ch)
}
if s.keepParent {
ch <- maybeMsg{msg: root}
}
return nil
}
if s.isRoot {
Expand Down

0 comments on commit 8ac7cf9

Please sign in to comment.