Allow nested routes sharing the same path parameter matcher
When two routes share the same parameter at the same node position
(e.g. GET /restaurants/${id} and GET /restaurants/${id}/menu),
reuse the existing equivalent matcher instead of raising a conflict.
Non-equivalent matchers (different names, kinds, or glob patterns)
at the same node for the same method still raise ValueError.
This commit is contained in:
@@ -190,3 +190,29 @@ class AsgiTest(unittest.TestCase):
|
||||
'employee_id': 101325
|
||||
}, response)
|
||||
|
||||
@async_test
|
||||
async def test_nested_param_routes(self):
|
||||
app = KayaApp()
|
||||
|
||||
@app.GET('/restaurants/${id}')
|
||||
async def restaurant(ctx: HttpContext, id: str) -> None:
|
||||
await ctx.send_str(200, f"restaurant:{id}")
|
||||
|
||||
@app.GET('/restaurants/${id}/menu')
|
||||
async def menu(ctx: HttpContext, id: str) -> None:
|
||||
await ctx.send_str(200, f"menu:{id}")
|
||||
|
||||
transport = httpx.ASGITransport(app=app)
|
||||
|
||||
async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client:
|
||||
r = await client.get("/restaurants/42")
|
||||
self.assertEqual(200, r.status_code)
|
||||
self.assertEqual("restaurant:42", r.text)
|
||||
|
||||
r = await client.get("/restaurants/42/menu")
|
||||
self.assertEqual(200, r.status_code)
|
||||
self.assertEqual("menu:42", r.text)
|
||||
|
||||
r = await client.get("/restaurants/42/unknown")
|
||||
self.assertEqual(404, r.status_code)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user