Skip to content

Change in behaviour for cron expression with day of the week range starting with SUN #26598

Closed
@andrecampanini

Description

@andrecampanini

Affects: Spring Framework 5.3


Before Spring 5.3 the parse for cron expressions had a different behaviour for the day of the week.

Example of an expression that has been working for years: "32 22/5 * ? * SUN-FRI"
Example of its use in Spring 5.3: CronExpression.parse("32 22/5 * ? * SUN-FRI");

Before the six space-separated string SUN-FRI was interpreted as 0-5.
Now in Spring 5.3 it is interpreted as 7-5 and that throws un exception:

Exception in thread "main" java.lang.IllegalArgumentException: Minimum value must be less than maximum value '7-5' in cron expression "32 22/5 * ? * SUN-FRI"
	at org.springframework.scheduling.support.CronExpression.parse(CronExpression.java:201)
	at TestCron.main(TestCron.java:8)
Caused by: java.lang.IllegalArgumentException: Minimum value must be less than maximum value '7-5'
	at org.springframework.scheduling.support.BitsCronField.parseField(BitsCronField.java:150)
	at org.springframework.scheduling.support.BitsCronField.parseDate(BitsCronField.java:117)
	at org.springframework.scheduling.support.BitsCronField.parseDaysOfWeek(BitsCronField.java:103)
	at org.springframework.scheduling.support.CronField.parseDaysOfWeek(CronField.java:105)
	at org.springframework.scheduling.support.CronExpression.parse(CronExpression.java:195)
	... 1 more
Caused by: java.lang.IllegalArgumentException: Minimum value must be less than maximum value
	at java.time.temporal.ValueRange.of(ValueRange.java:127)
	at org.springframework.scheduling.support.BitsCronField.parseRange(BitsCronField.java:169)
	at org.springframework.scheduling.support.BitsCronField.parseField(BitsCronField.java:129)
	... 5 more

A complete and very simple example of use:

import org.springframework.scheduling.support.CronExpression;

public class TestCron {

	public static void main(String[] args) throws Exception {
		CronExpression.parse("32 22/5 * ? * SUN-FRI");
	}
	
}

It seems the class org.springframework.scheduling.support.CronField always consider Monday as position 0 (zero) in its array DAYS. But the documentation https://spring.io/blog/2020/11/10/new-in-spring-5-3-improved-cron-expressions says Sunday is zero (or seven).

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: regressionA bug that is also a regression

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions