Skip to content

fix(alert): guard against null alert/dateCreated in email alert template - #6457

Open
wy471x wants to merge 1 commit into
apache:masterfrom
wy471x:fix_EmailAlertReportFail
Open

fix(alert): guard against null alert/dateCreated in email alert template#6457
wy471x wants to merge 1 commit into
apache:masterfrom
wy471x:fix_EmailAlertReportFail

Conversation

@wy471x

@wy471x wy471x commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Move null check before alert dereferencing in EmailAlertNotifyStrategy to prevent NullPointerException. Add @notblank validation on AlarmContent title and content fields. Add unit tests for both fixes.

Make sure that:

  • You have read the contribution guidelines.
  • You submit test cases (unit or integration tests) that back your changes.
  • Your local test passed ./mvnw clean install -Dmaven.javadoc.skip=true.

Summary of Changes

Bug Fix — EmailAlertNotifyStrategy.java

File: shenyu-alert/src/main/java/org/apache/shenyu/alert/strategy/EmailAlertNotifyStrategy.java

Moved the null check before any dereferencing of alert. Previously, alert.getContent() and
alert.getDateCreated() were called on lines 81-83 while the null guard if (Objects.isNull(alert))
sat dead on line 84. Now both alert == null and dateCreated == null are guarded with safe
fallbacks ("" for content, new Date() for date), matching the pattern already used in
AbstractAlertNotifyHandler.renderContent().

Bean Validation — AlarmContent.java

File: shenyu-common/src/main/java/org/apache/shenyu/common/dto/AlarmContent.java

Added @notblank constraints on title and content fields. The existing ExceptionHandlers already
catches MethodArgumentNotValidException, so invalid requests now get a controlled 400 response
instead of an internal error.

Dependency — shenyu-common/pom.xml

Added jakarta.validation-api (provided scope) since AlarmContent now imports @notblank.

Unit Tests

  • AlarmContentTest.java (shenyu-common) — verifies @notblank annotations are present on title and
    content, plus builder/getter/setter correctness.
  • EmailAlertNotifyStrategyTest.java (shenyu-alert) — verifies no NPE is thrown when alert is null,
    dateCreated is null, or content is null.

close #6446

@wy471x wy471x changed the title fix(alert): guard against null alert/dateCreated in email alert template (#xxx) fix(alert): guard against null alert/dateCreated in email alert template Jul 26, 2026
@wy471x
wy471x force-pushed the fix_EmailAlertReportFail branch from bb7c523 to cbe08cf Compare July 26, 2026 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Email alert reporting can fail with NPE for null or partial AlarmContent

1 participant