Skip to content

Include zone ID in CronTrigger's equals/hashCode implementations#36871

Closed
zmovo wants to merge 2 commits into
spring-projects:mainfrom
zmovo:fix-cron-trigger-equals-hashcode-zone
Closed

Include zone ID in CronTrigger's equals/hashCode implementations#36871
zmovo wants to merge 2 commits into
spring-projects:mainfrom
zmovo:fix-cron-trigger-equals-hashcode-zone

Conversation

@zmovo

@zmovo zmovo commented Jun 4, 2026

Copy link
Copy Markdown

By default, CronTrigger#equals and #hashCode consider only the wrapped
CronExpression. Since 5.3, CronTrigger also carries an optional ZoneId
that is used in nextExecution() when a time zone is configured explicitly.
Two triggers built with the same cron pattern but different zones can therefore
produce different next execution times while still comparing as equal and
sharing the same hash code.

This causes incorrect deduplication when triggers are stored in a Set or used
as Map keys, and breaks the general Object contract for a type whose runtime
behavior depends on both the expression and the zone.

This PR includes zoneId in equals and hashCode, using
ObjectUtils.nullSafeEquals and ObjectUtils.nullSafeHash so that triggers
with a null zone (the default constructor) remain equal to each other but
distinct from triggers with an explicit zone.

This does not change trigger scheduling behavior or how nextExecution
selects a zone at runtime; it only corrects value-based equality for triggers
that differ solely by time zone configuration.

It also adds a regression test in
CronTriggerTests#equalsAndHashCodeConsidersZoneId.

Test plan

  • ./gradlew :spring-context:test --tests "org.springframework.scheduling.support.CronTriggerTests.equalsAndHashCodeConsidersZoneId"

CronTrigger carries an optional ZoneId since 5.3 that affects
nextExecution, but equals and hashCode only considered the cron
expression. Include zoneId in both methods.

Signed-off-by: zhaomeng <zhaomeng1.vendor@sensetime.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 4, 2026
@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 4, 2026
@sbrannen sbrannen self-assigned this Jun 4, 2026
@sbrannen sbrannen added this to the 7.0.8 milestone Jun 4, 2026

@sbrannen sbrannen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

I've requested a few minor changes.

@sbrannen sbrannen added the status: waiting-for-feedback We need additional information before we can continue label Jun 4, 2026
@sbrannen sbrannen changed the title Fix CronTrigger equals/hashCode to include zoneId Include zone ID in CronTrigger's equals/hashCode implementations Jun 4, 2026
@zmovo

zmovo commented Jun 4, 2026

Copy link
Copy Markdown
Author

Thanks for the PR.

I've requested a few minor changes.

Thanks for the feedback! I've updated the PR accordingly. Could you please take another look?

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jun 4, 2026
@sbrannen sbrannen removed the status: feedback-provided Feedback has been provided label Jun 4, 2026
Ensure CronTrigger instances with the same cron expression but different
time zones are no longer considered equal.

Signed-off-by: zhaomeng <zhaomeng1.vendor@sensetime.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
@zmovo zmovo force-pushed the fix-cron-trigger-equals-hashcode-zone branch from c7b9784 to 45a013e Compare June 4, 2026 09:35
@zmovo zmovo requested a review from sbrannen June 4, 2026 09:44
sbrannen added a commit that referenced this pull request Jun 5, 2026
@sbrannen sbrannen closed this in 1b32d8d Jun 5, 2026
@sbrannen

sbrannen commented Jun 5, 2026

Copy link
Copy Markdown
Member

This has been merged into 7.0.x and main.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants