Skip to content

Conversation

@raccoonback
Copy link
Contributor

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 StringToZonedDateTimeConverter to convert both:

    • ISO-8601 datetime strings
    • Epoch millisecond values into ZonedDateTime
  • Registers the converter in RouterFunctionHolderFactory when the underlying ConversionService is configurable.

  • Updates ConversionServiceParameterValueMapper to rely on the injected ConversionService.

  • Provides full integration test coverage:

    • YAML-based configuration
    • Java DSL
    • Success and failure cases (range mismatch, past/future constraints)
  • Adds sample application-datetime.yml used by integration tests.

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]>
Comment on lines +147 to +151
if (this.conversionService instanceof ConfigurableConversionService configurableConversionService) {
configurableConversionService.addConverter(new StringToZonedDateTimeConverter());
}

this.parameterValueMapper = new ConversionServiceParameterValueMapper(this.conversionService);
Copy link
Contributor Author

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.

Comment on lines +31 to +45
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);
}
}

}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@raccoonback
Copy link
Contributor Author

@ryanjbaxter @spencergibb
Hello.
I'd appreciate it if you could review this PR.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants