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

Rest Controller wildcard registration #46487

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
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ private TrieNode(String key, T value, String wildcard) {

private void updateKeyWithNamedWildcard(String key) {
this.key = key;
namedWildcard = key.substring(key.indexOf('{') + 1, key.indexOf('}'));
String newNamedWildcard = key.substring(key.indexOf('{') + 1, key.indexOf('}'));
if (namedWildcard != null && newNamedWildcard.equals(namedWildcard) == false) {
throw new IllegalArgumentException("Trying to use conflicting wildcard names for same path: "
+ namedWildcard + " and " + newNamedWildcard);
}
namedWildcard = newNamedWildcard;
}

private void addInnerChild(String key, TrieNode child) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.common.path;

import org.elasticsearch.common.path.PathTrie.TrieMatchingMode;
import org.elasticsearch.rest.RestUtils;
import org.elasticsearch.test.ESTestCase;

Expand All @@ -29,8 +30,6 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;

import org.elasticsearch.common.path.PathTrie.TrieMatchingMode;

public class PathTrieTests extends ESTestCase {

public static final PathTrie.Decoder NO_DECODER = new PathTrie.Decoder() {
Expand Down Expand Up @@ -122,13 +121,13 @@ public void testWildcardMatchingModes() {
PathTrie<String> trie = new PathTrie<>(NO_DECODER);
trie.insert("{testA}", "test1");
trie.insert("{testA}/{testB}", "test2");
trie.insert("a/{testA}", "test3");
trie.insert("a/{testB}", "test3");
trie.insert("{testA}/b", "test4");
trie.insert("{testA}/b/c", "test5");
trie.insert("a/{testB}/c", "test6");
trie.insert("a/b/{testC}", "test7");
trie.insert("{testA}/b/{testB}", "test8");
trie.insert("x/{testA}/z", "test9");
trie.insert("x/{testB}/z", "test9");
trie.insert("{testA}/{testB}/{testC}", "test10");

Map<String, String> params = new HashMap<>();
Expand Down Expand Up @@ -196,7 +195,7 @@ public void testExplicitMatchingMode() {
trie.insert("a", "test2");
trie.insert("{testA}/{testB}", "test3");
trie.insert("a/{testB}", "test4");
trie.insert("{testB}/b", "test5");
trie.insert("{testA}/b", "test5");
trie.insert("a/b", "test6");
trie.insert("{testA}/b/{testB}", "test7");
trie.insert("x/{testA}/z", "test8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -176,6 +177,24 @@ public void testRegisterWithDeprecatedHandler() {
verify(controller).registerAsDeprecatedHandler(deprecatedMethod, deprecatedPath, handler, deprecationMessage, logger);
}

public void testRegisterSecondMethodWithDifferentNamedWildcard() {
final RestController restController = new RestController(null, null, null, circuitBreakerService, usageService);

RestRequest.Method firstMethod = randomFrom(RestRequest.Method.values());
RestRequest.Method secondMethod =
randomFrom(Arrays.stream(RestRequest.Method.values()).filter(m -> m != firstMethod).collect(Collectors.toList()));

final String path = "/_" + randomAlphaOfLengthBetween(1, 6);

RestHandler handler = mock(RestHandler.class);
restController.registerHandler(firstMethod, path + "/{wildcard1}", handler);

IllegalArgumentException exception = expectThrows(IllegalArgumentException.class,
() -> restController.registerHandler(secondMethod, path + "/{wildcard2}", handler));

assertThat(exception.getMessage(), equalTo("Trying to use conflicting wildcard names for same path: wildcard1 and wildcard2"));
}

public void testRestHandlerWrapper() throws Exception {
AtomicBoolean handlerCalled = new AtomicBoolean(false);
AtomicBoolean wrapperCalled = new AtomicBoolean(false);
Expand Down