-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Improve REST error handling when endpoint does not support HTTP verb, add OPTIONS support #24437
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
Conversation
Also improved OPTIONS http method handling to better conform with the http spec.
Simple addition to surface the RestResponse object so we can run tests against it (see issue elastic#15335).
Randomizing unit test for enhanced REST response error handling. See issue elastic#15335 for more details.
Constructor added to support RestController test cases.
# Conflicts: # core/src/main/java/org/elasticsearch/rest/RestController.java # test/framework/src/main/java/org/elasticsearch/test/rest/FakeRestRequest .java
Refactored executeHandler to prioritize explicit path matches. See elastic#15335 for more information.
In this test, an OPTIONS method request is valid, and should not return a 405 error.
Then TrieMatchingMode can be package private and not leak into RestController
@s1monw okay! I've pushed two commits that make some drastic changes to this PR: The first is a change from The second is a change in |
I accidentally removed this :X
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took another look and I like this. Yet, I think we need to make the change that you did here first in a separate PR and then do the actual change based on that. Is this possible? I'd love to make is smaller so reviews are more effective.
this.path = path; | ||
this.trie = trie; | ||
this.paramSupplier = paramSupplier; | ||
modes.add(TrieMatchingMode.EXPLICIT_NODES_ONLY); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just use Arrays.asList when this is initializaed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The modes need to go in a specific order, this ensures that even if someone changes the order in the ENUM definition the matching will still work correctly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, but I see what you're saying, you're just saying for contructing it, sure I'll change that
|
||
@Override | ||
public boolean hasNext() { | ||
if (modes.isEmpty() == false) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return modex.isEmpty() == false
?
* @param path Path to handle (e.g., "/{index}/{type}/_bulk") | ||
* @param handler The handler to actually execute | ||
*/ | ||
public void registerHandler(RestRequest.Method singleMethod, String path, RestHandler handler) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did you add this method and why don't you use the registerHandler
method above instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a compatibility layer for all the Rest*
actions, if I used only the one above I would have to change every action's registerHandler
method call (which I might do, but in a subsequent PR)
} | ||
|
||
// Loop through all possible handlers, attempting to dispatch the request | ||
Iterator<MethodHandlers> allHandlers = getAllHandlers(request); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm can't we do for (MethodHandlers handler : ((Iterable<MethodHandlers>) this::getAllHandlers))
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't cast the Iterator to Iterable (I tried it and got class cast exceptions), is there a different way to get an Iterable out of an Iterator?
…hods This changes `PathTrie` and `RestController` to use a single `PathTrie` for all endpoints, it also allows retrieving the endpoints' supported HTTP methods more easily. This is a spin-off and prerequisite of elastic#24437
I opened #25459 as a spin-off encapsulating the |
…hods (#25459) * Refactor PathTrie and RestController to use a single trie for all methods This changes `PathTrie` and `RestController` to use a single `PathTrie` for all endpoints, it also allows retrieving the endpoints' supported HTTP methods more easily. This is a spin-off and prerequisite of #24437 * Use EnumSet instead of multiple if conditions * Make MethodHandlers package-private and final * Remove duplicate registerHandler method * Remove public modifier
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks good to me. Yet, I think we should have a real REST test for this somehow? WDYT @dakrone we can add a test REST handler in a smoketest qa env where we can add a rest spec that we don't implement, something along those lines? This can be a folllowup IMO
// Get the map of matching handlers for a request, for the full set of HTTP methods. | ||
final Set<RestRequest.Method> validMethodSet = getValidHandlerMethodSet(request); | ||
if (validMethodSet.size() > 0 | ||
&& !validMethodSet.contains(request.method()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know its controversial but can we use == false
?
// Not Allowed error. | ||
handleUnsupportedHttpMethod(request, channel, validMethodSet); | ||
requestHandled = true; | ||
} else if (!validMethodSet.contains(request.method()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here ^^
* master: Remove deprecated created and found from index, delete and bulk (elastic#25516) fix testEnsureVersionCompatibility for 5.5.0 release fix Version.v6_0_0 min compatibility version to 5.5.0 Add bwc indices for 5.5.0 Add v5_5_1 constant [DOCS] revise high level client Search Scroll API docs (elastic#25599) Improve REST error handling when endpoint does not support HTTP verb, add OPTIONS support (elastic#24437) Avoid SecurityException in repository-S3 on DefaultS3OutputStream.flush() (elastic#25254) [Tests] Add tests for CompletionSuggestionBuilder#build() (elastic#25575)
* master: (42 commits) Harden global checkpoint tracker Remove deprecated created and found from index, delete and bulk (elastic#25516) fix testEnsureVersionCompatibility for 5.5.0 release fix Version.v6_0_0 min compatibility version to 5.5.0 Add bwc indices for 5.5.0 Add v5_5_1 constant [DOCS] revise high level client Search Scroll API docs (elastic#25599) Improve REST error handling when endpoint does not support HTTP verb, add OPTIONS support (elastic#24437) Avoid SecurityException in repository-S3 on DefaultS3OutputStream.flush() (elastic#25254) [Tests] Add tests for CompletionSuggestionBuilder#build() (elastic#25575) Enable cross-setting validation [Docs] Fix typo in bootstrap-checks.asciidoc (elastic#25597) Index ids in binary form. (elastic#25352) bwc checkout should fetch from all remotes IndexingIT should check for global checkpoints regardless of master version [Tests] Add tests for PhraseSuggestionBuilder#build() (elastic#25571) Remove unused class MinimalMap (elastic#25590) [Docs] Document Scroll API for Java High Level REST Client (elastic#25554) Disable date field mapping changing (elastic#25285) Allow BWC Testing against a specific branch (elastic#25510) ...
This is a follow-up of #17916 from @jbertouch's work brought up to date with the latest master, here's a subset of original description:
First pass at closing #15335. The new behavior for the
RestController#executeHandler
method is as follows:It allows things like the following:
Where you can see the
HTTP/1.1 405 Method Not Allowed
response andAllow: GET
header.It also adds support for
OPTIONS
: