From 6b16edfb5bc445ea28fde5e154b2c120315af1f0 Mon Sep 17 00:00:00 2001 From: MuhammadUmar045 Date: Sat, 21 Feb 2026 10:31:13 +0500 Subject: [PATCH] Add regression test for Annotated ForwardRef OpenAPI (issue #13056) --- ...ssue_13056_openapi_annotated_forwardref.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tests/test_issue_13056_openapi_annotated_forwardref.py diff --git a/tests/test_issue_13056_openapi_annotated_forwardref.py b/tests/test_issue_13056_openapi_annotated_forwardref.py new file mode 100644 index 0000000000..653e31d8d9 --- /dev/null +++ b/tests/test_issue_13056_openapi_annotated_forwardref.py @@ -0,0 +1,29 @@ +from typing import Annotated, Union + +from fastapi import FastAPI +from fastapi.testclient import TestClient +from pydantic import BaseModel + +app = FastAPI() + + +class Item(BaseModel): + name: str + # This is the ForwardRef issue context + next_item: Annotated[Union["Item", None], None] = None + + +@app.get("/", response_model=Item) +def read_root(): + return Item(name="root") + + +client = TestClient(app) + + +def test_issue_13056_openapi_annotated_forwardref(): + # This triggers the schema generation where the crash usually happens + response = client.get("/openapi.json") + assert response.status_code == 200, response.text + data = response.json() + assert data["components"]["schemas"]["Item"] \ No newline at end of file