From 308c762d6ebf68cfe356755d759fa0589508227d Mon Sep 17 00:00:00 2001 From: WrldEngine Date: Wed, 4 Jun 2025 21:38:15 +0500 Subject: [PATCH] edit: bson dependency comment for mypy in responses.py & add tests for bson serialization --- fastapi/responses.py | 4 ++-- tests/test_bson_response_class.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 tests/test_bson_response_class.py diff --git a/fastapi/responses.py b/fastapi/responses.py index c915cc7cb..34fc72986 100644 --- a/fastapi/responses.py +++ b/fastapi/responses.py @@ -21,9 +21,9 @@ except ImportError: # pragma: nocover try: - import bson + import bson # type: ignore[import-untyped] except ImportError: # pragma: nocover - bson = None # type: ignore + bson = None class UJSONResponse(JSONResponse): diff --git a/tests/test_bson_response_class.py b/tests/test_bson_response_class.py new file mode 100644 index 000000000..b603c3366 --- /dev/null +++ b/tests/test_bson_response_class.py @@ -0,0 +1,21 @@ +import bson + +from fastapi import FastAPI +from fastapi.responses import BSONResponse +from fastapi.testclient import TestClient + +app = FastAPI(default_response_class=BSONResponse) + + +@app.get("/bson_keys") +def get_bson_serialized_data(): + return {"key": "Hello World", 1: 1} + + +client = TestClient(app) + + +def test_bson_serialized_data(): + with client: + response = client.get("/bson_keys") + assert response.content == bson.dumps({"key": "Hello World", 1: 1})