Browse Source

Check resolver set when API version config customized

Closes gh-35256
pull/34146/merge
rstoyanchev 5 months ago
parent
commit
18ee8adaeb
  1. 14
      spring-webflux/src/main/java/org/springframework/web/reactive/config/ApiVersionConfigurer.java
  2. 13
      spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ApiVersionConfigurer.java

14
spring-webflux/src/main/java/org/springframework/web/reactive/config/ApiVersionConfigurer.java

@ -26,6 +26,7 @@ import java.util.Set; @@ -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 { @@ -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 { @@ -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());
}
}

13
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ApiVersionConfigurer.java

@ -26,6 +26,7 @@ import java.util.Set; @@ -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 { @@ -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 { @@ -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 { @@ -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());
}
}

Loading…
Cancel
Save