-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Support epoch millisecond values for datetime predicates in WebMVC #4005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Support epoch millisecond values for datetime predicates in WebMVC #4005
Conversation
Enable After, Before, and Between predicates to accept epoch milliseconds in YAML configuration by automatically registering StringToZonedDateTimeConverter to the ConversionService used by RouterFunctionHolderFactory Signed-off-by: raccoonback <[email protected]>
| if (this.conversionService instanceof ConfigurableConversionService configurableConversionService) { | ||
| configurableConversionService.addConverter(new StringToZonedDateTimeConverter()); | ||
| } | ||
|
|
||
| this.parameterValueMapper = new ConversionServiceParameterValueMapper(this.conversionService); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The custom StringToZonedDateTimeConverter is not registered in the shared ConversionService instance, so this PR explicitly registers it in the actual ConversionService used by WebMVC. This ensures that datetime predicates can correctly parse both ISO-8601 strings and epoch millisecond values.
| public class StringToZonedDateTimeConverter implements Converter<String, ZonedDateTime> { | ||
|
|
||
| @Override | ||
| public ZonedDateTime convert(String source) { | ||
| try { | ||
| long epoch = Long.parseLong(source); | ||
| return Instant.ofEpochMilli(epoch).atOffset(ZoneOffset.ofTotalSeconds(0)).toZonedDateTime(); | ||
| } | ||
| catch (NumberFormatException e) { | ||
| // try ZonedDateTime instead | ||
| return ZonedDateTime.parse(source); | ||
| } | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
@ryanjbaxter @spencergibb |
Motivation
WebMVC predicates previously accepted only ISO-8601 timestamps, limiting usability for configurations that rely on epoch values (common in distributed systems and machine-generated configs).
This PR enhances the developer experience and aligns WebMVC predicate behavior with the more flexible usage found in other parts of Spring.
Summary
This PR adds support for using epoch millisecond values in WebMVC datetime predicates (
After,Before,Between) when configured through YAML.Introduces
StringToZonedDateTimeConverterto convert both:ZonedDateTimeRegisters the converter in
RouterFunctionHolderFactorywhen the underlyingConversionServiceis configurable.Updates
ConversionServiceParameterValueMapperto rely on the injectedConversionService.Provides full integration test coverage:
Adds sample
application-datetime.ymlused by integration tests.