Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/core/routerResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,25 @@ async function buildRouterGraphInternal(
}
}

// Factory function: if the entrypoint variable (e.g. "app" from "main:app")
// is assigned via a factory function (`app = create_app()`) rather than a direct
// FastAPI() constructor call, static analysis can't determine the type. If routes
// are decorated with @app.get(...) etc. though, we know it must be a FastAPI instance.
if (
!appRouter &&
targetVariable &&
analysis.routes.some((r) => r.owner === targetVariable)
) {
appRouter = {
variableName: targetVariable,
type: "FastAPI",
prefix: "",
tags: [],
line: 0,
column: 0,
}
}

if (!appRouter || !analysis) {
return null
}
Expand Down
32 changes: 32 additions & 0 deletions src/test/core/routerResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,5 +538,37 @@ suite("routerResolver", () => {
assert.strictEqual(result.children[0].router.prefix, "/users")
assert.ok(result.children[0].router.routes.length >= 2)
})

test("infers FastAPI app when assigned via factory function (app = get_fastapi_app())", async () => {
const result = await buildRouterGraph(
fixtures.factoryFunc.mainPy,
parser,
fixtures.factoryFunc.root,
nodeFileSystem,
"app",
)

assert.ok(
result,
"Should find app even when assigned via factory function",
)
assert.strictEqual(result.type, "FastAPI")
assert.strictEqual(result.variableName, "app")
assert.strictEqual(result.routes.length, 2)
const paths = result.routes.map((r) => r.path)
assert.ok(paths.includes("/1"))
assert.ok(paths.includes("/2"))
})

test("returns null without targetVariable when app is a factory function", async () => {
const result = await buildRouterGraph(
fixtures.factoryFunc.mainPy,
parser,
fixtures.factoryFunc.root,
nodeFileSystem,
)

assert.strictEqual(result, null)
})
})
})
5 changes: 5 additions & 0 deletions src/test/fixtures/factory-func/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from fastapi import FastAPI


def get_fastapi_app() -> FastAPI:
return FastAPI()
13 changes: 13 additions & 0 deletions src/test/fixtures/factory-func/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from app import get_fastapi_app

app = get_fastapi_app()


@app.get("/1")
def one():
return "Route one"


@app.get("/2")
def two():
return "Route two"
4 changes: 4 additions & 0 deletions src/test/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ export const fixtures = {
projectRoot: uri(join(fixturesPath, "monorepo", "service")),
mainPy: uri(join(fixturesPath, "monorepo", "service", "myapp", "main.py")),
},
factoryFunc: {
root: uri(join(fixturesPath, "factory-func")),
mainPy: uri(join(fixturesPath, "factory-func", "main.py")),
},
}

/**
Expand Down
Loading