🐛 Get rid of a Flask warning

Also migrate allow_nan fix to custom JsonProvider
This commit is contained in:
Gina Häußge committed 2022-10-27 12:06:18 +02:00
1 parent 22315b1742
commit 7e495ed476
2 files changed
+6 -32

No files matched your search

+1 -2
View File
@@ -366,7 +366,6 @@ class Server:
util.tornado.fix_json_encode()
util.tornado.fix_websocket_check_origin()
util.tornado.enable_per_message_deflate_extension()
util.flask.fix_flask_jsonify()
self._setup_mimetypes()
@@ -1391,7 +1390,6 @@ class Server:
app.jinja_environment = PrefixAwareJinjaEnvironment
app.config["TEMPLATES_AUTO_RELOAD"] = True
app.config["JSONIFY_PRETTYPRINT_REGULAR"] = False
app.config["REMEMBER_COOKIE_DURATION"] = 90 * 24 * 60 * 60 # 90 days
app.config["REMEMBER_COOKIE_HTTPONLY"] = True
# REMEMBER_COOKIE_SECURE will be taken care of by our custom cookie handling
@@ -1401,6 +1399,7 @@ class Server:
# setup octoprint's flask json serialization/deserialization
app.json = OctoPrintJsonProvider(app)
app.json.compact = False
s = settings()
+5 -30
View File
@@ -12,7 +12,7 @@ import os
import threading
import time
from datetime import datetime, timedelta
from typing import Union
from typing import Any, Union
import flask
import flask.json
@@ -237,35 +237,6 @@ def fix_webassets_convert_item_to_flask_url():
flask_assets.FlaskResolver.convert_item_to_flask_url = fixed_convert_item_to_flask_url
def fix_flask_jsonify():
def fixed_jsonify(*args, **kwargs):
"""Backported from https://github.com/pallets/flask/blob/7e714bd28b6e96d82b2848b48cf8ff48b517b09b/flask/json/__init__.py#L257"""
from flask.json import current_app, dumps
indent = None
separators = (",", ":")
if current_app.config["JSONIFY_PRETTYPRINT_REGULAR"] or current_app.debug:
indent = 2
separators = (", ", ": ")
if args and kwargs:
raise TypeError(
"jsonify() behavior undefined when passed both args and kwargs"
)
elif len(args) == 1: # single args are passed directly to dumps()
data = args[0]
else:
data = args or kwargs
return current_app.response_class(
dumps(data, indent=indent, separators=separators, allow_nan=False) + "\n",
mimetype="application/json",
)
flask.jsonify = fixed_jsonify
# ~~ WSGI environment wrapper for reverse proxying
@@ -1936,6 +1907,10 @@ class OctoPrintJsonProvider(flask.json.provider.DefaultJSONProvider):
except TypeError:
return flask.json.provider.DefaultJSONProvider.default(object_)
def dumps(self, obj: Any, **kwargs: Any) -> str:
kwargs.setdefault("allow_nan", False)
return super().dumps(obj, **kwargs)
##~~ Session signing