Skip to content

Commit 18ee8ad

Browse files
committed
Check resolver set when API version config customized
Closes gh-35256
1 parent 5a3bad6 commit 18ee8ad

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.jspecify.annotations.Nullable;
2727

2828
import org.springframework.http.MediaType;
29+
import org.springframework.util.Assert;
2930
import org.springframework.web.accept.ApiVersionParser;
3031
import org.springframework.web.accept.InvalidApiVersionException;
3132
import org.springframework.web.accept.SemanticApiVersionParser;
@@ -49,7 +50,7 @@ public class ApiVersionConfigurer {
4950

5051
private @Nullable ApiVersionParser<?> versionParser;
5152

52-
private boolean versionRequired = true;
53+
private @Nullable Boolean versionRequired;
5354

5455
private @Nullable String defaultVersion;
5556

@@ -188,18 +189,25 @@ public ApiVersionConfigurer setDeprecationHandler(ApiVersionDeprecationHandler h
188189
}
189190

190191
protected @Nullable ApiVersionStrategy getApiVersionStrategy() {
192+
191193
if (this.versionResolvers.isEmpty()) {
194+
Assert.state(isNotCustomized(), "API version config customized, but no ApiVersionResolver provided");
192195
return null;
193196
}
194197

195198
DefaultApiVersionStrategy strategy = new DefaultApiVersionStrategy(this.versionResolvers,
196199
(this.versionParser != null ? this.versionParser : new SemanticApiVersionParser()),
197-
this.versionRequired, this.defaultVersion, this.detectSupportedVersions,
198-
this.deprecationHandler);
200+
(this.versionRequired != null ? this.versionRequired : true),
201+
this.defaultVersion, this.detectSupportedVersions, this.deprecationHandler);
199202

200203
this.supportedVersions.forEach(strategy::addSupportedVersion);
201204

202205
return strategy;
203206
}
204207

208+
private boolean isNotCustomized() {
209+
return (this.versionParser == null && this.versionRequired == null &&
210+
this.defaultVersion == null && this.supportedVersions.isEmpty());
211+
}
212+
205213
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.jspecify.annotations.Nullable;
2727

2828
import org.springframework.http.MediaType;
29+
import org.springframework.util.Assert;
2930
import org.springframework.web.accept.ApiVersionDeprecationHandler;
3031
import org.springframework.web.accept.ApiVersionParser;
3132
import org.springframework.web.accept.ApiVersionResolver;
@@ -49,7 +50,7 @@ public class ApiVersionConfigurer {
4950

5051
private @Nullable ApiVersionParser<?> versionParser;
5152

52-
private boolean versionRequired = true;
53+
private @Nullable Boolean versionRequired;
5354

5455
private @Nullable String defaultVersion;
5556

@@ -188,18 +189,26 @@ public ApiVersionConfigurer setDeprecationHandler(ApiVersionDeprecationHandler h
188189
}
189190

190191
protected @Nullable ApiVersionStrategy getApiVersionStrategy() {
192+
191193
if (this.versionResolvers.isEmpty()) {
194+
Assert.state(isNotCustomized(), "API version config customized, but no ApiVersionResolver provided");
192195
return null;
193196
}
194197

195198
DefaultApiVersionStrategy strategy = new DefaultApiVersionStrategy(this.versionResolvers,
196199
(this.versionParser != null ? this.versionParser : new SemanticApiVersionParser()),
197-
this.versionRequired, this.defaultVersion, this.detectSupportedVersions,
200+
(this.versionRequired != null ? this.versionRequired : true),
201+
this.defaultVersion, this.detectSupportedVersions,
198202
this.deprecationHandler);
199203

200204
this.supportedVersions.forEach(strategy::addSupportedVersion);
201205

202206
return strategy;
203207
}
204208

209+
private boolean isNotCustomized() {
210+
return (this.versionParser == null && this.versionRequired == null &&
211+
this.defaultVersion == null && this.supportedVersions.isEmpty());
212+
}
213+
205214
}

0 commit comments

Comments
 (0)