Skip to content

fix(http): prefer the more specific route when templates collide - #14165

Merged
HarshMN2345 merged 5 commits into
mainfrom
fix/attribute-key-route-collision
Oct 6, 2026
Merged

HarshMN2345 merged 5 commits into
mainfrom
fix/attribute-key-route-collision

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Fixes #8760

PATCH /v1/databases/:databaseId/collections/:collectionId/attributes/enum/relationship (updating an enum attribute keyed relationship) matches two templates: the typed route attributes/enum/:key and the legacy alias attributes/:key/relationship, which the relationship update routes still register (TablesDB has the same pair on columns/: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 reached updateRelationship with key=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 keyed relationship.

preparePath() now keeps Router::$params sorted 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 which match() tries candidate templates changes. Since packages/http is published as utopia-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:

Request main this branch
Databases PATCH .../attributes/enum/relationship 404 attribute_not_found (key enum) 200, elements [a,b,c]
TablesDB PATCH .../columns/enum/relationship 404 column_not_found 200

Unchanged on both APIs: GET/DELETE attributes/relationship and columns/relationship (200/204); GET and PATCH for keys string, enum, integer and indexes (200); PATCH enum/status, PATCH relationship/rel, the legacy PATCH rel/relationship, and all DELETEs; canonical PATCH relationship/email for a relationship keyed email (200). A collision sweep over every registered HTTP route (app/init.php plus general.php) found 68 template pairs that can match the same path; exactly 34 winners changed, all of them the :key/relationship alias pairs, and the other 34 resolve to the same route as before. The packages/http unit tests pass before and after (136), Pint passes and PHPStan max passes on the file. RouterTest now covers both registration orders of attributes/enum/:key vs attributes/:key/relationship, and the test fails without the sort. testUpdateRelationshipAttributeAliasPrecedence pinned the old resolution (404 attribute_not_found for the relationship keyed string); it now expects the string update keyed relationship (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>/relationship for a relationship attribute whose key is exactly one of the 17 type names (for example a relationship keyed email) now routes to the typed update route and returns 400 general_argument_invalid instead 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 keyed relationship. Node SDK 20.0.0 and earlier use the legacy {key}/relationship path; 24.0.0 and later use relationship/{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.

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.
@ghost

ghost commented Oct 6, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The new assertions exercise the corrected API behavior, and no defects were found in the incremental changes.

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 relationship, checking the returned key and default.

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.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
packages/http/src/Router.php Sort parameter positions during registration to establish static-first template precedence.
packages/http/tests/RouterTest.php Test overlapping enum and relationship templates in both registration orders.
tests/e2e/Services/Databases/DatabasesBase.php Expect the typed string update for key relationship to succeed and return the updated default.

Reviewed the commits since bcb8e37 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

Comment thread packages/http/src/Router.php Resolved
@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/attribute-key-route-collision (after).

Metric Before After Change
🚀 Requests/sec 157.8 167.19 🟢 +5.9%
⏱️ Latency P50 107.46 ms 103.06 ms ⚪ -4.1%
⏱️ Latency P95 267.97 ms 245.62 ms 🟢 -8.3%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 103.06 245.62 10,488 167.19 -22.36
Account 196.7 368.56 552 9.13 -52.81
TablesDB 99.78 195.58 5,704 92.53 -10.1
Storage 92.82 207.55 2,760 46.83 -19.88
Functions 143.46 298.53 1,472 25.76 -29.77

Top API waits (after)

API request Max wait (ms)
account.name.update 659.52
functions.variables.update 575.59
tablesdb.rows.create 510.26
functions.delete 453.53
account.prefs.update 453.15

HarshMN2345 and others added 3 commits October 6, 2026 16:42
Check that attributes/enum/:key wins over attributes/:key/relationship for .../attributes/enum/relationship in both registration orders.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

…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.
@HarshMN2345
HarshMN2345 merged commit f812516 into main Oct 6, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Bug Report: Enum attribute named 'relationship' cannot be edited

2 participants