Skip to content

Adds new ApiVersionCustomizer #46653

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,16 @@ static class WebFluxConfig implements WebFluxConfigurer {

private final ObjectProvider<ApiVersionDeprecationHandler> apiVersionDeprecationHandler;

private final ObjectProvider<ApiVersionCustomizer> apiVersionCustomizers;

WebFluxConfig(Environment environment, WebProperties webProperties, WebFluxProperties webFluxProperties,
ListableBeanFactory beanFactory, ObjectProvider<HandlerMethodArgumentResolver> resolvers,
ObjectProvider<CodecCustomizer> codecCustomizers,
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizers,
ObjectProvider<ViewResolver> viewResolvers, ObjectProvider<ApiVersionResolver> apiVersionResolvers,
ObjectProvider<ApiVersionParser<?>> apiVersionParser,
ObjectProvider<ApiVersionDeprecationHandler> apiVersionDeprecationHandler) {
ObjectProvider<ApiVersionDeprecationHandler> apiVersionDeprecationHandler,
ObjectProvider<ApiVersionCustomizer> apiVersionCustomizers) {
this.environment = environment;
this.resourceProperties = webProperties.getResources();
this.webFluxProperties = webFluxProperties;
Expand All @@ -202,6 +205,7 @@ static class WebFluxConfig implements WebFluxConfigurer {
this.apiVersionResolvers = apiVersionResolvers;
this.apiVersionParser = apiVersionParser;
this.apiVersionDeprecationHandler = apiVersionDeprecationHandler;
this.apiVersionCustomizers = apiVersionCustomizers;
}

@Override
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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"));
});
}

Expand Down Expand Up @@ -1312,6 +1316,17 @@ ApiVersionParser<String> apiVersionParser() {
return (version) -> String.valueOf(version);
}

@Bean
Predicate<Comparable<?>> supportedVersionPredicate() {
return (comparable) -> true;
}

@Bean
ApiVersionCustomizer apiVersionCustomizer(
@Qualifier("supportedVersionPredicate") Predicate<Comparable<?>> supportedVersionPredicate) {
return (configurer) -> configurer.setSupportedVersionPredicate(supportedVersionPredicate);
}

}

}
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,17 @@ static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, Servlet

private final ObjectProvider<ApiVersionDeprecationHandler> apiVersionDeprecationHandler;

private final ObjectProvider<ApiVersionCustomizer> apiVersionCustomizers;

WebMvcAutoConfigurationAdapter(WebProperties webProperties, WebMvcProperties mvcProperties,
ListableBeanFactory beanFactory, ObjectProvider<HttpMessageConverters> messageConvertersProvider,
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider,
ObjectProvider<DispatcherServletPath> dispatcherServletPath,
ObjectProvider<ServletRegistrationBean<?>> servletRegistrations,
ObjectProvider<ApiVersionResolver> apiVersionResolvers,
ObjectProvider<ApiVersionParser<?>> apiVersionParser,
ObjectProvider<ApiVersionDeprecationHandler> apiVersionDeprecationHandler) {
ObjectProvider<ApiVersionDeprecationHandler> apiVersionDeprecationHandler,
ObjectProvider<ApiVersionCustomizer> apiVersionCustomizers) {
this.resourceProperties = webProperties.getResources();
this.mvcProperties = mvcProperties;
this.beanFactory = beanFactory;
Expand All @@ -229,6 +232,7 @@ static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, Servlet
this.apiVersionResolvers = apiVersionResolvers;
this.apiVersionParser = apiVersionParser;
this.apiVersionDeprecationHandler = apiVersionDeprecationHandler;
this.apiVersionCustomizers = apiVersionCustomizers;
}

@Override
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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"));
});
}

Expand Down Expand Up @@ -1683,6 +1687,17 @@ ApiVersionParser<String> apiVersionParser() {
return (version) -> String.valueOf(version);
}

@Bean
Predicate<Comparable<?>> supportedVersionPredicate() {
return (comparable) -> true;
}

@Bean
ApiVersionCustomizer apiVersionCustomizer(
@Qualifier("supportedVersionPredicate") Predicate<Comparable<?>> supportedVersionPredicate) {
return (configurer) -> configurer.setSupportedVersionPredicate(supportedVersionPredicate);
}

}

}