-
Notifications
You must be signed in to change notification settings - Fork 40
-
I have added support for application/bson
in my flask application, alongside application/json
This format is almost like json, with support for binary data, aside from text.
A simple support in flask is to do this:
@app.before_request def setup_request(): if request.content_type and request.content_type.startswith("application/bson"): request.is_bson = True request.bson = None try: if request.data is not None: request.bson = BSON(request.data).decode() except Exception as ex: logger.error(ex, "Unable to decode BSON request data")
I want to use the same built-in validation as for application/json
that flask-openapi3 supports, the only way I was able to find to make it work is by doing this:
from flask_openapi3 import request as flask_openapi3_request def _validate_body(body: type[BaseModel], func_kwargs: dict): obj = None if request.is_json: obj = request.get_json(silent=True) elif request.is_bson: obj = request.bson if isinstance(obj, str): body_model = body.model_validate_json(json_data=obj) else: body_model = body.model_validate(obj=obj) func_kwargs["body"] = body_model # Patching body validation as only json is supported by default flask_openapi3_request._validate_body = _validate_body
Is there a more elegant, future proof way instead of patching the private api ?
Also, thank you for the great implementation you have in this project.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment