From 18ee8adaebfaa8b4f14ffc71339d5b313f60681c Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Thu, 31 Jul 2025 12:26:15 +0100 Subject: [PATCH] Check resolver set when API version config customized Closes gh-35256 --- .../web/reactive/config/ApiVersionConfigurer.java | 14 +++++++++++--- .../config/annotation/ApiVersionConfigurer.java | 13 +++++++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/config/ApiVersionConfigurer.java b/spring-webflux/src/main/java/org/springframework/web/reactive/config/ApiVersionConfigurer.java index 22119b0f14b..b6c351d9030 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/config/ApiVersionConfigurer.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/config/ApiVersionConfigurer.java @@ -26,6 +26,7 @@ import java.util.Set; import org.jspecify.annotations.Nullable; import org.springframework.http.MediaType; +import org.springframework.util.Assert; import org.springframework.web.accept.ApiVersionParser; import org.springframework.web.accept.InvalidApiVersionException; import org.springframework.web.accept.SemanticApiVersionParser; @@ -49,7 +50,7 @@ public class ApiVersionConfigurer { private @Nullable ApiVersionParser versionParser; - private boolean versionRequired = true; + private @Nullable Boolean versionRequired; private @Nullable String defaultVersion; @@ -188,18 +189,25 @@ public class ApiVersionConfigurer { } protected @Nullable ApiVersionStrategy getApiVersionStrategy() { + if (this.versionResolvers.isEmpty()) { + Assert.state(isNotCustomized(), "API version config customized, but no ApiVersionResolver provided"); return null; } DefaultApiVersionStrategy strategy = new DefaultApiVersionStrategy(this.versionResolvers, (this.versionParser != null ? this.versionParser : new SemanticApiVersionParser()), - this.versionRequired, this.defaultVersion, this.detectSupportedVersions, - this.deprecationHandler); + (this.versionRequired != null ? this.versionRequired : true), + this.defaultVersion, this.detectSupportedVersions, this.deprecationHandler); this.supportedVersions.forEach(strategy::addSupportedVersion); return strategy; } + private boolean isNotCustomized() { + return (this.versionParser == null && this.versionRequired == null && + this.defaultVersion == null && this.supportedVersions.isEmpty()); + } + } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ApiVersionConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ApiVersionConfigurer.java index 93e54192b58..d1aea97d8ac 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ApiVersionConfigurer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ApiVersionConfigurer.java @@ -26,6 +26,7 @@ import java.util.Set; import org.jspecify.annotations.Nullable; import org.springframework.http.MediaType; +import org.springframework.util.Assert; import org.springframework.web.accept.ApiVersionDeprecationHandler; import org.springframework.web.accept.ApiVersionParser; import org.springframework.web.accept.ApiVersionResolver; @@ -49,7 +50,7 @@ public class ApiVersionConfigurer { private @Nullable ApiVersionParser versionParser; - private boolean versionRequired = true; + private @Nullable Boolean versionRequired; private @Nullable String defaultVersion; @@ -188,13 +189,16 @@ public class ApiVersionConfigurer { } protected @Nullable ApiVersionStrategy getApiVersionStrategy() { + if (this.versionResolvers.isEmpty()) { + Assert.state(isNotCustomized(), "API version config customized, but no ApiVersionResolver provided"); return null; } DefaultApiVersionStrategy strategy = new DefaultApiVersionStrategy(this.versionResolvers, (this.versionParser != null ? this.versionParser : new SemanticApiVersionParser()), - this.versionRequired, this.defaultVersion, this.detectSupportedVersions, + (this.versionRequired != null ? this.versionRequired : true), + this.defaultVersion, this.detectSupportedVersions, this.deprecationHandler); this.supportedVersions.forEach(strategy::addSupportedVersion); @@ -202,4 +206,9 @@ public class ApiVersionConfigurer { return strategy; } + private boolean isNotCustomized() { + return (this.versionParser == null && this.versionRequired == null && + this.defaultVersion == null && this.supportedVersions.isEmpty()); + } + }