mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
Resolve API version in RequestMappingHandlerMapping
API version resolution and parsing is already applied as long as an ApiVersionStrategy is configured and irrespective of whether a given RequestMapping has a version or not. RequestMappingHandlerMapping also needs to be aware of the API version in order to apply deprecated version handling. So it is better to resolve, parse, and validate the version in the beginning of handler mapping rather than in the first call to any VersionRequestCondition. Closes gh-35049
This commit is contained in:
+19
-11
@@ -36,7 +36,7 @@ public class DefaultApiVersionStrategiesTests {
|
||||
|
||||
|
||||
@Test
|
||||
void defaultVersion() {
|
||||
void defaultVersionIsParsed() {
|
||||
SemanticApiVersionParser.Version version = this.parser.parseVersion("1.2.3");
|
||||
ApiVersionStrategy strategy = initVersionStrategy(version.toString());
|
||||
|
||||
@@ -44,21 +44,29 @@ public class DefaultApiVersionStrategiesTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void supportedVersions() {
|
||||
SemanticApiVersionParser.Version v1 = this.parser.parseVersion("1");
|
||||
SemanticApiVersionParser.Version v2 = this.parser.parseVersion("2");
|
||||
SemanticApiVersionParser.Version v9 = this.parser.parseVersion("9");
|
||||
void validateSupportedVersion() {
|
||||
SemanticApiVersionParser.Version v12 = this.parser.parseVersion("1.2");
|
||||
|
||||
DefaultApiVersionStrategy strategy = initVersionStrategy(null);
|
||||
strategy.addSupportedVersion(v1.toString());
|
||||
strategy.addSupportedVersion(v2.toString());
|
||||
strategy.addSupportedVersion(v12.toString());
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
strategy.validateVersion(v1, request);
|
||||
strategy.validateVersion(v2, request);
|
||||
strategy.validateVersion(v12, request);
|
||||
}
|
||||
|
||||
assertThatThrownBy(() -> strategy.validateVersion(v9, request))
|
||||
.isInstanceOf(InvalidApiVersionException.class);
|
||||
@Test
|
||||
void validateUnsupportedVersion() {
|
||||
assertThatThrownBy(() -> initVersionStrategy(null).validateVersion("1.2", new MockHttpServletRequest()))
|
||||
.isInstanceOf(InvalidApiVersionException.class)
|
||||
.hasMessage("400 BAD_REQUEST \"Invalid API version: '1.2'.\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingRequiredVersion() {
|
||||
DefaultApiVersionStrategy strategy = initVersionStrategy(null);
|
||||
assertThatThrownBy(() -> strategy.validateVersion(null, new MockHttpServletRequest()))
|
||||
.isInstanceOf(MissingApiVersionException.class)
|
||||
.hasMessage("400 BAD_REQUEST \"API version is required.\"");
|
||||
}
|
||||
|
||||
private static DefaultApiVersionStrategy initVersionStrategy(@Nullable String defaultValue) {
|
||||
|
||||
Reference in New Issue
Block a user