jsonify() behavior undefined when passed both args and kwargs
Package:
flask
56479

Exception Class:
TypeError
Raise code
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(
f"{dumps(data, indent=indent, separators=separators)}\n",
Links to the raise (1)
https://github.com/pallets/flask/blob/83f7efa04708a22c9701488f0f29b59c2b756bef/src/flask/json/__init__.py#L341Ways to fix
Arguments and keyword arguments should not be passed at the same time to jsonify.
How to reproduce:
from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/")
def index():
return jsonify("text", i="test")
app.run()
Code with fix
from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/")
def index():
return jsonify(j="text", i="test") #<-- pass only arg or kwarg, not both
app.run()
Add a possible fix
Please authorize to post fix