diff --git a/module/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/ApiVersionCustomizer.java b/module/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/ApiVersionCustomizer.java new file mode 100644 index 000000000000..15f6124eccd8 --- /dev/null +++ b/module/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/ApiVersionCustomizer.java @@ -0,0 +1,35 @@ +/* + * Copyright 2012-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.boot.webflux.autoconfigure; + +import org.springframework.web.reactive.config.ApiVersionConfigurer; + +/** + * Customizer that can be used to modify the auto-configured {@link ApiVersionConfigurer}. + * + * @author Spencer Gibb + * @since 4.0.0 + */ +public interface ApiVersionCustomizer { + + /** + * Customize the given configurer. + * @param apiVersionConfigurer the configurer to customize + */ + void customize(ApiVersionConfigurer apiVersionConfigurer); + +} diff --git a/module/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/WebFluxAutoConfiguration.java b/module/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/WebFluxAutoConfiguration.java index ff252ab8b917..7fdd04c6085b 100644 --- a/module/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/WebFluxAutoConfiguration.java +++ b/module/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/WebFluxAutoConfiguration.java @@ -184,13 +184,16 @@ static class WebFluxConfig implements WebFluxConfigurer { private final ObjectProvider apiVersionDeprecationHandler; + private final ObjectProvider apiVersionCustomizers; + WebFluxConfig(Environment environment, WebProperties webProperties, WebFluxProperties webFluxProperties, ListableBeanFactory beanFactory, ObjectProvider resolvers, ObjectProvider codecCustomizers, ObjectProvider resourceHandlerRegistrationCustomizers, ObjectProvider viewResolvers, ObjectProvider apiVersionResolvers, ObjectProvider> apiVersionParser, - ObjectProvider apiVersionDeprecationHandler) { + ObjectProvider apiVersionDeprecationHandler, + ObjectProvider apiVersionCustomizers) { this.environment = environment; this.resourceProperties = webProperties.getResources(); this.webFluxProperties = webFluxProperties; @@ -202,6 +205,7 @@ static class WebFluxConfig implements WebFluxConfigurer { this.apiVersionResolvers = apiVersionResolvers; this.apiVersionParser = apiVersionParser; this.apiVersionDeprecationHandler = apiVersionDeprecationHandler; + this.apiVersionCustomizers = apiVersionCustomizers; } @Override @@ -284,6 +288,7 @@ public void configureApiVersioning(ApiVersionConfigurer configurer) { this.apiVersionResolvers.orderedStream().forEach(configurer::useVersionResolver); this.apiVersionParser.ifAvailable(configurer::setVersionParser); this.apiVersionDeprecationHandler.ifAvailable(configurer::setDeprecationHandler); + this.apiVersionCustomizers.orderedStream().forEach((customizer) -> customizer.customize(configurer)); } private void configureApiVersioningUse(ApiVersionConfigurer configurer, Use use) { diff --git a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/WebFluxAutoConfigurationTests.java b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/WebFluxAutoConfigurationTests.java index a8c21f690869..526aac70ec26 100644 --- a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/WebFluxAutoConfigurationTests.java +++ b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/WebFluxAutoConfigurationTests.java @@ -31,6 +31,7 @@ import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; +import java.util.function.Predicate; import jakarta.validation.ValidatorFactory; import org.aspectj.lang.JoinPoint; @@ -45,6 +46,7 @@ import org.junit.jupiter.params.provider.ValueSource; import org.springframework.aop.support.AopUtils; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration; import org.springframework.boot.http.codec.CodecCustomizer; @@ -896,6 +898,8 @@ void apiVersionBeansAreInjected() { assertThat(versionStrategy).extracting("deprecationHandler") .isEqualTo(context.getBean(ApiVersionDeprecationHandler.class)); assertThat(versionStrategy).extracting("versionParser").isEqualTo(context.getBean(ApiVersionParser.class)); + assertThat(versionStrategy).extracting("supportedVersionPredicate") + .isEqualTo(context.getBean("supportedVersionPredicate")); }); } @@ -1312,6 +1316,17 @@ ApiVersionParser apiVersionParser() { return (version) -> String.valueOf(version); } + @Bean + Predicate> supportedVersionPredicate() { + return (comparable) -> true; + } + + @Bean + ApiVersionCustomizer apiVersionCustomizer( + @Qualifier("supportedVersionPredicate") Predicate> supportedVersionPredicate) { + return (configurer) -> configurer.setSupportedVersionPredicate(supportedVersionPredicate); + } + } } diff --git a/module/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/autoconfigure/ApiVersionCustomizer.java b/module/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/autoconfigure/ApiVersionCustomizer.java new file mode 100644 index 000000000000..27723d1a5a31 --- /dev/null +++ b/module/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/autoconfigure/ApiVersionCustomizer.java @@ -0,0 +1,35 @@ +/* + * Copyright 2012-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.boot.webmvc.autoconfigure; + +import org.springframework.web.servlet.config.annotation.ApiVersionConfigurer; + +/** + * Customizer that can be used to modify the auto-configured {@link ApiVersionConfigurer}. + * + * @author Spencer Gibb + * @since 4.0.0 + */ +public interface ApiVersionCustomizer { + + /** + * Customize the given configurer. + * @param apiVersionConfigurer the configurer to customize + */ + void customize(ApiVersionConfigurer apiVersionConfigurer); + +} diff --git a/module/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/autoconfigure/WebMvcAutoConfiguration.java b/module/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/autoconfigure/WebMvcAutoConfiguration.java index 484557675b8e..2321166991b1 100644 --- a/module/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/autoconfigure/WebMvcAutoConfiguration.java +++ b/module/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/autoconfigure/WebMvcAutoConfiguration.java @@ -211,6 +211,8 @@ static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, Servlet private final ObjectProvider apiVersionDeprecationHandler; + private final ObjectProvider apiVersionCustomizers; + WebMvcAutoConfigurationAdapter(WebProperties webProperties, WebMvcProperties mvcProperties, ListableBeanFactory beanFactory, ObjectProvider messageConvertersProvider, ObjectProvider resourceHandlerRegistrationCustomizerProvider, @@ -218,7 +220,8 @@ static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, Servlet ObjectProvider> servletRegistrations, ObjectProvider apiVersionResolvers, ObjectProvider> apiVersionParser, - ObjectProvider apiVersionDeprecationHandler) { + ObjectProvider apiVersionDeprecationHandler, + ObjectProvider apiVersionCustomizers) { this.resourceProperties = webProperties.getResources(); this.mvcProperties = mvcProperties; this.beanFactory = beanFactory; @@ -229,6 +232,7 @@ static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, Servlet this.apiVersionResolvers = apiVersionResolvers; this.apiVersionParser = apiVersionParser; this.apiVersionDeprecationHandler = apiVersionDeprecationHandler; + this.apiVersionCustomizers = apiVersionCustomizers; } @Override @@ -398,6 +402,7 @@ public void configureApiVersioning(ApiVersionConfigurer configurer) { this.apiVersionResolvers.orderedStream().forEach(configurer::useVersionResolver); this.apiVersionParser.ifAvailable(configurer::setVersionParser); this.apiVersionDeprecationHandler.ifAvailable(configurer::setDeprecationHandler); + this.apiVersionCustomizers.orderedStream().forEach((customizer) -> customizer.customize(configurer)); } private void configureApiVersioningUse(ApiVersionConfigurer configurer, Use use) { diff --git a/module/spring-boot-webmvc/src/test/java/org/springframework/boot/webmvc/autoconfigure/WebMvcAutoConfigurationTests.java b/module/spring-boot-webmvc/src/test/java/org/springframework/boot/webmvc/autoconfigure/WebMvcAutoConfigurationTests.java index a19c8b154a0d..434d123de200 100644 --- a/module/spring-boot-webmvc/src/test/java/org/springframework/boot/webmvc/autoconfigure/WebMvcAutoConfigurationTests.java +++ b/module/spring-boot-webmvc/src/test/java/org/springframework/boot/webmvc/autoconfigure/WebMvcAutoConfigurationTests.java @@ -32,6 +32,7 @@ import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; +import java.util.function.Predicate; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -43,6 +44,7 @@ import org.junit.jupiter.api.Test; import org.springframework.aop.support.AopUtils; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration; @@ -1101,6 +1103,8 @@ void apiVersionBeansAreInjected() { assertThat(versionStrategy).extracting("deprecationHandler") .isEqualTo(context.getBean(ApiVersionDeprecationHandler.class)); assertThat(versionStrategy).extracting("versionParser").isEqualTo(context.getBean(ApiVersionParser.class)); + assertThat(versionStrategy).extracting("supportedVersionPredicate") + .isEqualTo(context.getBean("supportedVersionPredicate")); }); } @@ -1683,6 +1687,17 @@ ApiVersionParser apiVersionParser() { return (version) -> String.valueOf(version); } + @Bean + Predicate> supportedVersionPredicate() { + return (comparable) -> true; + } + + @Bean + ApiVersionCustomizer apiVersionCustomizer( + @Qualifier("supportedVersionPredicate") Predicate> supportedVersionPredicate) { + return (configurer) -> configurer.setSupportedVersionPredicate(supportedVersionPredicate); + } + } }