Skip to content

get_json's silent option cache None and causes weird side-effect #2651

Description

@RobertDeRose

Expected Behavior

Sample Code

app = Flask(__name__)

@app.route("/", methods=["POST"])
def broke_behvior():
    print(request.is_json)
    rv = request.get_json(silent=True)
    print(rv)
    rv = request.get_json()
    print(rv)
    return "", 200

client = app.test_client()
client.post("/", data="'invalid json'")

Output

False
None
<Response streamed [400 BAD REQUEST]>

Actual Behavior

False
None
None
<Response streamed [200 OK]>

The fact that calling get_json with silent and then calling it without silent seems to break expected behavior. When I don't call silent I am depending on the exception being thrown, however, because the code caches even on error, you get this weird behavior.

While this might be expected behavior by the authors, the docs don't make this clear.

I would think, invalid json should not be cached, yet I can see the value of it, however, it adds an layer of uncertainty and therefore, I my opinion, is a bug.

Environment

  • Python version: 3.4
  • Flask version: 0.12.2
  • Werkzeug version: 0.14.1

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions