Adapt to upstream API version updates
Build and Deploy Snapshot / Build and Deploy Snapshot (push) Waiting to run Details
Build and Deploy Snapshot / Trigger Docs Build (push) Blocked by required conditions Details
Build and Deploy Snapshot / Verify (push) Blocked by required conditions Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:false version:24], map[id:${{ vars.UBUNTU_MEDIUM || 'ubuntu-latest' }} name:Linux]) (push) Waiting to run Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:false version:24], map[id:windows-latest name:Windows]) (push) Waiting to run Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:17], map[id:${{ vars.UBUNTU_MEDIUM || 'ubuntu-latest' }} name:Linux]) (push) Waiting to run Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:17], map[id:windows-latest name:Windows]) (push) Waiting to run Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:21], map[id:${{ vars.UBUNTU_MEDIUM || 'ubuntu-latest' }} name:Linux]) (push) Waiting to run Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:21], map[id:windows-latest name:Windows]) (push) Waiting to run Details
Run CodeQL Analysis / run-analysis (push) Waiting to run Details
Run System Tests / Java ${{ matrix.java.version}} (map[toolchain:true version:17]) (push) Waiting to run Details
Run System Tests / Java ${{ matrix.java.version}} (map[toolchain:true version:21]) (push) Waiting to run Details

Closes gh-46519
This commit is contained in:
Phillip Webb 2025-07-31 17:51:44 +01:00
parent 0f2e0d693a
commit dba148ea37
9 changed files with 59 additions and 41 deletions

View File

@ -69,6 +69,11 @@ public class ApiversionProperties {
*/ */
private Integer pathSegment; private Integer pathSegment;
/**
* Insert the version into a media type parameter with the given name.
*/
private String mediaTypeParameter;
public String getHeader() { public String getHeader() {
return this.header; return this.header;
} }
@ -93,6 +98,14 @@ public class ApiversionProperties {
this.pathSegment = pathSegment; this.pathSegment = pathSegment;
} }
public String getMediaTypeParameter() {
return this.mediaTypeParameter;
}
public void setMediaTypeParameter(String mediaTypeParameter) {
this.mediaTypeParameter = mediaTypeParameter;
}
} }
} }

View File

@ -98,6 +98,7 @@ public final class PropertiesApiVersionInserter implements ApiVersionInserter {
map.from(insert::getHeader).whenHasText().as(counter::counted).to(builder::useHeader); map.from(insert::getHeader).whenHasText().as(counter::counted).to(builder::useHeader);
map.from(insert::getQueryParameter).whenHasText().as(counter::counted).to(builder::useQueryParam); map.from(insert::getQueryParameter).whenHasText().as(counter::counted).to(builder::useQueryParam);
map.from(insert::getPathSegment).as(counter::counted).to(builder::usePathSegment); map.from(insert::getPathSegment).as(counter::counted).to(builder::usePathSegment);
map.from(insert::getMediaTypeParameter).to(builder::useMediaTypeParam);
if (!counter.isEmpty()) { if (!counter.isEmpty()) {
inserters.add(builder.build()); inserters.add(builder.build());
} }

View File

@ -22,6 +22,7 @@ import java.util.Locale;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.ApiVersionFormatter; import org.springframework.web.client.ApiVersionFormatter;
import org.springframework.web.client.ApiVersionInserter; import org.springframework.web.client.ApiVersionInserter;
@ -60,12 +61,15 @@ class PropertiesApiVersionInserterTests {
ApiversionProperties properties2 = new ApiversionProperties(); ApiversionProperties properties2 = new ApiversionProperties();
properties2.getInsert().setQueryParameter("v2"); properties2.getInsert().setQueryParameter("v2");
properties2.getInsert().setPathSegment(1); properties2.getInsert().setPathSegment(1);
properties2.getInsert().setMediaTypeParameter("mtp");
ApiVersionInserter inserter = PropertiesApiVersionInserter.get(null, null, properties1, properties2); ApiVersionInserter inserter = PropertiesApiVersionInserter.get(null, null, properties1, properties2);
URI uri = new URI("https://example.com/foo/bar"); URI uri = new URI("https://example.com/foo/bar");
assertThat(inserter.insertVersion("123", uri)).hasToString("https://example.com/foo/123/bar?v1=123&v2=123"); assertThat(inserter.insertVersion("123", uri)).hasToString("https://example.com/foo/123/bar?v1=123&v2=123");
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
inserter.insertVersion("123", headers); inserter.insertVersion("123", headers);
assertThat(headers.get("x-test")).containsExactly("123"); assertThat(headers.get("x-test")).containsExactly("123");
assertThat(headers.getContentType().getParameters()).containsEntry("mtp", "123");
} }
@Test @Test

View File

@ -276,10 +276,10 @@ public final class WebFluxAutoConfiguration {
public void configureApiVersioning(ApiVersionConfigurer configurer) { public void configureApiVersioning(ApiVersionConfigurer configurer) {
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
Apiversion properties = this.webFluxProperties.getApiversion(); Apiversion properties = this.webFluxProperties.getApiversion();
map.from(properties::isRequired).to(configurer::setVersionRequired); map.from(properties::getRequired).to(configurer::setVersionRequired);
map.from(properties::getDefaultVersion).to(configurer::setDefaultVersion); map.from(properties::getDefaultVersion).to(configurer::setDefaultVersion);
map.from(properties::getSupported).to((supported) -> supported.forEach(configurer::addSupportedVersions)); map.from(properties::getSupported).to((supported) -> supported.forEach(configurer::addSupportedVersions));
map.from(properties::isDetectSupported).to(configurer::detectSupportedVersions); map.from(properties::getDetectSupported).to(configurer::detectSupportedVersions);
configureApiVersioningUse(configurer, properties.getUse()); configureApiVersioningUse(configurer, properties.getUse());
this.apiVersionResolvers.orderedStream().forEach(configurer::useVersionResolver); this.apiVersionResolvers.orderedStream().forEach(configurer::useVersionResolver);
this.apiVersionParser.ifAvailable(configurer::setVersionParser); this.apiVersionParser.ifAvailable(configurer::setVersionParser);
@ -289,7 +289,7 @@ public final class WebFluxAutoConfiguration {
private void configureApiVersioningUse(ApiVersionConfigurer configurer, Use use) { private void configureApiVersioningUse(ApiVersionConfigurer configurer, Use use) {
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(use::getHeader).whenHasText().to(configurer::useRequestHeader); map.from(use::getHeader).whenHasText().to(configurer::useRequestHeader);
map.from(use::getRequestParameter).whenHasText().to(configurer::useRequestParam); map.from(use::getQueryParameter).whenHasText().to(configurer::useQueryParam);
map.from(use::getPathSegment).to(configurer::usePathSegment); map.from(use::getPathSegment).to(configurer::usePathSegment);
use.getMediaTypeParameter() use.getMediaTypeParameter()
.forEach((mediaType, parameterName) -> configurer.useMediaTypeParameter(mediaType, parameterName)); .forEach((mediaType, parameterName) -> configurer.useMediaTypeParameter(mediaType, parameterName));

View File

@ -176,7 +176,7 @@ public class WebFluxProperties {
/** /**
* Whether the API version is required with each request. * Whether the API version is required with each request.
*/ */
private boolean required = false; private Boolean required;
/** /**
* Default version that should be used for each request. * Default version that should be used for each request.
@ -192,18 +192,18 @@ public class WebFluxProperties {
/** /**
* Whether supported versions should be detected from controllers. * Whether supported versions should be detected from controllers.
*/ */
private boolean detectSupported = true; private Boolean detectSupported;
/** /**
* How version details should be inserted into requests. * How version details should be inserted into requests.
*/ */
private final Use use = new Use(); private final Use use = new Use();
public boolean isRequired() { public Boolean getRequired() {
return this.required; return this.required;
} }
public void setRequired(boolean required) { public void setRequired(Boolean required) {
this.required = required; this.required = required;
} }
@ -223,18 +223,18 @@ public class WebFluxProperties {
this.supported = supported; this.supported = supported;
} }
public Use getUse() { public Boolean getDetectSupported() {
return this.use;
}
public boolean isDetectSupported() {
return this.detectSupported; return this.detectSupported;
} }
public void setDetectSupported(boolean detectSupported) { public void setDetectSupported(Boolean detectSupported) {
this.detectSupported = detectSupported; this.detectSupported = detectSupported;
} }
public Use getUse() {
return this.use;
}
public static class Use { public static class Use {
/** /**
@ -245,7 +245,7 @@ public class WebFluxProperties {
/** /**
* Use the query parameter with the given name to obtain the version. * Use the query parameter with the given name to obtain the version.
*/ */
private String requestParameter; private String queryParameter;
/** /**
* Use the path segment at the given index to obtain the version. * Use the path segment at the given index to obtain the version.
@ -265,12 +265,12 @@ public class WebFluxProperties {
this.header = header; this.header = header;
} }
public String getRequestParameter() { public String getQueryParameter() {
return this.requestParameter; return this.queryParameter;
} }
public void setRequestParameter(String queryParameter) { public void setQueryParameter(String queryParameter) {
this.requestParameter = queryParameter; this.queryParameter = queryParameter;
} }
public Integer getPathSegment() { public Integer getPathSegment() {

View File

@ -850,8 +850,8 @@ class WebFluxAutoConfigurationTests {
} }
@Test @Test
void apiVersionUseRequestParameterPropertyIsApplied() { void apiVersionUseQueryParameterPropertyIsApplied() {
this.contextRunner.withPropertyValues("spring.webflux.apiversion.use.request-parameter=rpv").run((context) -> { this.contextRunner.withPropertyValues("spring.webflux.apiversion.use.query-parameter=rpv").run((context) -> {
DefaultApiVersionStrategy versionStrategy = context.getBean("webFluxApiVersionStrategy", DefaultApiVersionStrategy versionStrategy = context.getBean("webFluxApiVersionStrategy",
DefaultApiVersionStrategy.class); DefaultApiVersionStrategy.class);
MockServerWebExchange request = MockServerWebExchange MockServerWebExchange request = MockServerWebExchange

View File

@ -390,10 +390,10 @@ public final class WebMvcAutoConfiguration {
public void configureApiVersioning(ApiVersionConfigurer configurer) { public void configureApiVersioning(ApiVersionConfigurer configurer) {
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
Apiversion properties = this.mvcProperties.getApiversion(); Apiversion properties = this.mvcProperties.getApiversion();
map.from(properties::isRequired).to(configurer::setVersionRequired); map.from(properties::getRequired).to(configurer::setVersionRequired);
map.from(properties::getDefaultVersion).to(configurer::setDefaultVersion); map.from(properties::getDefaultVersion).to(configurer::setDefaultVersion);
map.from(properties::getSupported).to((supported) -> supported.forEach(configurer::addSupportedVersions)); map.from(properties::getSupported).to((supported) -> supported.forEach(configurer::addSupportedVersions));
map.from(properties::isDetectSupported).to(configurer::detectSupportedVersions); map.from(properties::getDetectSupported).to(configurer::detectSupportedVersions);
configureApiVersioningUse(configurer, properties.getUse()); configureApiVersioningUse(configurer, properties.getUse());
this.apiVersionResolvers.orderedStream().forEach(configurer::useVersionResolver); this.apiVersionResolvers.orderedStream().forEach(configurer::useVersionResolver);
this.apiVersionParser.ifAvailable(configurer::setVersionParser); this.apiVersionParser.ifAvailable(configurer::setVersionParser);
@ -403,7 +403,7 @@ public final class WebMvcAutoConfiguration {
private void configureApiVersioningUse(ApiVersionConfigurer configurer, Use use) { private void configureApiVersioningUse(ApiVersionConfigurer configurer, Use use) {
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(use::getHeader).whenHasText().to(configurer::useRequestHeader); map.from(use::getHeader).whenHasText().to(configurer::useRequestHeader);
map.from(use::getRequestParameter).whenHasText().to(configurer::useRequestParam); map.from(use::getQueryParameter).whenHasText().to(configurer::useQueryParam);
map.from(use::getPathSegment).to(configurer::usePathSegment); map.from(use::getPathSegment).to(configurer::usePathSegment);
use.getMediaTypeParameter() use.getMediaTypeParameter()
.forEach((mediaType, parameterName) -> configurer.useMediaTypeParameter(mediaType, parameterName)); .forEach((mediaType, parameterName) -> configurer.useMediaTypeParameter(mediaType, parameterName));

View File

@ -477,7 +477,7 @@ public class WebMvcProperties {
/** /**
* Whether the API version is required with each request. * Whether the API version is required with each request.
*/ */
private boolean required = false; private Boolean required;
/** /**
* Default version that should be used for each request. * Default version that should be used for each request.
@ -493,18 +493,18 @@ public class WebMvcProperties {
/** /**
* Whether supported versions should be detected from controllers. * Whether supported versions should be detected from controllers.
*/ */
private boolean detectSupported = true; private Boolean detectSupported;
/** /**
* How version details should be inserted into requests. * How version details should be inserted into requests.
*/ */
private final Use use = new Use(); private final Use use = new Use();
public boolean isRequired() { public Boolean getRequired() {
return this.required; return this.required;
} }
public void setRequired(boolean required) { public void setRequired(Boolean required) {
this.required = required; this.required = required;
} }
@ -524,18 +524,18 @@ public class WebMvcProperties {
this.supported = supported; this.supported = supported;
} }
public Use getUse() { public Boolean getDetectSupported() {
return this.use;
}
public boolean isDetectSupported() {
return this.detectSupported; return this.detectSupported;
} }
public void setDetectSupported(boolean detectSupported) { public void setDetectSupported(Boolean detectSupported) {
this.detectSupported = detectSupported; this.detectSupported = detectSupported;
} }
public Use getUse() {
return this.use;
}
public static class Use { public static class Use {
/** /**
@ -546,7 +546,7 @@ public class WebMvcProperties {
/** /**
* Use the query parameter with the given name to obtain the version. * Use the query parameter with the given name to obtain the version.
*/ */
private String requestParameter; private String queryParameter;
/** /**
* Use the path segment at the given index to obtain the version. * Use the path segment at the given index to obtain the version.
@ -566,12 +566,12 @@ public class WebMvcProperties {
this.header = header; this.header = header;
} }
public String getRequestParameter() { public String getQueryParameter() {
return this.requestParameter; return this.queryParameter;
} }
public void setRequestParameter(String queryParameter) { public void setQueryParameter(String queryParameter) {
this.requestParameter = queryParameter; this.queryParameter = queryParameter;
} }
public Integer getPathSegment() { public Integer getPathSegment() {

View File

@ -1060,11 +1060,11 @@ class WebMvcAutoConfigurationTests {
} }
@Test @Test
void apiVersionUseRequestParameterPropertyIsApplied() { void apiVersionUseQueryParameterPropertyIsApplied() {
this.contextRunner.withPropertyValues("spring.mvc.apiversion.use.request-parameter=rpv").run((context) -> { this.contextRunner.withPropertyValues("spring.mvc.apiversion.use.query-parameter=rpv").run((context) -> {
ApiVersionStrategy versionStrategy = context.getBean("mvcApiVersionStrategy", ApiVersionStrategy.class); ApiVersionStrategy versionStrategy = context.getBean("mvcApiVersionStrategy", ApiVersionStrategy.class);
MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("rpv", "123"); request.setQueryString("rpv=123");
assertThat(versionStrategy.resolveVersion(request)).isEqualTo("123"); assertThat(versionStrategy.resolveVersion(request)).isEqualTo("123");
}); });
} }