Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Ability to support and validate other input formats / content types such as application/bson #202

Unanswered
iongion asked this question in Q&A
Discussion options

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.

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /