CCCLL opened a new issue, #16561:
URL: https://github.com/apache/dubbo/issues/16561
### Apache Dubbo Component
Java SDK (apache/dubbo)
### Dubbo Version
Dubbo Java 3.3.0 to 3.3.6 and the `3.3` branch (1afcdf7883), OpenJDK 21,
macOS.
### Steps to reproduce this issue
A Triple REST service with a `?` in its mapping paths:
```java
@Mapping("/")
public interface Api {
@Mapping("/pages/t?st.html")
String page();
@Mapping("/orders/{id:\\d+(?:-\\d+)?}")
String order(@Param(value = "id", type = ParamType.PathVariable) String
id);
}
```
With `org.apache.dubbo.rpc.protocol.tri.rest` at DEBUG, the mappings are
registered as:
```
Register rest mapping: '/pages/t' -> mapping=RequestMapping{name='...#page',
path=PathCondition{paths=[/pages/t?st.html]}}, ...
Register rest mapping: '/orders/{id:\d+(' ->
mapping=RequestMapping{name='...#order',
path=PathCondition{paths=[/orders/{id:\d+(?:-\d+)?}]}}, ...
```
```
GET /pages/test.html -> 404 {"message":"Invoker not found","status":"404"}
GET /pages/t -> 200 "page"
GET /orders/12 -> 404 {"message":"Invoker not found","status":"404"}
GET /orders/12-3 -> 404 {"message":"Invoker not found","status":"404"}
```
No error or warning is logged at startup. Same result with the released
3.3.0 and 3.3.6 jars.
### What you expected to happen
`/pages/test.html` returns `page`, `/pages/t` returns 404, and `/orders/12`
and `/orders/12-3` return the id.
The Triple REST manual lists `?` ("Matches a single character") among the
supported patterns and gives `/pages/t?st.html` as an example. Spring's
`PathPattern` and `AntPathMatcher` both match `/pages/test.html` with it.
`PathExpressionTest` also has cases with `?` (`/resources/ima?e.png`,
`/a?cd/ef`, `{param:o?e}`), but they call `PathParser`/`PathExpression`
directly.
When a mapping is registered, `PathCondition.getExpressions()` first
normalizes the pattern with `PathUtils.normalize(contextPath, path)`.
`normalize` stops at the first `?` (and `#`), because it is also used for
request URIs: `DefaultRequestMappingRegistry#lookup` passes `request.uri()`,
query string included. So everything from the `?` on is dropped, and a pattern
that is cut inside `{...}` is no longer recognized as a pattern and is
registered as a literal path. `RadixTree.addPath(String)`, which registers the
patterns of `RestFilter`/`RestExtension`, normalizes the same way. This has
been the case since 3.3.0 (#13607).
### Anything else
I have a change that normalizes patterns with a variant of `normalize` that
keeps `?` (used by `PathCondition` and `RadixTree.addPath(String)`; request
URIs are still cut at `?`). With it, the example above works. But I am not
sending it as a PR yet, because two things need a decision first:
1. **`PathParser` cannot handle a `{variable}` and a wildcard in the same
segment.** A variable followed by `*` already fails today:
`@Mapping("/files/{name}*.txt")` makes the module fail to start on 3.3.6 with
`NullPointerException: Cannot invoke "java.util.List.get(int)" because
"this.variables" is null` (`PathSegment.getVariable` from
`PathParser.transformSegments`), while Spring matches `/files/a1.txt` with
`name=a1`. The same happens with `?` once it is no longer cut off: patterns
such as `/{a}?b` or `/users/{id}?fields={f}` currently start (as `/{a}` and
`/users/{id}`), but would then fail at startup. The other order, `/?{x}`,
parses but does not match `/ab`, which Spring matches with `x=b`. So keeping
`?` needs a `PathParser` fix for mixed segments first, or together with it.
2. **Mapping paths that contain a query part.** A mapping such as
`/users?type=1` is registered today as `/users` and works. If `?` is kept as a
wildcard, it no longer matches `/users` (Spring does not match it either; it
matches `/usersXtype=1`), and nothing is logged. Should such mappings change
silently like that, should Dubbo log a warning for them, or should they keep
working?
Once the direction is clear, I can open a PR for the `?` handling and the
`PathParser` part.
I compared how Dubbo registers 39 patterns containing `?` (as a mapping and
as a filter pattern) with Spring's `PathPattern` and `AntPathMatcher` on 255
paths: the two Spring matchers agree on every case. On 3.3.6 Dubbo differs from
them in 252 cases per registration path. With the change above, 9 of the
patterns (the `{x}?` forms) fail with the `NullPointerException`, and the
remaining 45 differences are all `?{x}` patterns.
### Are you willing to submit a pull request to fix on your own?
- [x] Yes I am willing to submit a pull request on my own!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]