Skip to content

Commit ad86210

Browse files
authored
python fastapi openapi docs fix (#2957)
<!-- CURSOR_SUMMARY --> > [!NOTE] > Ensure FastAPI WebApps respect mount paths by setting ASGI root_path and add e2e tests validating OpenAPI JSON and Swagger UI endpoints. > > - **Python wrapper (`consumption_runner.py`)**: > - Set ASGI scope `root_path` to the FastAPI WebApp `mount_path` to correctly serve mounted routes (e.g., OpenAPI/docs). > - **E2E Tests (`apps/framework-cli-e2e/test/templates.test.ts`)**: > - Add FastAPI WebApp tests verifying OpenAPI JSON (`/fastapi/openapi.json`) structure and presence of `/health` path. > - Add test for interactive docs endpoint (`/fastapi/docs`). > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit d747c05. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 22fcd3d commit ad86210

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

apps/framework-cli-e2e/test/templates.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,42 @@ const createTemplateTestSuite = (config: TemplateTestConfig) => {
12631263
);
12641264
});
12651265

1266+
it("should serve OpenAPI documentation for FastAPI WebApp", async function () {
1267+
this.timeout(TIMEOUTS.TEST_SETUP_MS);
1268+
1269+
// Test OpenAPI JSON schema endpoint
1270+
await verifyWebAppEndpoint("/fastapi/openapi.json", 200, (json) => {
1271+
if (!json.openapi) {
1272+
throw new Error(
1273+
"Expected OpenAPI schema to have 'openapi' field",
1274+
);
1275+
}
1276+
if (!json.info) {
1277+
throw new Error("Expected OpenAPI schema to have 'info' field");
1278+
}
1279+
if (!json.paths) {
1280+
throw new Error("Expected OpenAPI schema to have 'paths' field");
1281+
}
1282+
// Verify that paths include the mount_path prefix
1283+
const paths = Object.keys(json.paths);
1284+
if (paths.length === 0) {
1285+
throw new Error(
1286+
"Expected OpenAPI schema to have at least one path",
1287+
);
1288+
}
1289+
// Check that at least one path includes /health (should be /fastapi/health or just /health)
1290+
const hasHealthPath = paths.some((p) => p.includes("/health"));
1291+
if (!hasHealthPath) {
1292+
throw new Error(
1293+
`Expected OpenAPI schema to include /health path. Found paths: ${paths.join(", ")}`,
1294+
);
1295+
}
1296+
});
1297+
1298+
// Test interactive docs endpoint (Swagger UI)
1299+
await verifyWebAppEndpoint("/fastapi/docs", 200, undefined);
1300+
});
1301+
12661302
it("should handle multiple WebApp endpoints independently (PY)", async function () {
12671303
this.timeout(TIMEOUTS.TEST_SETUP_MS);
12681304

apps/framework-cli/src/framework/python/wrappers/consumption_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def handle_request(self):
242242
'scheme': 'http',
243243
'path': proxied_path,
244244
'query_string': parsed_path.query.encode() if parsed_path.query else b'',
245-
'root_path': '',
245+
'root_path': normalized_mount,
246246
'headers': [(k.lower().encode(), v.encode()) for k, v in self.headers.items()],
247247
'server': (server_name, server_port),
248248
'client': self.client_address,

0 commit comments

Comments
 (0)