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

[BUG] Rest Requests do not properly parse named wildcards #45

Closed
dbwiddis opened this issue Sep 17, 2023 · 0 comments · Fixed by #75
Closed

[BUG] Rest Requests do not properly parse named wildcards #45

dbwiddis opened this issue Sep 17, 2023 · 0 comments · Fixed by #75
Assignees
Labels
bug Something isn't working hacktoberfest Welcome Hacktoberfest participants!

Comments

@dbwiddis
Copy link
Member

dbwiddis commented Sep 17, 2023

What is the bug?

A Rest Handler route using a named param does not match.

How can one reproduce the bug?

Add a route which includes a named param. For example

def routes(self) -> list[NamedRoute]:
    return [NamedRoute(method=RestMethod.GET, path="/hello", unique_name="greeting"),
            NamedRoute(method=RestMethod.GET, path="/hello/{name}", unique_name="personal_greeting")]

Then send a Rest Request such as /hello/Tester.

This results in

ERROR:root:'GET /hello/Tester'
Traceback (most recent call last):
  File "/Users/danielwiddis/git/opensearch-sdk-py/samples/hello/hello.py", line 48, in handle_connection
    output = request_handlers.handle(request, input)
  File "/Users/danielwiddis/git/opensearch-sdk-py/src/opensearch_sdk_py/actions/request_handlers.py", line 35, in handle
    return handler.handle(request, input) if handler else None
  File "/Users/danielwiddis/git/opensearch-sdk-py/src/opensearch_sdk_py/actions/internal/extension_rest_request_handler.py", line 31, in handle
    response = ExtensionRestHandlers().handle(route, extension_rest_request)
  File "/Users/danielwiddis/git/opensearch-sdk-py/src/opensearch_sdk_py/rest/extension_rest_handlers.py", line 45, in handle
    return self[route].handle_request(request)
KeyError: 'GET /hello/Tester'

What is the expected behavior?

The Rest Request is sent to the correct handler, and the params map is populated with the named wildcard key and value (in this case, params["name"] = "Tester" and this code would then work:

consumed_params = list[str]()
if rest_request.params["name"]:
    consumed_params.append("name")
    response_bytes = bytes("Hello " + rest_request.params["name"] + "!", "utf-8") + b"\x20\xf0\x9f\x91\x8b"
else:
    response_bytes = bytes("Hello from Python!", "utf-8") + b"\x20\xf0\x9f\x91\x8b"
return ExtensionRestResponse(RestStatus.OK, response_bytes, ExtensionRestResponse.TEXT_CONTENT_TYPE, consumed_params=consumed_params)

Do you have any additional context?

This relates to #35 in that we need to track wildcards in the RestHandlers class and keep track of their original names. Matching incoming strings will be more complex than a dictionary key. I expect glob matching will be the most straightforward solution, or instead of using a regular dictionary use a Trie like OpenSearch's PathTrie

@dbwiddis dbwiddis added bug Something isn't working untriaged Issues that require attention from the maintainers. hacktoberfest Welcome Hacktoberfest participants! and removed untriaged Issues that require attention from the maintainers. labels Sep 17, 2023
@dbwiddis dbwiddis changed the title [BUG] Rest Requests do not properly parse named params [BUG] Rest Requests do not properly parse named wildcards Sep 17, 2023
This was referenced Oct 28, 2023
@dbwiddis dbwiddis self-assigned this Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working hacktoberfest Welcome Hacktoberfest participants!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant