SbloodyS commented on code in PR #18741:
URL:
https://github.com/apache/dolphinscheduler/pull/18741#discussion_r4396648141
##########
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java:
##########
@@ -217,12 +218,52 @@ private void setMsgInRequestBody(String msg) {
return;
}
- bodyParams.forEach((key, value) -> {
- String valueOf = String.valueOf(value);
- if (valueOf.contains(HttpAlertConstants.MSG_PARAMS)) {
- bodyParams.put(key,
valueOf.replace(HttpAlertConstants.MSG_PARAMS, msg));
+ if (msg == null) {
+ if (containsMsgPlaceholder(bodyParams)) {
+ throw new RuntimeException(
+ String.format("alert content is null, can not replace
%s in the body params",
+ HttpAlertConstants.MSG_PARAMS));
}
- });
+ return;
+ }
+
+ replaceMsg(bodyParams, msg);
+ }
+
+ /**
+ * Whether the body params require an alert message, so that a static body
without the
+ * placeholder can still be sent when the message is null.
+ */
+ @SuppressWarnings("unchecked")
Review Comment:
Please avoid using `@SuppressWarnings`
##########
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java:
##########
@@ -217,12 +218,52 @@ private void setMsgInRequestBody(String msg) {
return;
}
- bodyParams.forEach((key, value) -> {
- String valueOf = String.valueOf(value);
- if (valueOf.contains(HttpAlertConstants.MSG_PARAMS)) {
- bodyParams.put(key,
valueOf.replace(HttpAlertConstants.MSG_PARAMS, msg));
+ if (msg == null) {
+ if (containsMsgPlaceholder(bodyParams)) {
+ throw new RuntimeException(
+ String.format("alert content is null, can not replace
%s in the body params",
+ HttpAlertConstants.MSG_PARAMS));
}
- });
+ return;
+ }
+
+ replaceMsg(bodyParams, msg);
+ }
+
+ /**
+ * Whether the body params require an alert message, so that a static body
without the
+ * placeholder can still be sent when the message is null.
+ */
+ @SuppressWarnings("unchecked")
+ private boolean containsMsgPlaceholder(Object value) {
+ if (value instanceof String) {
+ return ((String) value).contains(HttpAlertConstants.MSG_PARAMS);
+ }
+ if (value instanceof Map) {
+ return ((Map<Object, Object>)
value).values().stream().anyMatch(this::containsMsgPlaceholder);
+ }
+ if (value instanceof List) {
+ return ((List<Object>)
value).stream().anyMatch(this::containsMsgPlaceholder);
+ }
+ return false;
+ }
+
+ /**
+ * Recursively replace ${msg} in string nodes while keeping the json
structure,
+ * so that nested objects/arrays are not flattened into a string.
+ * Characters in msg are escaped by Jackson when the request body is
serialized.
+ */
+ @SuppressWarnings("unchecked")
Review Comment:
Same here.
--
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]