Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform json.Number values from http_endpoint #27480

Merged
merged 2 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix gcp/vpcflow module error where input type was defaulting to file. {pull}24719[24719]
- Fix s3 input when there is a blank line in the log file. {pull}25357[25357]
- Fixes the Snyk module to work with the new API changes. {pull}27358[27358]
- Fixes a bug in `http_endpoint` that caused numbers encoded as strings. {issue}27382[27382] {pull}27480[27480]

*Heartbeat*

Expand Down
4 changes: 4 additions & 0 deletions x-pack/filebeat/input/http_endpoint/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
stateless "github.com/elastic/beats/v7/filebeat/input/v2/input-stateless"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/jsontransform"
"github.com/elastic/beats/v7/libbeat/logp"
)

Expand Down Expand Up @@ -135,6 +136,9 @@ func decodeJSON(body io.Reader) (objs []common.MapStr, rawMessages []json.RawMes
return nil, nil, errUnsupportedType
}
}
for i := range objs {
jsontransform.TransformNumbers(objs[i])
}
return objs, rawMessages, nil
}

Expand Down
25 changes: 21 additions & 4 deletions x-pack/filebeat/input/http_endpoint/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func Test_httpReadJSON(t *testing.T) {
}{
{
name: "single object",
body: `{"a": "42", "b": "c"}`,
wantObjs: []common.MapStr{{"a": "42", "b": "c"}},
body: `{"a": 42, "b": "c"}`,
wantObjs: []common.MapStr{{"a": int64(42), "b": "c"}},
wantStatus: http.StatusOK,
},
{
Expand All @@ -52,8 +52,8 @@ func Test_httpReadJSON(t *testing.T) {
},
{
name: "sequence of objects accepted (CRLF)",
body: `{"a":"1"}` + "\r" + `{"a":"2"}`,
wantObjs: []common.MapStr{{"a": "1"}, {"a": "2"}},
body: `{"a":1}` + "\r" + `{"a":2}`,
wantObjs: []common.MapStr{{"a": int64(1)}, {"a": int64(2)}},
wantStatus: http.StatusOK,
},
{
Expand Down Expand Up @@ -99,6 +99,23 @@ func Test_httpReadJSON(t *testing.T) {
wantObjs: []common.MapStr{{"a": "1"}, {"a": "2"}, {"a": "3"}, {"a": "4"}},
wantStatus: http.StatusOK,
},
{
name: "numbers",
body: `{"a":1} [{"a":false},{"a":3.14}] {"a":-4}`,
wantRawMessage: []json.RawMessage{
[]byte(`{"a":1}`),
[]byte(`{"a":false}`),
[]byte(`{"a":3.14}`),
[]byte(`{"a":-4}`),
},
wantObjs: []common.MapStr{
{"a": int64(1)},
{"a": false},
{"a": 3.14},
{"a": int64(-4)},
},
wantStatus: http.StatusOK,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down