-
Notifications
You must be signed in to change notification settings - Fork 0
feat(django): support string-prefixed route literals and re_path() #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| from django.urls import path, re_path | ||
| from . import views | ||
|
|
||
| # Mixes the idiomatic raw-string `r"..."` route form with plain-quoted routes | ||
| # and an re_path() regex route, so the extractor is exercised against every | ||
| # common Python string-literal style. | ||
| urlpatterns = [ | ||
| path(r"posts/", views.post_list), | ||
| path(r"posts/new/", views.post_create), | ||
| path("drafts/", views.draft_list), | ||
| re_path(r"^posts/(?P<pk>[0-9]+)/$", views.post_detail), | ||
| ] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from rest_framework.decorators import api_view | ||
|
|
||
|
|
||
| @api_view(["GET", "POST"]) | ||
| def feed(request): | ||
| return None |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from django.contrib import admin | ||
| from django.urls import path, include | ||
| from . import views | ||
|
|
||
| # Root URLconf: plain-quoted routes plus an include() into the blog app. | ||
| urlpatterns = [ | ||
| path("admin/", admin.site.urls), | ||
| path("", views.home), | ||
| path("blog/", include("blog.urls")), | ||
| ] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # Django project marker (used for framework detection only). |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { resolve } from "path"; | ||
| import { django } from "./django.ts"; | ||
| import { createScanContext } from "../scan-context.ts"; | ||
|
|
||
| const FIXTURE = resolve(import.meta.dir, "../../scripts/fixtures/django-urls"); | ||
|
|
||
| function extract(fixturePath: string = FIXTURE) { | ||
| const ctx = createScanContext(fixturePath); | ||
| return django.extract(ctx); | ||
| } | ||
|
|
||
| describe("django url extraction", () => { | ||
| const endpoints = extract(); | ||
| const byPath = (p: string) => endpoints.find((e) => e.path === p); | ||
|
|
||
| test('extracts routes declared with string-prefixed literals (r"...")', () => { | ||
| // blog.urls is mounted under "blog/" via include(), so routes are prefixed. | ||
| expect(byPath("/blog/posts")).toBeDefined(); | ||
| expect(byPath("/blog/posts/new")).toBeDefined(); | ||
| }); | ||
|
|
||
| test("extracts plain-quoted routes", () => { | ||
| expect(byPath("/admin")).toBeDefined(); | ||
| expect(byPath("/blog/drafts")).toBeDefined(); | ||
| }); | ||
|
|
||
| test("extracts re_path() regex routes", () => { | ||
| expect(endpoints.find((e) => e.handler === "post_detail")).toBeDefined(); | ||
| }); | ||
|
|
||
| test("applies include() prefix to mounted app routes", () => { | ||
| // Unprefixed it would be "/posts"; the include mounts blog.urls at /blog. | ||
| expect(byPath("/posts")).toBeUndefined(); | ||
| expect(byPath("/blog/posts")).toBeDefined(); | ||
| }); | ||
|
|
||
| test("extracts @api_view endpoints with their methods", () => { | ||
| const feed = endpoints.filter((e) => e.handler === "feed"); | ||
| expect(feed.map((e) => e.method).sort()).toEqual(["GET", "POST"]); | ||
| }); | ||
|
|
||
| test("captures every path()/re_path() route declared (recall guard)", () => { | ||
| for (const p of [ | ||
| "/admin", | ||
| "/blog/posts", | ||
| "/blog/posts/new", | ||
| "/blog/drafts", | ||
| ]) { | ||
| expect(byPath(p)).toBeDefined(); | ||
| } | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re_path regex stored as URL
Medium Severity
directPathRenow matchesre_path()with the same pipeline aspath(), so the first capture is the full regex string (anchors, groups, quantifiers), not a URL segment. Afterinclude()prefixing andnormalizePath, the reported endpointpathis a regex literal rather than a routable URL shape consumers expect for discovery and scanning.Reviewed by Cursor Bugbot for commit 46b3e63. Configure here.