fix(alert): guard against null alert/dateCreated in email alert template - #6457
fix(alert): guard against null alert/dateCreated in email alert template#6457wy471x wants to merge 20 commits into
Conversation
bb7c523 to
cbe08cf
Compare
|
I found one regression risk outside the email-template null guard. Adding That can drop existing alerts. For example, the gateway error flow can build an alert from an exception message; an Could we keep the email |
…yer DTO Remove @notblank from AlarmContent (shared DTO used by gateway AlarmSender) and add a separate AlertReportRequest DTO at the controller layer instead. This prevents gateway-generated alerts with null content (e.g. from IllegalArgumentException with no message) from being silently dropped by Spring @Valid rejection before dispatch. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@Aias00 Hi, I have fixed the issues you mentioned. Please take a look when you have time. |
Aias00
left a comment
There was a problem hiding this comment.
Reviewed #6457. The core fix is sound: EmailAlertNotifyStrategy.buildAlertHtmlTemplate previously dereferenced alert.getContent()/alert.getDateCreated() before the dead Objects.isNull(alert) guard; the new ordering with dateCreated == null → new Date() is correct and is reachable via the HTTP report path (a caller omitting dateCreated). EmailAlertNotifyStrategyTest.testNullDateCreatedShouldNotThrowNpe backs that path. Moving @NotBlank from the shared AlarmContent DTO to a controller-layer AlertReportRequest is the right call — it avoids silently dropping gateway-generated alerts (which bypass the controller via AlarmSender → AlertDispatchService.dispatchAlert) when content is null.
Non-blocking issues worth addressing before/after merge:
-
PR description is stale. The body still claims
@NotBlankwas added toAlarmContent, thatjakarta.validation-apiwas added toshenyu-common/pom.xml, and thatAlarmContentTestverifies@NotBlank. None of that is true in the final diff —AlarmContentis unmodified, the only pom change is a trailing-whitespace trim, andAlarmContentTestonly tests builder/setters. Please rewrite the body to match the merged approach (controller-layerAlertReportRequest). -
No test backs the new validation or the request→AlarmContent mapping.
AlertReportController.reportAlertnow manually copies 7 fields and relies on@NotBlank, but there is noAlertReportControllerTest(the repo has many*ControllerTestexamples). AMockMvctest asserting (a) blank title/body → 400 with the validation message and (b) a valid request maps through todispatchAlertwith the right fields would close the gap. -
shenyu-common/pom.xmlwhitespace-only hunk — drop it; it serves no purpose in the final approach. -
Minor: the
alert == nullbranch inbuildAlertHtmlTemplateis unreachable (AlertDispatchServiceImpl.DispatchTaskalready guardsObjects.nonNull(alert)before invoking handlers), whileDingTalkRobotAlertNotifyStrategy.send()still dereferencesalert.getTitle()unguarded. Either apply the guard consistently or drop the unreachablealert == nullpart and keep only the realdateCreated == nullfix.
CI note: it / shenyu-integrated-test-combination failed (other it jobs cancelled); ci and e2e are green. Please confirm the combination failure is unrelated/flaky before merging.
…ion test Remove the dead alert==null check in EmailAlertNotifyStrategy since DispatchTask.run() already guarantees non-null alerts upstream. Add AlertReportControllerTest covering @notblank validation rejections and correct AlertReportRequest→AlarmContent mapping. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
I have fixed all the issues you mentioned, as well as the CI problems. I have already verified that they pass in my own fork repository. Thank you for the code review. |
|
Good fix — the old null guard was dead code: Moving validation into a dedicated Two minor notes:
|
Move null check before alert dereferencing in EmailAlertNotifyStrategy to prevent NullPointerException. Add @notblank validation on AlertReportRequest title and content fields. Add field by field mapping from AlertReportRequest to AlertContent in the AlertReportController. Add unit tests for these fixes.
Make sure that:
./mvnw clean install -Dmaven.javadoc.skip=true.Summary of Changes
AlertReportRequest.java (new, shenyu-admin) — Controller-layer request DTO with @notblank on title and content. Validation only applies to external HTTP requests to
/alert/report, not to internal AlarmContent users.
AlertReportController.java — Changed from @Valid @RequestBody AlarmContent to @Valid @RequestBody AlertReportRequest, with explicit field-by-field mapping into AlarmContent
before dispatch.
EmailAlertNotifyStrategy.java — Guards dateCreated == null with a fallback to new Date().
AlarmContentTest.java (new) — Builder and getter/setter test for AlarmContent.
EmailAlertNotifyStrategyTest.java (new) — Tests that buildAlertHtmlTemplate does not throw on null dateCreated or null content.
AlertReportControllerTest.java (new) — MockMvc tests covering blank/null title → 400, blank/null content → 400, and valid request → 200 with correct field mapping to
dispatchAlert.
close #6446