ChiangHaosion opened a new pull request, #18741:
URL: https://github.com/apache/dolphinscheduler/pull/18741
## Was this PR generated or assisted by AI?
YES. The first implementation draft of `replaceMsg` and the added unit test
were drafted with an AI coding assistant; the reproduction, the design decision
(recursive replacement instead of string escaping), the review and the
verification were done manually.
## Purpose of the pull request
Fix #18740: the HTTP alert plugin flattens nested `bodyParams` when
substituting `${msg}`, so any body containing a nested object/array is sent as
Java `Map/List.toString()` text and is rejected by the receiver (WeChat Work
robot webhook in my case).
Since #17912 / PR #17911, a nested body can be *configured* (`bodyParams`
became `Map<String, Object>`), but it still cannot be *sent*:
`setMsgInRequestBody` stringified each top-level value before replacing the
placeholder. This PR makes the substitution structure-preserving.
Before, with body params `{"msgtype":"text","text":{"content":"${msg}"}}`:
```text
{"msgtype":"text","text":"{content=hello}"}
```
After:
```text
{"msgtype":"text","text":{"content":"hello"}}
```
Quoting and escaping inside `${msg}` is no longer done manually, it is
handled by Jackson in `OkHttpUtils`, which also removes the risk of producing
invalid JSON.
## Brief change log
- *HttpSender#setMsgInRequestBody*: replace `${msg}` by recursively walking
the parsed body (`Map` / `List`) and substituting only in `String` nodes,
instead of calling `String.valueOf(value)` on top-level values; skip the work
when `msg` is null.
- *HttpSenderTest*: add `testHttpSenderPostWithNestedBody`, which records
the request with MockWebServer and asserts that the nested structure survives
the substitution.
- *HttpSenderTest*: read the recorded body with `Buffer.readUtf8()` instead
of `Buffer.toString()`, whose output is `[size=... text=...]` and is not
version-stable.
## Verify this pull request
This change added tests and can be verified as follows:
- `HttpSenderTest#testHttpSenderPostWithNestedBody` builds an `HttpSender`
with body params
`{"msgtype":"text","text":{"content":[{"message":"${msg}"}]}}`, sends a message
containing a newline and double quotes against MockWebServer, then re-parses
the recorded request body and asserts that `text` is still an object, `content`
is still an array, and `content[0].message` equals the message. Without the
fix, `text` arrives as the string `{content=[{message=...}]}` and the assertion
fails.
- Module tests pass:
```bash
./mvnw -pl
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http
\
-am test -Dtest=HttpSenderTest -Dsurefire.failIfNoSpecifiedTests=false
```
- Manually verified against a real WeChat Work robot webhook with
`{"msgtype":"text","text":{"content":"${msg}"}}` and
`{"msgtype":"markdown","markdown":{"content":"${msg}"}}`: the robot now answers
`{"errcode":0,"errmsg":"ok"}`, while it rejected the flattened payload before.
Closes #18740
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]