Skip to content

Add failing test to ensure OpenAPI does not execute lifespan#1167

Open
Riddhi8077 wants to merge 1 commit intoAOSSIE-Org:mainfrom
Riddhi8077:fix-openapi-lifespan
Open

Add failing test to ensure OpenAPI does not execute lifespan#1167
Riddhi8077 wants to merge 1 commit intoAOSSIE-Org:mainfrom
Riddhi8077:fix-openapi-lifespan

Conversation

@Riddhi8077
Copy link

@Riddhi8077 Riddhi8077 commented Feb 11, 2026

This PR adds a focused failing test to verify that generating OpenAPI schema does not trigger application lifespan events.

No functional changes are included yet - this is only the initial test as per contribution workflow.

Summary by CodeRabbit

  • Tests
    • Added test coverage to verify application stability when OpenAPI generation encounters errors.

@github-actions
Copy link
Contributor

⚠️ No issue was linked in the PR description.
Please make sure to link an issue (e.g., 'Fixes #issue_number')

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 11, 2026

📝 Walkthrough

Walkthrough

A new test file is introduced to validate that OpenAPI generation failures do not crash the application. The test mocks the OpenAPI generation to raise an exception and verifies the app remains stable despite the failure.

Changes

Cohort / File(s) Summary
OpenAPI Lifespan Test
backend/tests/test_openapi_lifespan.py
New test file with test_openapi_failure_does_not_crash_app() that verifies OpenAPI generation exceptions do not crash the FastAPI application.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A test hops in, so shiny and new,
When OpenAPI stumbles, we'll still pull through!
Mock the exception, catch it with care,
The app stays resilient—no crash anywhere! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: adding a test to ensure OpenAPI generation doesn't execute lifespan events, which aligns with the actual test code added.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Contributor

⚠️ No issue was linked in the PR description.
Please make sure to link an issue (e.g., 'Fixes #issue_number')

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Fix all issues with AI agents
In `@backend/tests/test_openapi_lifespan.py`:
- Line 15: Remove the unused local FastAPI() instance and its import from the
test; instead reference the module-level app used by generate_openapi_json()
(i.e., rely on generate_openapi_json() from the application module or import the
module-level app if needed) so the test actually exercises
generate_openapi_json() rather than a stray local app variable; delete the line
creating app = FastAPI() and the FastAPI import and update imports to call
generate_openapi_json() from the application module.
- Around line 17-23: The test currently uses a redundant try/except with a
boolean flag because generate_openapi_json already swallows exceptions; either
simplify the test to directly call generate_openapi_json() (no try/except/flag)
and let the test fail on unexpected exceptions, or, if the goal is to assert
that generating the OpenAPI schema does not trigger lifespan events, replace the
boolean pattern with instrumentation/mocking of the app's lifespan handler and
assert that the mocked lifespan handler was not called during the call to
generate_openapi_json(); locate generate_openapi_json in main.py and update
backend/tests/test_openapi_lifespan.py accordingly to either remove the
unnecessary try/except and assert nothing (or simply call the function), or set
up a mock/spy for the application lifespan handler and assert it remains
uninvoked after generate_openapi_json().
- Around line 1-2: Remove the unused FastAPI import: the test file imports
FastAPI but never uses it, so delete the FastAPI import token to avoid
unused-import warnings; keep the pytest import for fixtures like monkeypatch and
ensure only pytest remains in the import statement.

Comment on lines +1 to +2
import pytest
from fastapi import FastAPI
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major

Remove unused import.

FastAPI (line 2) is imported but the instance created from it is unused by the test logic. pytest (line 1) is fine since monkeypatch is a pytest fixture.

Proposed fix
 import pytest
-from fastapi import FastAPI
 from main import generate_openapi_json
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import pytest
from fastapi import FastAPI
import pytest
from main import generate_openapi_json
🤖 Prompt for AI Agents
In `@backend/tests/test_openapi_lifespan.py` around lines 1 - 2, Remove the unused
FastAPI import: the test file imports FastAPI but never uses it, so delete the
FastAPI import token to avoid unused-import warnings; keep the pytest import for
fixtures like monkeypatch and ensure only pytest remains in the import
statement.

broken_openapi
)

app = FastAPI()
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Unused FastAPI instance — not connected to generate_openapi_json().

generate_openapi_json() operates on the module-level app object defined in main.py, not on this local instance. This line (and the FastAPI import on line 2) has no effect and is misleading, as it suggests the test exercises this app.

🤖 Prompt for AI Agents
In `@backend/tests/test_openapi_lifespan.py` at line 15, Remove the unused local
FastAPI() instance and its import from the test; instead reference the
module-level app used by generate_openapi_json() (i.e., rely on
generate_openapi_json() from the application module or import the module-level
app if needed) so the test actually exercises generate_openapi_json() rather
than a stray local app variable; delete the line creating app = FastAPI() and
the FastAPI import and update imports to call generate_openapi_json() from the
application module.

@Riddhi8077
Copy link
Author

Opened this PR to submit the initial failing test as per contribution workflow.
Waiting for mentor feedback before implementing any fix.

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.

1 participant