From a8bbcebcb40c2005037c0ec25d9a400417897b68 Mon Sep 17 00:00:00 2001 From: Yurii Motov Date: Wed, 10 Dec 2025 18:01:09 +0100 Subject: [PATCH] Fix coverage --- fastapi/_compat/v2.py | 2 +- ..._schema_fals_with_nested_computed_field.py | 46 +++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/fastapi/_compat/v2.py b/fastapi/_compat/v2.py index 75bc673d2..2b6c0a5f6 100644 --- a/fastapi/_compat/v2.py +++ b/fastapi/_compat/v2.py @@ -176,7 +176,7 @@ def _model_has_computed_fields(model_or_enum: Any) -> bool: model_schema = model_or_enum.__pydantic_core_schema__.get("schema", {}) computed_fields = model_schema.get("computed_fields", []) return len(computed_fields) > 0 - return False + return False # pragma: no cover def _has_computed_fields(field: ModelField) -> bool: diff --git a/tests/test_separate_input_output_schema_fals_with_nested_computed_field.py b/tests/test_separate_input_output_schema_fals_with_nested_computed_field.py index 64aaa1471..12014c1d4 100644 --- a/tests/test_separate_input_output_schema_fals_with_nested_computed_field.py +++ b/tests/test_separate_input_output_schema_fals_with_nested_computed_field.py @@ -8,8 +8,8 @@ from pydantic import BaseModel from .utils import needs_pydanticv2 -@pytest.fixture(name="client") -def get_client(): +@pytest.fixture(name="client", params=[True, False]) +def get_client(request: pytest.FixtureRequest): from pydantic import computed_field class MyModel(BaseModel): @@ -22,11 +22,7 @@ def get_client(): def is_adult(self) -> bool: return self.age >= 18 - app = FastAPI(separate_input_output_schemas=False) - - @app.get("/item") - def get_item() -> MyModel: - return MyModel(id=1, name="Alice", age=30) + app = FastAPI(separate_input_output_schemas=request.param) @app.get("/list") def get_items() -> List[MyModel]: @@ -39,6 +35,26 @@ def get_client(): yield TestClient(app) +@needs_pydanticv2 +def test_create_item(client: TestClient): + response = client.post( + "/item", + json={"id": 1, "name": "Alice", "age": 30}, + ) + assert response.status_code == 200, response.text + assert response.json() == {"id": 1, "name": "Alice", "age": 30, "is_adult": True} + + +@needs_pydanticv2 +def test_get_items(client: TestClient): + response = client.get("/list") + assert response.status_code == 200, response.text + assert response.json() == [ + {"id": 1, "name": "Alice", "age": 30, "is_adult": True}, + {"id": 2, "name": "Bob", "age": 17, "is_adult": False}, + ] + + @needs_pydanticv2 def test_openapi(client: TestClient): response = client.get("/openapi.json") @@ -51,22 +67,6 @@ def test_openapi(client: TestClient): "openapi": "3.1.0", "paths": { "/item": { - "get": { - "operationId": "get_item_item_get", - "responses": { - "200": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/MyModel-Output", - }, - }, - }, - "description": "Successful Response", - }, - }, - "summary": "Get Item", - }, "post": { "operationId": "create_item_item_post", "requestBody": {