Repository navigation
fix(http): prefer the more specific route when templates collide - #14165
Conversation
Keep Router::$params sorted in descending order at registration, so when several route templates match the same path, match() picks the one with a static segment at the leftmost position where they differ instead of the one whose param position was registered first.
🟢 Tier S · Ready to merge
The router now sorts parameter positions during route registration so overlapping templates prefer a static segment at the first differing position. Unit coverage checks the selected route and extracted key in both registration orders. The API regression test now expects a successful string attribute update when its key is Latest changes: The newest commit updates the API alias-precedence regression from expecting a 404 to asserting a successful update with the correct key and default.
📂 Walkthrough · 3
Reviewed the commits since |
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Check that attributes/enum/:key wins over attributes/:key/relationship for .../attributes/enum/relationship in both registration orders.
…alias An attribute keyed relationship is now updated through its typed route, so PATCH .../string/relationship returns the string attribute instead of a not-found relationship keyed string.
Fixes #8760
PATCH /v1/databases/:databaseId/collections/:collectionId/attributes/enum/relationship(updating an enum attribute keyedrelationship) matches two templates: the typed routeattributes/enum/:keyand the legacy aliasattributes/:key/relationship, which the relationship update routes still register (TablesDB has the same pair oncolumns/:key/relationship).Router::match()tries candidate templates in the order their param positions were first registered, not by specificity, so the alias won and the request reachedupdateRelationshipwithkey=enum, failing with "Attribute with the requested key enum could not be found". This hits all 17 typed update routes on both APIs for an attribute or column keyedrelationship.preparePath()now keepsRouter::$paramssorted in descending order.combinations()already treats the first element as the lowest bit, so the first template that matches is the one with a static segment at the leftmost position where the candidates differ, which here is the canonical typed route. The sort runs when routes are registered, not per request; only the order in whichmatch()tries candidate templates changes. Sincepackages/httpis published asutopia-php/http, other consumers with overlapping param templates also get static-segment-first precedence instead of registration order.Checked against a local API container running main and then this branch, with the same requests:
PATCH .../attributes/enum/relationshipattribute_not_found(keyenum)[a,b,c]PATCH .../columns/enum/relationshipcolumn_not_foundUnchanged on both APIs: GET/DELETE
attributes/relationshipandcolumns/relationship(200/204); GET and PATCH for keysstring,enum,integerand indexes (200); PATCHenum/status, PATCHrelationship/rel, the legacy PATCHrel/relationship, and all DELETEs; canonical PATCHrelationship/emailfor a relationship keyedemail(200). A collision sweep over every registered HTTP route (app/init.phpplusgeneral.php) found 68 template pairs that can match the same path; exactly 34 winners changed, all of them the:key/relationshipalias pairs, and the other 34 resolve to the same route as before. Thepackages/httpunit tests pass before and after (136), Pint passes and PHPStan max passes on the file.RouterTestnow covers both registration orders ofattributes/enum/:keyvsattributes/:key/relationship, and the test fails without the sort.testUpdateRelationshipAttributeAliasPrecedencepinned the old resolution (404attribute_not_foundfor the relationship keyedstring); it now expects the string update keyedrelationship(200), and passes on the Legacy and TablesDB server, client and console suites.One behaviour change on an ambiguous path: the legacy alias
PATCH .../attributes/<type>/relationshipfor a relationship attribute whose key is exactly one of the 17 type names (for example a relationship keyedemail) now routes to the typed update route and returns 400general_argument_invalidinstead of updating the relationship. No routing rule can serve both readings of that path, and on main every SDK fails to update a non-relationship attribute keyedrelationship. Node SDK 20.0.0 and earlier use the legacy{key}/relationshippath; 24.0.0 and later userelationship/{key}. Cloud-only routes were not part of the sweep; on main their precedence between overlapping templates depended on registration order, and after this they follow the same static-first rule as CE.