Skip to content

Commit

Permalink
Fix route matching order mistake
Browse files Browse the repository at this point in the history
  • Loading branch information
oxsean committed Nov 25, 2024
1 parent 9753552 commit 73eafc1
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private RestFilter[] matchFilters(RestFilter[] filters, String path) {
if (size > 1) {
Collections.sort(matches);
}
if (matches.get(size - 1).getValue()) {
if (matches.get(0).getValue()) {
continue;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ public PathCondition match(HttpRequest request) {
}
}
if (matches != null) {
Collections.sort(matches);
if (matches.size() > 1) {
Collections.sort(matches);
}
Set<String> result = CollectionUtils.newLinkedHashSet(matches.size());
for (int i = 0, size = matches.size(); i < size; i++) {
result.add(matches.get(i).getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public int compareTo(PathExpression other) {
return result;
}
}
return otherSize - size;
return size - otherSize;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ public int compareTo(PathSegment other) {
return comparison;
}
}
int size = variables == null ? 0 : variables.size();
int otherSize = other.variables == null ? 0 : other.variables.size();
return otherSize - size;
int size = variables == null ? 99 : variables.size();
int otherSize = other.variables == null ? 99 : other.variables.size();
return size - otherSize;
}

public enum Type {
Expand All @@ -212,18 +212,18 @@ public enum Type {
LITERAL(1),
/**
* A wildcard segment.
* E.g.: 't?st*uv' and '/foo/&ast;/bar'
* E.g.: 't?st*uv'
*/
WILDCARD,
/**
* A wildcard matching suffix.
* Transient type used for parsing, will not be present in the PathExpression
* E.g.: '/foo/**' and '/**' and '/{*bar}'
* E.g.: '/foo/**' or '/**' or '/{*bar}'
*/
WILDCARD_TAIL,
/**
* A template variable segment.
* E.g.: '{foo}'
* E.g.: '{foo}' or '/foo/&ast;/bar'
*/
VARIABLE(10),
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
import org.apache.dubbo.remoting.http12.HttpRequest;
import org.apache.dubbo.remoting.http12.HttpResponse;
import org.apache.dubbo.remoting.http12.HttpResult;
import org.apache.dubbo.remoting.http12.HttpStatus;
import org.apache.dubbo.remoting.http12.exception.HttpStatusException;
import org.apache.dubbo.remoting.http12.exception.HttpResultPayloadException;
import org.apache.dubbo.remoting.http12.message.MediaType;
import org.apache.dubbo.remoting.http12.rest.OpenAPIRequest;
import org.apache.dubbo.rpc.RpcContext;
Expand Down Expand Up @@ -176,7 +175,6 @@ private List<OpenAPI> resolveOpenAPIs() {
@Override
public String getDocument(OpenAPIRequest request) {
String path = null;
HttpResult<?> result;
try {
request = Helper.formatRequest(request);

Expand All @@ -187,13 +185,14 @@ public String getDocument(OpenAPIRequest request) {

path = RequestUtils.getPathVariable(httpRequest, "path");
if (StringUtils.isEmpty(path)) {
throw HttpResult.found(PathUtils.join(httpRequest.path(), "swagger-ui/index.html")).toPayload();
String url = PathUtils.join(httpRequest.path(), "swagger-ui/index.html");
throw HttpResult.found(url).toPayload();
}

path = '/' + path;
List<Match<OpenAPIRequestHandler>> matches = tree.matchRelaxed(path);
if (matches.isEmpty()) {
throw new HttpStatusException(HttpStatus.NOT_FOUND.getCode());
throw HttpResult.notFound().toPayload();
}

Collections.sort(matches);
Expand All @@ -204,13 +203,14 @@ public String getDocument(OpenAPIRequest request) {
}
httpRequest.setAttribute(OpenAPIRequest.class.getName(), request);
httpRequest.setAttribute(RestConstants.URI_TEMPLATE_VARIABLES_ATTRIBUTE, match.getVariableMap());
result = match.getValue().handle(path, httpRequest, httpResponse);
throw match.getValue().handle(path, httpRequest, httpResponse).toPayload();
} catch (HttpResultPayloadException e) {
throw e;
} catch (Throwable t) {
Level level = ExceptionUtils.resolveLogLevel(t);
Level level = ExceptionUtils.resolveLogLevel(ExceptionUtils.unwrap(t));
LOG.log(level, "Failed to processing OpenAPI request {} for path: '{}'", request, path, t);
throw t;
}
throw result.toPayload();
}

private String handleDocument(OpenAPIRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private boolean isClassExcluded(Class<?> clazz) {
} else if (size > 1) {
Collections.sort(matches);
}
return matches.get(size - 1).getValue();
return matches.get(0).getValue();
}

public static void addPath(RadixTree<Boolean> tree, String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ class PathExpressionTest extends Specification {
'/one' | '/one' | 0
'/one' | '/two' | 0
'/one/{two}/three' | '/one/{t}/three' | 0
'/one/two' | '/one/{two}' | -1
'/one/{two}/three' | '/one/*/three' | -1
'/one/*/three' | '/one/**/three' | -1
'/one/two' | '/one' | -1
Expand Down

0 comments on commit 73eafc1

Please sign in to comment.