Skip to content

Commit

Permalink
Transform json.Number values from http_endpoint (#27480) (#27490)
Browse files Browse the repository at this point in the history
This adds a necessary transform to the output of http_endpoint
so that numeric values are encoded as such instead of as a
string.

Fixes #27382

(cherry picked from commit 704007d)

Co-authored-by: Adrian Serrano <adrisr83@gmail.com>
  • Loading branch information
mergify[bot] and adriansr authored Aug 19, 2021
1 parent 24f910a commit c30b7af
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,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

0 comments on commit c30b7af

Please sign in to comment.