aws / aws/chalice

Feature Request: Expose matched URL pattern on Request object

Open
#1,483 3 comments 0 reactions 0 assignees View on GitHub
feature-request
Dominant language
Python
Stars
11.1k
Forks
1k
Avg merge
1d 22h
Merged PRs (30d)
2

Description

Hello,

I'm building a REST API with Chalice and I'm using [OpenAPI 3](https://swagger.io/specification/) to document the API and validate incoming requests. In order to find the right validations to enforce, it's necessary to compare an incoming request against the specification document, and match the right URL pattern. Today, this is hard to do with Chalice because the [Request object](https://github.com/aws/chalice/blob/master/chalice/app.py#L380) doesn't expose the URL pattern of the route handling that request.

The Chalice app does have this information (because it needs to find the right decorated function to call) but it's hard to access from inside the function.

I'm imagining something like this:

```python
@app.route('/hello/{name}')
def hello(name):
return {'pattern': app.current_request.matched_url_pattern,
# For extra credit (I know path is in the context already):
'path': app.current_request.path}

# GET /hello/brendan would return:
# {
# 'pattern': '/hello/{name}',
# 'path': '/hello/brendan'
# }

```
(obviously in this silly trivial case I could just assign `'/hello/{name}'` to a variable and use it both places)

I happen to be using [this library](https://github.com/grktsh/python-oas) but the feature I'm asking for would be library agnostic.

Thanks for all your work on Chalice!

Contributor guide

Open the contributing guide

Research direction

Start in chalice/app.py around the Request object referenced in the issue, then trace how the Chalice app selects the decorated function for a route. Expose the matched URL pattern through app.current_request so the example can return '/hello/{name}' for /hello/brendan; verify the behavior with the project's request and routing tests if available.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.