feat(python): support FastAPI APIRouter prefixes#81
Closed
AsishKumarDalal wants to merge 1 commit into
Closed
Conversation
Contributor
|
Thanks @AsishKumarDalal. I landed this on main in fb46ba9 after adding the maintainer-side changelog entry and regression coverage. I could not push the fixups back to the PR branch because maintainer edits are disabled on the fork, so I closed this PR after landing the equivalent maintainer commit. For future PRs, enabling Allow edits by maintainers lets us preserve the normal PR update/merge path. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
When mapping FastAPI projects,
clawpatchpreviously only mapped the route paths specified directly inside route decorators (e.g.,@router.get("/items")). If theAPIRouterwas initialized with a path prefix (e.g.,router = APIRouter(prefix="/api/v1")), the generated seeds would incorrectly display the route path as/itemsinstead of the fully resolved/api/v1/itemspath.This Pull Request resolves this limitation by introducing robust static analysis to extract, parse, and propagate
APIRouterprefix declarations, seamlessly combining them with decorators to generate 100% accurate FastAPI routes.Key Changes
src/mappers/python.ts):parseFastApiRouterPrefixes(source)which matchesAPIRouter(initializations and identifies the variable name it is assigned to.findBalancedParenthesis) and top-level Python argument splitter (splitTopLevelPythonArgs) to securely extract theprefixvalue, even when other arguments are present or lines are formatted differently.combineFastApiPaths):/and has no duplicate internal slashes (e.g.,/api/+/itemsmerges correctly to/api/items).This high-value contribution drastically improves the quality and correctness of the Python mapper for modern FastAPI codebases.