-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
httpjson: Add toJSON helper function #32472
httpjson: Add toJSON helper function #32472
Conversation
3cb9c70
to
d0852d3
Compare
Pinging @elastic/security-external-integrations (Team:Security-External Integrations) |
toJSON converts the given structure into a JSON string. Example usage: response.pagination: - set: target: body.pagingIdentifiers value: '[[ toJSON .last_response.body.pagingIdentifiers ]]' value_type: json fail_on_template_error: true
d0852d3
to
f5a431c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to me that this should be made to work without needing this. In the case that top level maps are mapstr.M
then this is true, so it would be worth finding out why the map that prompted the fix did not end up being that type.
I'm struggling to find where that could be happening or to make the test fail, e.g. this passes
{
name: "func toJSON",
value: "[[ .last_response.body.paginationParams.m.m ]]",
paramCtx: &transformContext{
firstEvent: &mapstr.M{},
lastEvent: &mapstr.M{},
lastResponse: newTestResponse(map[string]interface{}{"paginationParams": map[string]interface{}{"m": map[string]interface{}{"m": map[string]interface{}{"id": 1234}}}}, nil, ""),
},
paramTr: transformable{},
expectedVal: `{"id":1234}`,
},
@@ -574,6 +574,17 @@ func TestValueTpl(t *testing.T) { | |||
paramTr: transformable{}, | |||
expectedVal: "my value", | |||
}, | |||
{ | |||
name: "func toJSON", | |||
value: "[[ toJSON .last_response.body.paginationParams ]]", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test passes without the toJSON
call since the parameter is a mapstr.M
whose String
method renders to JSON.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I changed the test such that it will fail without toJSON
. The code uses mapstr.M.Clone to convert inner objects to mapstr.M, but it does not look inside of []interface{}
for maps. So without toJSON you get
Error Trace: value_tpl_test.go:609
Error: Not equal:
expected: "[{\"id\":1234}]"
actual : "[map[id:1234]]"
Interesting, one clue I have is that this was only needed when processing a response body that is an array of objects. If the response body is an object then it was working fine without running the
So perhaps when the response is an array then the inner objects are left as map[string]interface{} instead of being converted to mapstr.M that has a |
Yes, that explains it. This would be fixed if Alternatively this, but with a failable test. |
This pull request does not have a backport label.
|
Instead of calling toJSON on a mapstr.M, which would already return JSON because mapstr.M implements fmt.Stringer to output JSON, call it on a map[string]interface{}.
In decodeAsJSON, if we apply a conversion to the If I knew all the places where data originates then I would make sure they it were all updated to only produce mapstr.M. I raised and issue to consider making some changes to elastic/elastic-agent-libs#75. But I think I would like to proceed with toJSON since it's simple and does not preclude other solutions. Conversion hack: httpjson-deep-clone-mapstr.patch.txt |
toJSON converts the given structure into a JSON string. Example usage: response.pagination: - set: target: body.pagingIdentifiers value: '[[ toJSON .last_response.body.pagingIdentifiers ]]' value_type: json fail_on_template_error: true (cherry picked from commit 396af77)
toJSON converts the given structure into a JSON string. Example usage: response.pagination: - set: target: body.pagingIdentifiers value: '[[ toJSON .last_response.body.pagingIdentifiers ]]' value_type: json fail_on_template_error: true (cherry picked from commit 396af77) Co-authored-by: Andrew Kroh <[email protected]>
toJSON converts the given structure into a JSON string. Example usage: response.pagination: - set: target: body.pagingIdentifiers value: '[[ toJSON .last_response.body.pagingIdentifiers ]]' value_type: json fail_on_template_error: true
What does this PR do?
toJSON converts the given structure into a JSON string.
Example usage:
Why is it important?
I didn't find any way to use the
set
transform to add an existing object to the output. If you omit thetoJSON
and reference an object in a template then the output is a stringified version of the map likemap[hashes:map[13b97806f9067edfc11359732d8c7d63:0]
which is not what you want when constructing a JSON request body.Checklist
CHANGELOG.next.asciidoc
orCHANGELOG-developer.next.asciidoc
.