-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,6 @@ | |
|
|
||
| import java.util.Objects; | ||
|
|
||
| import org.springframework.boot.convert.ApplicationConversionService; | ||
| import org.springframework.cloud.gateway.server.mvc.invoke.OperationParameter; | ||
| import org.springframework.cloud.gateway.server.mvc.invoke.ParameterMappingException; | ||
| import org.springframework.cloud.gateway.server.mvc.invoke.ParameterValueMapper; | ||
|
|
@@ -29,19 +28,13 @@ | |
| * | ||
| * @author Stephane Nicoll | ||
| * @author Phillip Webb | ||
| * @author raccoonback | ||
| * @since 2.0.0 | ||
| */ | ||
| public class ConversionServiceParameterValueMapper implements ParameterValueMapper { | ||
|
|
||
| private final ConversionService conversionService; | ||
|
|
||
| /** | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't remove this public constructor in a patch/minor
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ryanjbaxter |
||
| * Create a new {@link ConversionServiceParameterValueMapper} instance. | ||
| */ | ||
| public ConversionServiceParameterValueMapper() { | ||
| this(ApplicationConversionService.getSharedInstance()); | ||
| } | ||
|
|
||
| /** | ||
| * Create a new {@link ConversionServiceParameterValueMapper} instance backed by a | ||
| * specific conversion service. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * Copyright 2025-present the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.cloud.gateway.server.mvc.support; | ||
|
|
||
| import java.time.Instant; | ||
| import java.time.ZoneOffset; | ||
| import java.time.ZonedDateTime; | ||
|
|
||
| import org.springframework.core.convert.converter.Converter; | ||
|
|
||
| /** | ||
| * Converter for converting String to ZonedDateTime. Supports both ISO-8601 format and | ||
| * epoch milliseconds. | ||
| * | ||
| * @author raccoonback | ||
| */ | ||
| 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); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
Comment on lines
+31
to
+45
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| /* | ||
| * Copyright 2025-present the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.cloud.gateway.server.mvc.predicate; | ||
|
|
||
| import java.time.ZonedDateTime; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.SpringBootConfiguration; | ||
| import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
| import org.springframework.cloud.gateway.server.mvc.test.HttpbinTestcontainers; | ||
| import org.springframework.cloud.gateway.server.mvc.test.HttpbinUriResolver; | ||
| import org.springframework.cloud.gateway.server.mvc.test.PermitAllSecurityConfiguration; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Import; | ||
| import org.springframework.test.context.ActiveProfiles; | ||
| import org.springframework.test.context.ContextConfiguration; | ||
| import org.springframework.test.web.servlet.client.RestTestClient; | ||
| import org.springframework.web.servlet.function.RouterFunction; | ||
| import org.springframework.web.servlet.function.ServerResponse; | ||
|
|
||
| import static org.springframework.cloud.gateway.server.mvc.filter.AfterFilterFunctions.addResponseHeader; | ||
| import static org.springframework.cloud.gateway.server.mvc.filter.FilterFunctions.setPath; | ||
| import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route; | ||
| import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http; | ||
| import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.after; | ||
| import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.before; | ||
| import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.between; | ||
|
|
||
| /** | ||
| * Integration tests for datetime predicates (After, Before, Between) with YAML | ||
| * configuration. | ||
| * | ||
| * @author raccoonback | ||
| */ | ||
| @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, | ||
| properties = { "spring.cloud.gateway.server.webmvc.function.enabled=false" }) | ||
| @ActiveProfiles("datetime") | ||
| @ContextConfiguration(initializers = HttpbinTestcontainers.class) | ||
| class DateTimePredicateIntegrationTests { | ||
|
|
||
| @Autowired | ||
| RestTestClient testClient; | ||
|
|
||
| @Test | ||
| void afterPredicateWorksWithYamlConfig() { | ||
| testClient.get() | ||
| .uri("/test/after") | ||
| .exchange() | ||
| .expectStatus() | ||
| .isOk() | ||
| .expectHeader() | ||
| .valueEquals("X-Predicate-Type", "After"); | ||
| } | ||
|
|
||
| @Test | ||
| void beforePredicateWorksWithYamlConfig() { | ||
| testClient.get() | ||
| .uri("/test/before") | ||
| .exchange() | ||
| .expectStatus() | ||
| .isOk() | ||
| .expectHeader() | ||
| .valueEquals("X-Predicate-Type", "Before"); | ||
| } | ||
|
|
||
| @Test | ||
| void betweenPredicateWorksWithYamlConfig() { | ||
| testClient.get() | ||
| .uri("/test/between") | ||
| .exchange() | ||
| .expectStatus() | ||
| .isOk() | ||
| .expectHeader() | ||
| .valueEquals("X-Predicate-Type", "Between"); | ||
| } | ||
|
|
||
| @Test | ||
| void betweenPredicateWorksWithEpochMilliseconds() { | ||
| testClient.get() | ||
| .uri("/test/between-epoch") | ||
| .exchange() | ||
| .expectStatus() | ||
| .isOk() | ||
| .expectHeader() | ||
| .valueEquals("X-Predicate-Type", "BetweenEpoch"); | ||
| } | ||
|
|
||
| @Test | ||
| void afterPredicateRejectsPastTime() { | ||
| // This route requires future time, should NOT match | ||
| testClient.get().uri("/test/future-only").exchange().expectStatus().isNotFound(); | ||
| } | ||
|
|
||
| @Test | ||
| void beforePredicateRejectsFutureTime() { | ||
| // This route requires time before 2020, should NOT match | ||
| testClient.get().uri("/test/past-only").exchange().expectStatus().isNotFound(); | ||
| } | ||
|
|
||
| @Test | ||
| void betweenPredicateRejectsOutsideRange() { | ||
| // This route requires time in 2020, should NOT match | ||
| testClient.get().uri("/test/outside-range").exchange().expectStatus().isNotFound(); | ||
| } | ||
|
|
||
| @Test | ||
| void betweenPredicateRejectsFutureRange() { | ||
| // This route requires time in 2099, should NOT match | ||
| testClient.get().uri("/test/future-range").exchange().expectStatus().isNotFound(); | ||
| } | ||
|
|
||
| @Test | ||
| void afterPredicateWorksWithJavaDsl() { | ||
| testClient.get() | ||
| .uri("/test/after-dsl") | ||
| .exchange() | ||
| .expectStatus() | ||
| .isOk() | ||
| .expectHeader() | ||
| .valueEquals("X-Predicate-Type", "AfterDSL"); | ||
| } | ||
|
|
||
| @Test | ||
| void beforePredicateWorksWithJavaDsl() { | ||
| testClient.get() | ||
| .uri("/test/before-dsl") | ||
| .exchange() | ||
| .expectStatus() | ||
| .isOk() | ||
| .expectHeader() | ||
| .valueEquals("X-Predicate-Type", "BeforeDSL"); | ||
| } | ||
|
|
||
| @Test | ||
| void betweenPredicateWorksWithJavaDsl() { | ||
| testClient.get() | ||
| .uri("/test/between-dsl") | ||
| .exchange() | ||
| .expectStatus() | ||
| .isOk() | ||
| .expectHeader() | ||
| .valueEquals("X-Predicate-Type", "BetweenDSL"); | ||
| } | ||
|
|
||
| @Test | ||
| void afterPredicateRejectsPastTimeWithJavaDsl() { | ||
| // This route requires future time, should NOT match | ||
| testClient.get().uri("/test/after-future-dsl").exchange().expectStatus().isNotFound(); | ||
| } | ||
|
|
||
| @Test | ||
| void beforePredicateRejectsFutureTimeWithJavaDsl() { | ||
| // This route requires past time, should NOT match | ||
| testClient.get().uri("/test/before-past-dsl").exchange().expectStatus().isNotFound(); | ||
| } | ||
|
|
||
| @Test | ||
| void betweenPredicateRejectsOutsideRangeWithJavaDsl() { | ||
| // This route requires time outside current range, should NOT match | ||
| testClient.get().uri("/test/between-past-dsl").exchange().expectStatus().isNotFound(); | ||
| } | ||
|
|
||
| @EnableAutoConfiguration | ||
| @SpringBootConfiguration | ||
| @Import(PermitAllSecurityConfiguration.class) | ||
| static class TestConfig { | ||
|
|
||
| @Bean | ||
| public RouterFunction<ServerResponse> dateTimeRoutes() { | ||
| // @formatter:off | ||
| // Success cases | ||
| return route("after_dsl") | ||
| .GET("/test/after-dsl", after(ZonedDateTime.now().minusDays(1)), http()) | ||
| .filter(setPath("/anything/after-dsl")) | ||
| .before(new HttpbinUriResolver()) | ||
| .after(addResponseHeader("X-Predicate-Type", "AfterDSL")) | ||
| .build() | ||
| .and(route("before_dsl") | ||
| .GET("/test/before-dsl", before(ZonedDateTime.now().plusDays(1)), http()) | ||
| .filter(setPath("/anything/before-dsl")) | ||
| .before(new HttpbinUriResolver()) | ||
| .after(addResponseHeader("X-Predicate-Type", "BeforeDSL")) | ||
| .build()) | ||
| .and(route("between_dsl") | ||
| .GET("/test/between-dsl", between(ZonedDateTime.now().minusDays(1), ZonedDateTime.now().plusDays(1)), http()) | ||
| .filter(setPath("/anything/between-dsl")) | ||
| .before(new HttpbinUriResolver()) | ||
| .after(addResponseHeader("X-Predicate-Type", "BetweenDSL")) | ||
| .build()) | ||
| // Failure cases - should NOT match | ||
| .and(route("after_future_dsl") | ||
| .GET("/test/after-future-dsl", after(ZonedDateTime.now().plusDays(365)), http()) | ||
| .filter(setPath("/anything/after-future-dsl")) | ||
| .before(new HttpbinUriResolver()) | ||
| .after(addResponseHeader("X-Predicate-Type", "AfterFutureDSL")) | ||
| .build()) | ||
| .and(route("before_past_dsl") | ||
| .GET("/test/before-past-dsl", before(ZonedDateTime.now().minusDays(365)), http()) | ||
| .filter(setPath("/anything/before-past-dsl")) | ||
| .before(new HttpbinUriResolver()) | ||
| .after(addResponseHeader("X-Predicate-Type", "BeforePastDSL")) | ||
| .build()) | ||
| .and(route("between_past_dsl") | ||
| .GET("/test/between-past-dsl", between(ZonedDateTime.now().minusDays(730), ZonedDateTime.now().minusDays(365)), http()) | ||
| .filter(setPath("/anything/between-past-dsl")) | ||
| .before(new HttpbinUriResolver()) | ||
| .after(addResponseHeader("X-Predicate-Type", "BetweenPastDSL")) | ||
| .build()); | ||
| // @formatter:on | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
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
StringToZonedDateTimeConverteris not registered in theshared ConversionService instance, so this PR explicitly registers it in the actualConversionServiceused by WebMVC. This ensures that datetime predicates can correctly parse both ISO-8601 strings and epoch millisecond values.