Skip to content

Commit dba148e

Browse files
committed
Adapt to upstream API version updates
Closes gh-46519
1 parent 0f2e0d6 commit dba148e

File tree

9 files changed

+59
-41
lines changed

9 files changed

+59
-41
lines changed

module/spring-boot-http-client/src/main/java/org/springframework/boot/http/client/autoconfigure/ApiversionProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public static class Insert {
6969
*/
7070
private Integer pathSegment;
7171

72+
/**
73+
* Insert the version into a media type parameter with the given name.
74+
*/
75+
private String mediaTypeParameter;
76+
7277
public String getHeader() {
7378
return this.header;
7479
}
@@ -93,6 +98,14 @@ public void setPathSegment(Integer pathSegment) {
9398
this.pathSegment = pathSegment;
9499
}
95100

101+
public String getMediaTypeParameter() {
102+
return this.mediaTypeParameter;
103+
}
104+
105+
public void setMediaTypeParameter(String mediaTypeParameter) {
106+
this.mediaTypeParameter = mediaTypeParameter;
107+
}
108+
96109
}
97110

98111
}

module/spring-boot-http-client/src/main/java/org/springframework/boot/http/client/autoconfigure/PropertiesApiVersionInserter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public static ApiVersionInserter get(ApiVersionInserter apiVersionInserter, ApiV
9898
map.from(insert::getHeader).whenHasText().as(counter::counted).to(builder::useHeader);
9999
map.from(insert::getQueryParameter).whenHasText().as(counter::counted).to(builder::useQueryParam);
100100
map.from(insert::getPathSegment).as(counter::counted).to(builder::usePathSegment);
101+
map.from(insert::getMediaTypeParameter).to(builder::useMediaTypeParam);
101102
if (!counter.isEmpty()) {
102103
inserters.add(builder.build());
103104
}

module/spring-boot-http-client/src/test/java/org/springframework/boot/http/client/autoconfigure/PropertiesApiVersionInserterTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.jupiter.api.Test;
2323

2424
import org.springframework.http.HttpHeaders;
25+
import org.springframework.http.MediaType;
2526
import org.springframework.web.client.ApiVersionFormatter;
2627
import org.springframework.web.client.ApiVersionInserter;
2728

@@ -60,12 +61,15 @@ void getReturnsInserterThatAppliesProperties() throws Exception {
6061
ApiversionProperties properties2 = new ApiversionProperties();
6162
properties2.getInsert().setQueryParameter("v2");
6263
properties2.getInsert().setPathSegment(1);
64+
properties2.getInsert().setMediaTypeParameter("mtp");
6365
ApiVersionInserter inserter = PropertiesApiVersionInserter.get(null, null, properties1, properties2);
6466
URI uri = new URI("https://example.com/foo/bar");
6567
assertThat(inserter.insertVersion("123", uri)).hasToString("https://example.com/foo/123/bar?v1=123&v2=123");
6668
HttpHeaders headers = new HttpHeaders();
69+
headers.setContentType(MediaType.APPLICATION_JSON);
6770
inserter.insertVersion("123", headers);
6871
assertThat(headers.get("x-test")).containsExactly("123");
72+
assertThat(headers.getContentType().getParameters()).containsEntry("mtp", "123");
6973
}
7074

7175
@Test

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,10 @@ public void addFormatters(FormatterRegistry registry) {
276276
public void configureApiVersioning(ApiVersionConfigurer configurer) {
277277
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
278278
Apiversion properties = this.webFluxProperties.getApiversion();
279-
map.from(properties::isRequired).to(configurer::setVersionRequired);
279+
map.from(properties::getRequired).to(configurer::setVersionRequired);
280280
map.from(properties::getDefaultVersion).to(configurer::setDefaultVersion);
281281
map.from(properties::getSupported).to((supported) -> supported.forEach(configurer::addSupportedVersions));
282-
map.from(properties::isDetectSupported).to(configurer::detectSupportedVersions);
282+
map.from(properties::getDetectSupported).to(configurer::detectSupportedVersions);
283283
configureApiVersioningUse(configurer, properties.getUse());
284284
this.apiVersionResolvers.orderedStream().forEach(configurer::useVersionResolver);
285285
this.apiVersionParser.ifAvailable(configurer::setVersionParser);
@@ -289,7 +289,7 @@ public void configureApiVersioning(ApiVersionConfigurer configurer) {
289289
private void configureApiVersioningUse(ApiVersionConfigurer configurer, Use use) {
290290
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
291291
map.from(use::getHeader).whenHasText().to(configurer::useRequestHeader);
292-
map.from(use::getRequestParameter).whenHasText().to(configurer::useRequestParam);
292+
map.from(use::getQueryParameter).whenHasText().to(configurer::useQueryParam);
293293
map.from(use::getPathSegment).to(configurer::usePathSegment);
294294
use.getMediaTypeParameter()
295295
.forEach((mediaType, parameterName) -> configurer.useMediaTypeParameter(mediaType, parameterName));

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public static class Apiversion {
176176
/**
177177
* Whether the API version is required with each request.
178178
*/
179-
private boolean required = false;
179+
private Boolean required;
180180

181181
/**
182182
* Default version that should be used for each request.
@@ -192,18 +192,18 @@ public static class Apiversion {
192192
/**
193193
* Whether supported versions should be detected from controllers.
194194
*/
195-
private boolean detectSupported = true;
195+
private Boolean detectSupported;
196196

197197
/**
198198
* How version details should be inserted into requests.
199199
*/
200200
private final Use use = new Use();
201201

202-
public boolean isRequired() {
202+
public Boolean getRequired() {
203203
return this.required;
204204
}
205205

206-
public void setRequired(boolean required) {
206+
public void setRequired(Boolean required) {
207207
this.required = required;
208208
}
209209

@@ -223,18 +223,18 @@ public void setSupported(List<String> supported) {
223223
this.supported = supported;
224224
}
225225

226-
public Use getUse() {
227-
return this.use;
228-
}
229-
230-
public boolean isDetectSupported() {
226+
public Boolean getDetectSupported() {
231227
return this.detectSupported;
232228
}
233229

234-
public void setDetectSupported(boolean detectSupported) {
230+
public void setDetectSupported(Boolean detectSupported) {
235231
this.detectSupported = detectSupported;
236232
}
237233

234+
public Use getUse() {
235+
return this.use;
236+
}
237+
238238
public static class Use {
239239

240240
/**
@@ -245,7 +245,7 @@ public static class Use {
245245
/**
246246
* Use the query parameter with the given name to obtain the version.
247247
*/
248-
private String requestParameter;
248+
private String queryParameter;
249249

250250
/**
251251
* Use the path segment at the given index to obtain the version.
@@ -265,12 +265,12 @@ public void setHeader(String header) {
265265
this.header = header;
266266
}
267267

268-
public String getRequestParameter() {
269-
return this.requestParameter;
268+
public String getQueryParameter() {
269+
return this.queryParameter;
270270
}
271271

272-
public void setRequestParameter(String queryParameter) {
273-
this.requestParameter = queryParameter;
272+
public void setQueryParameter(String queryParameter) {
273+
this.queryParameter = queryParameter;
274274
}
275275

276276
public Integer getPathSegment() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,8 +850,8 @@ void apiVersionUseHeaderPropertyIsApplied() {
850850
}
851851

852852
@Test
853-
void apiVersionUseRequestParameterPropertyIsApplied() {
854-
this.contextRunner.withPropertyValues("spring.webflux.apiversion.use.request-parameter=rpv").run((context) -> {
853+
void apiVersionUseQueryParameterPropertyIsApplied() {
854+
this.contextRunner.withPropertyValues("spring.webflux.apiversion.use.query-parameter=rpv").run((context) -> {
855855
DefaultApiVersionStrategy versionStrategy = context.getBean("webFluxApiVersionStrategy",
856856
DefaultApiVersionStrategy.class);
857857
MockServerWebExchange request = MockServerWebExchange

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,10 @@ private void customizeResourceHandlerRegistration(ResourceHandlerRegistration re
390390
public void configureApiVersioning(ApiVersionConfigurer configurer) {
391391
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
392392
Apiversion properties = this.mvcProperties.getApiversion();
393-
map.from(properties::isRequired).to(configurer::setVersionRequired);
393+
map.from(properties::getRequired).to(configurer::setVersionRequired);
394394
map.from(properties::getDefaultVersion).to(configurer::setDefaultVersion);
395395
map.from(properties::getSupported).to((supported) -> supported.forEach(configurer::addSupportedVersions));
396-
map.from(properties::isDetectSupported).to(configurer::detectSupportedVersions);
396+
map.from(properties::getDetectSupported).to(configurer::detectSupportedVersions);
397397
configureApiVersioningUse(configurer, properties.getUse());
398398
this.apiVersionResolvers.orderedStream().forEach(configurer::useVersionResolver);
399399
this.apiVersionParser.ifAvailable(configurer::setVersionParser);
@@ -403,7 +403,7 @@ public void configureApiVersioning(ApiVersionConfigurer configurer) {
403403
private void configureApiVersioningUse(ApiVersionConfigurer configurer, Use use) {
404404
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
405405
map.from(use::getHeader).whenHasText().to(configurer::useRequestHeader);
406-
map.from(use::getRequestParameter).whenHasText().to(configurer::useRequestParam);
406+
map.from(use::getQueryParameter).whenHasText().to(configurer::useQueryParam);
407407
map.from(use::getPathSegment).to(configurer::usePathSegment);
408408
use.getMediaTypeParameter()
409409
.forEach((mediaType, parameterName) -> configurer.useMediaTypeParameter(mediaType, parameterName));

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ public static class Apiversion {
477477
/**
478478
* Whether the API version is required with each request.
479479
*/
480-
private boolean required = false;
480+
private Boolean required;
481481

482482
/**
483483
* Default version that should be used for each request.
@@ -493,18 +493,18 @@ public static class Apiversion {
493493
/**
494494
* Whether supported versions should be detected from controllers.
495495
*/
496-
private boolean detectSupported = true;
496+
private Boolean detectSupported;
497497

498498
/**
499499
* How version details should be inserted into requests.
500500
*/
501501
private final Use use = new Use();
502502

503-
public boolean isRequired() {
503+
public Boolean getRequired() {
504504
return this.required;
505505
}
506506

507-
public void setRequired(boolean required) {
507+
public void setRequired(Boolean required) {
508508
this.required = required;
509509
}
510510

@@ -524,18 +524,18 @@ public void setSupported(List<String> supported) {
524524
this.supported = supported;
525525
}
526526

527-
public Use getUse() {
528-
return this.use;
529-
}
530-
531-
public boolean isDetectSupported() {
527+
public Boolean getDetectSupported() {
532528
return this.detectSupported;
533529
}
534530

535-
public void setDetectSupported(boolean detectSupported) {
531+
public void setDetectSupported(Boolean detectSupported) {
536532
this.detectSupported = detectSupported;
537533
}
538534

535+
public Use getUse() {
536+
return this.use;
537+
}
538+
539539
public static class Use {
540540

541541
/**
@@ -546,7 +546,7 @@ public static class Use {
546546
/**
547547
* Use the query parameter with the given name to obtain the version.
548548
*/
549-
private String requestParameter;
549+
private String queryParameter;
550550

551551
/**
552552
* Use the path segment at the given index to obtain the version.
@@ -566,12 +566,12 @@ public void setHeader(String header) {
566566
this.header = header;
567567
}
568568

569-
public String getRequestParameter() {
570-
return this.requestParameter;
569+
public String getQueryParameter() {
570+
return this.queryParameter;
571571
}
572572

573-
public void setRequestParameter(String queryParameter) {
574-
this.requestParameter = queryParameter;
573+
public void setQueryParameter(String queryParameter) {
574+
this.queryParameter = queryParameter;
575575
}
576576

577577
public Integer getPathSegment() {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,11 +1060,11 @@ void apiVersionUseHeaderPropertyIsApplied() {
10601060
}
10611061

10621062
@Test
1063-
void apiVersionUseRequestParameterPropertyIsApplied() {
1064-
this.contextRunner.withPropertyValues("spring.mvc.apiversion.use.request-parameter=rpv").run((context) -> {
1063+
void apiVersionUseQueryParameterPropertyIsApplied() {
1064+
this.contextRunner.withPropertyValues("spring.mvc.apiversion.use.query-parameter=rpv").run((context) -> {
10651065
ApiVersionStrategy versionStrategy = context.getBean("mvcApiVersionStrategy", ApiVersionStrategy.class);
10661066
MockHttpServletRequest request = new MockHttpServletRequest();
1067-
request.addParameter("rpv", "123");
1067+
request.setQueryString("rpv=123");
10681068
assertThat(versionStrategy.resolveVersion(request)).isEqualTo("123");
10691069
});
10701070
}

0 commit comments

Comments
 (0)