Skip to content

Commit

Permalink
Rest Controller wildcard registration (elastic#46487)
Browse files Browse the repository at this point in the history
Registering two different http methods on the same path using different
wildcard names would result in the last wildcard name being active only.
Now throw an exception instead.

Closes elastic#46482
  • Loading branch information
henningandersen authored Sep 9, 2019
1 parent c8fca03 commit 39c4b6b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
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

0 comments on commit 39c4b6b

Please sign in to comment.