mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-22 05:49:04 +00:00
Polishing contribution
Closes gh-36398
This commit is contained in:
+19
-14
@@ -30,10 +30,13 @@ import org.springframework.web.util.ServletRequestPathUtils;
|
||||
/**
|
||||
* {@link ApiVersionResolver} that extract the version from a path segment.
|
||||
*
|
||||
* <p>Note that this resolver will either resolve the version from the specified
|
||||
* path segment, or raise an {@link InvalidApiVersionException}, e.g. if there
|
||||
* are not enough path segments. It never returns {@code null}, and therefore
|
||||
* cannot yield to other resolvers.
|
||||
* <p>If the resolver is created with a path index only, it will always return
|
||||
* a version, or raise an {@link InvalidApiVersionException}, but never
|
||||
* return {@code null}.
|
||||
*
|
||||
* <p>The resolver can also be created with an additional
|
||||
* {@code Predicate<RequestPath>} that provides more flexibility in deciding
|
||||
* whether a given path is versioned or not, possibly resolving to {@code null}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 7.0
|
||||
@@ -41,7 +44,8 @@ import org.springframework.web.util.ServletRequestPathUtils;
|
||||
public class PathApiVersionResolver implements ApiVersionResolver {
|
||||
|
||||
private final int pathSegmentIndex;
|
||||
private @Nullable Predicate<RequestPath> includePath;
|
||||
|
||||
private @Nullable Predicate<RequestPath> versionPathPredicate;
|
||||
|
||||
|
||||
/**
|
||||
@@ -55,22 +59,19 @@ public class PathApiVersionResolver implements ApiVersionResolver {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a resolver instance.
|
||||
* @param pathSegmentIndex the index of the path segment that contains the API version
|
||||
* @param includePath a {@link Predicate} that tests if the given path should be included
|
||||
* Constructor variant of {@link #PathApiVersionResolver(int)} with an
|
||||
* additional {@code Predicate<RequestPath>} to help determine whether
|
||||
* a given path is versioned (true) or not (false).
|
||||
*/
|
||||
public PathApiVersionResolver(int pathSegmentIndex, Predicate<RequestPath> includePath) {
|
||||
public PathApiVersionResolver(int pathSegmentIndex, Predicate<RequestPath> versionPathPredicate) {
|
||||
this(pathSegmentIndex);
|
||||
this.includePath = includePath;
|
||||
this.versionPathPredicate = versionPathPredicate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String resolveVersion(HttpServletRequest request) {
|
||||
if (!ServletRequestPathUtils.hasParsedRequestPath(request)) {
|
||||
throw new IllegalStateException("Expected parsed request path");
|
||||
}
|
||||
RequestPath path = ServletRequestPathUtils.getParsedRequestPath(request);
|
||||
if (this.includePath != null && !this.includePath.test(path)) {
|
||||
if (!isVersionedPath(path)) {
|
||||
return null;
|
||||
}
|
||||
int i = 0;
|
||||
@@ -82,4 +83,8 @@ public class PathApiVersionResolver implements ApiVersionResolver {
|
||||
throw new InvalidApiVersionException("No path segment at index " + this.pathSegmentIndex);
|
||||
}
|
||||
|
||||
private boolean isVersionedPath(RequestPath path) {
|
||||
return (this.versionPathPredicate == null || this.versionPathPredicate.test(path));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user