From 5195ac978b8c16e3ae076ce341e3eac31986711e Mon Sep 17 00:00:00 2001 From: WrldEngine Date: Wed, 4 Jun 2025 04:34:05 +0500 Subject: [PATCH] feat: add bson in FastAPI and update .gitignore for pdm.lock & other additional files --- .gitignore | 4 ++++ fastapi/responses.py | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/.gitignore b/.gitignore index ef6364a9a..b46385849 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,7 @@ archive.zip # macOS .DS_Store + +# Pdm additional files +.pdm-python +pdm.lock diff --git a/fastapi/responses.py b/fastapi/responses.py index 6c8db6f33..0be42e99e 100644 --- a/fastapi/responses.py +++ b/fastapi/responses.py @@ -20,6 +20,12 @@ except ImportError: # pragma: nocover orjson = None # type: ignore +try: + import bson +except ImportError: # pragma: nocover + bson = None # type: ignore + + class UJSONResponse(JSONResponse): """ JSON response using the high-performance ujson library to serialize data to JSON. @@ -46,3 +52,22 @@ class ORJSONResponse(JSONResponse): return orjson.dumps( content, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY ) + + +class BSONResponse(Response): + """ + BSON response using the current Python bson library for data serialization. + + Note: This is a temporary solution. Soon, a custom Rust wrapper will be implemented for BSON serialization/deserialization to improve performance and reliability. + + Read more about it in the + [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/). + """ + + media_type = "application/bson" + + def render( + self, content: Any, generator: Any | None = None, on_unknown: Any | None = None + ) -> bytes: + assert bson is not None, "bson must be installed to use BSONResponse" + return bson.dumps(content, generator=generator, on_unknown=on_unknown)