Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support wildcard in path template. #3559

Merged
merged 4 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/se/webserver/03_routing.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///////////////////////////////////////////////////////////////////////////////

Copyright (c) 2018, 2020 Oracle and/or its affiliates.
Copyright (c) 2018, 2021 Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -85,6 +85,7 @@ You can use *path pattern* instead of _path_ with the following syntax:
* `/foo/{+var}` - Convenience shortcut for {var:.+}. A matcher is not a true URI template (as defined by RFC) but this convenience is in sync with the Apiary templates
* `/foo/{+}` - Convenience shortcut for unnamed segment with regular expression {:.+}
* `/foo[/bar]` - An optional block, which translates to the `/foo(/bar)?` regular expression
* `/*` or `/foo*` - `*` Wildcard character can be matched with any number of characters.


IMPORTANT: Path (matcher) routing is *exact*. For example, a `/foo/bar` request is *not* routed to `.post('/foo', ...)`.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,6 +54,8 @@
* <td headers="description">A convenience shortcut for {@code /foo/{:.+}}.</td></tr>
* <tr><td headers="construct">{@code /foo[/bar]}</td>
* <td headers="description">A optional section. Translated to regexp: {@code /foo(/bar)?}</td></tr>
* <tr><td headers="construct">{@code /* or /foo*}</td>
* <td headers="description">Wildcard character can be matched with any number of characters.</td></tr>
* </table>
*/
public interface PathMatcher {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates.
* Copyright (c) 2018, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -126,6 +126,10 @@ static PathMatcher compile(CharSequence pattern) {
paramCounter++;
}
break;
case '*':
klustria marked this conversation as resolved.
Show resolved Hide resolved
isRegexp = true;
regexp.append(".*?");
break;
default:
shouldContinue = false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,6 @@
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
Expand All @@ -32,7 +31,6 @@
/**
* The PathTemplateTest.
*/
@Disabled
public class PathPatternTest {

private String normalize(String path) {
Expand Down Expand Up @@ -229,6 +227,22 @@ public void testGreedyParams() throws Exception {
assertMatchWithParams("/foo/bar/baz/xxx", "/foo/{+}/xxx");
}

@Test
public void testWildCard() throws Exception {
assertMatch("/foo/bar", "/foo*");
assertMatch("/foo/bar", "/foo/*");
assertMatch("/foo/bar", "/foo/ba*");
assertMatch("/foo/bar", "/foo[/*]");
assertMatch("/foo/bar", "/foo[/ba*]");
assertMatch("/foo/bar/baz", "/foo/*");
assertMatch("/foo/bar/baz", "/foo/ba*");
assertMatch("/foo/bar/baz", "/foo/*/*");
assertMatch("/foo/bar/baz", "/foo/*/b*");
assertNotMatch("/foobar", "/foo/*");
assertMatchWithParams("/foo/bar/baz", "/foo[/{var}]/*", "var", "bar");
assertMatchWithParams("/foo/bar/baz", "/foo/*[/{var}]", "var", "baz");
}

@Test
public void testOptionals() throws Exception {
assertMatch("/foo/bar", "/foo[/bar]");
Expand Down