Skip to content

Commit 40723c6

Browse files
committed
Adds new ApiVersionCustomizer
This allows users to create beans to customize ApiVersionConfigurer.
1 parent 1edaad8 commit 40723c6

File tree

6 files changed

+94
-2
lines changed

6 files changed

+94
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.webflux.autoconfigure;
18+
19+
import org.springframework.web.reactive.config.ApiVersionConfigurer;
20+
21+
/**
22+
* Customizer that can be used to modify the auto-configured
23+
* {@link ApiVersionConfigurer}
24+
*
25+
* @author Spencer Gibb
26+
* @since 4.0.0
27+
*/
28+
public interface ApiVersionCustomizer {
29+
30+
/**
31+
* Customize the given configurer.
32+
* @param apiVersionConfigurer the configurer to customize
33+
*/
34+
void customize(ApiVersionConfigurer apiVersionConfigurer);
35+
}

module/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/WebFluxAutoConfiguration.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,16 @@ static class WebFluxConfig implements WebFluxConfigurer {
184184

185185
private final ObjectProvider<ApiVersionDeprecationHandler> apiVersionDeprecationHandler;
186186

187+
private final ObjectProvider<ApiVersionCustomizer> apiVersionCustomizers;
188+
187189
WebFluxConfig(Environment environment, WebProperties webProperties, WebFluxProperties webFluxProperties,
188190
ListableBeanFactory beanFactory, ObjectProvider<HandlerMethodArgumentResolver> resolvers,
189191
ObjectProvider<CodecCustomizer> codecCustomizers,
190192
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizers,
191193
ObjectProvider<ViewResolver> viewResolvers, ObjectProvider<ApiVersionResolver> apiVersionResolvers,
192194
ObjectProvider<ApiVersionParser<?>> apiVersionParser,
193-
ObjectProvider<ApiVersionDeprecationHandler> apiVersionDeprecationHandler) {
195+
ObjectProvider<ApiVersionDeprecationHandler> apiVersionDeprecationHandler,
196+
ObjectProvider<ApiVersionCustomizer> apiVersionCustomizers) {
194197
this.environment = environment;
195198
this.resourceProperties = webProperties.getResources();
196199
this.webFluxProperties = webFluxProperties;
@@ -202,6 +205,7 @@ static class WebFluxConfig implements WebFluxConfigurer {
202205
this.apiVersionResolvers = apiVersionResolvers;
203206
this.apiVersionParser = apiVersionParser;
204207
this.apiVersionDeprecationHandler = apiVersionDeprecationHandler;
208+
this.apiVersionCustomizers = apiVersionCustomizers;
205209
}
206210

207211
@Override
@@ -284,6 +288,7 @@ public void configureApiVersioning(ApiVersionConfigurer configurer) {
284288
this.apiVersionResolvers.orderedStream().forEach(configurer::useVersionResolver);
285289
this.apiVersionParser.ifAvailable(configurer::setVersionParser);
286290
this.apiVersionDeprecationHandler.ifAvailable(configurer::setDeprecationHandler);
291+
this.apiVersionCustomizers.orderedStream().forEach(customizer -> customizer.customize(configurer));
287292
}
288293

289294
private void configureApiVersioningUse(ApiVersionConfigurer configurer, Use use) {

module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/WebFluxAutoConfigurationTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@ void apiVersionBeansAreInjected() {
896896
assertThat(versionStrategy).extracting("deprecationHandler")
897897
.isEqualTo(context.getBean(ApiVersionDeprecationHandler.class));
898898
assertThat(versionStrategy).extracting("versionParser").isEqualTo(context.getBean(ApiVersionParser.class));
899+
assertThat(versionStrategy).extracting("supportedVersionPredicate").isEqualTo(context.getBean(ApiVersionCustomizer.class));
899900
});
900901
}
901902

@@ -1312,6 +1313,11 @@ ApiVersionParser<String> apiVersionParser() {
13121313
return (version) -> String.valueOf(version);
13131314
}
13141315

1316+
@Bean
1317+
ApiVersionCustomizer apiVersionCustomizer() {
1318+
return configurer -> configurer.setSupportedVersionPredicate(comparable -> true);
1319+
}
1320+
13151321
}
13161322

13171323
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.webmvc.autoconfigure;
18+
19+
20+
import org.springframework.web.servlet.config.annotation.ApiVersionConfigurer;
21+
22+
/**
23+
* Customizer that can be used to modify the auto-configured
24+
* {@link ApiVersionConfigurer}
25+
*
26+
* @author Spencer Gibb
27+
* @since 4.0.0
28+
*/
29+
public interface ApiVersionCustomizer {
30+
31+
/**
32+
* Customize the given configurer.
33+
* @param apiVersionConfigurer the configurer to customize
34+
*/
35+
void customize(ApiVersionConfigurer apiVersionConfigurer);
36+
}

module/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/autoconfigure/WebMvcAutoConfiguration.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,17 @@ static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, Servlet
211211

212212
private final ObjectProvider<ApiVersionDeprecationHandler> apiVersionDeprecationHandler;
213213

214+
private final ObjectProvider<ApiVersionCustomizer> apiVersionCustomizers;
215+
214216
WebMvcAutoConfigurationAdapter(WebProperties webProperties, WebMvcProperties mvcProperties,
215217
ListableBeanFactory beanFactory, ObjectProvider<HttpMessageConverters> messageConvertersProvider,
216218
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider,
217219
ObjectProvider<DispatcherServletPath> dispatcherServletPath,
218220
ObjectProvider<ServletRegistrationBean<?>> servletRegistrations,
219221
ObjectProvider<ApiVersionResolver> apiVersionResolvers,
220222
ObjectProvider<ApiVersionParser<?>> apiVersionParser,
221-
ObjectProvider<ApiVersionDeprecationHandler> apiVersionDeprecationHandler) {
223+
ObjectProvider<ApiVersionDeprecationHandler> apiVersionDeprecationHandler,
224+
ObjectProvider<ApiVersionCustomizer> apiVersionCustomizers) {
222225
this.resourceProperties = webProperties.getResources();
223226
this.mvcProperties = mvcProperties;
224227
this.beanFactory = beanFactory;
@@ -229,6 +232,7 @@ static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, Servlet
229232
this.apiVersionResolvers = apiVersionResolvers;
230233
this.apiVersionParser = apiVersionParser;
231234
this.apiVersionDeprecationHandler = apiVersionDeprecationHandler;
235+
this.apiVersionCustomizers = apiVersionCustomizers;
232236
}
233237

234238
@Override
@@ -398,6 +402,7 @@ public void configureApiVersioning(ApiVersionConfigurer configurer) {
398402
this.apiVersionResolvers.orderedStream().forEach(configurer::useVersionResolver);
399403
this.apiVersionParser.ifAvailable(configurer::setVersionParser);
400404
this.apiVersionDeprecationHandler.ifAvailable(configurer::setDeprecationHandler);
405+
this.apiVersionCustomizers.orderedStream().forEach(customizer -> customizer.customize(configurer));
401406
}
402407

403408
private void configureApiVersioningUse(ApiVersionConfigurer configurer, Use use) {

module/spring-boot-webmvc/src/test/java/org/springframework/boot/webmvc/autoconfigure/WebMvcAutoConfigurationTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,7 @@ void apiVersionBeansAreInjected() {
11011101
assertThat(versionStrategy).extracting("deprecationHandler")
11021102
.isEqualTo(context.getBean(ApiVersionDeprecationHandler.class));
11031103
assertThat(versionStrategy).extracting("versionParser").isEqualTo(context.getBean(ApiVersionParser.class));
1104+
assertThat(versionStrategy).extracting("supportedVersionPredicate").isEqualTo(context.getBean(ApiVersionCustomizer.class));
11041105
});
11051106
}
11061107

@@ -1683,6 +1684,10 @@ ApiVersionParser<String> apiVersionParser() {
16831684
return (version) -> String.valueOf(version);
16841685
}
16851686

1687+
@Bean
1688+
ApiVersionCustomizer apiVersionCustomizer() {
1689+
return configurer -> configurer.setSupportedVersionPredicate(comparable -> true);
1690+
}
16861691
}
16871692

16881693
}

0 commit comments

Comments
 (0)