Skip to content

Conversation

@eligundry
Copy link
Contributor

@eligundry eligundry commented May 13, 2016

Recently, we switched from Flask Classy to Flask Classful and discovered that all our routes using flask.jsonify with custom headers and status codes weren't working. For example:

def post(self):
    return jsonify({"success": True}), 201, {"X-CUSTOM-HEADER": "1"}

would return the proper JSON response but would be missing the headers and status code. Currently, we've had to change all those routes to:

def post(self):
    return make_response(jsonify({"success": True}), 201, {"X-CUSTOM-HEADER": "1"})

This is a definite regression from Flask Classy and this PR remedies that.

Shoutout to @hjc1710 for pair programming this with me.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.1%) to 97.081% when pulling 3f4df01 on eligundry:fix-custom-status-codes into 21dcb29 on teracyhq:develop.

@hoatle hoatle self-assigned this May 13, 2016
@hoatle hoatle added this to the 0.10.0 milestone May 13, 2016
@hoatle hoatle added the bug label May 13, 2016
elif any(x is not sentinel for x in (code, headers)):
# A response can be passed into `make_response` and it will set
# the key appropriately
response = make_response(response, code, headers)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eligundry - let's just make those None's.

Because of this:

In [14]: with app.app_context():
    make_response("asd", object(), {})
   ....:     
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
AttributeError: 'object' object has no attribute 'encode'

Looked into how make_response works and None is not like a special value that clears out status codes or headers, so we can safely use that. That was my original fear and why I recommended a sentinel. My bad, broski!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we should add a test that doesn't return a tuple, and just returns a Flask Response, because that would have totally caught this bug.

Copy link
Contributor Author

@eligundry eligundry May 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this and removed the sentinel value. Also added a test with a normal jsonify.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.1%) to 97.084% when pulling 8c6a759 on eligundry:fix-custom-status-codes into 21dcb29 on teracyhq:develop.

@hjc
Copy link
Contributor

hjc commented May 13, 2016

@eligundry - fails across the board for Python2.6. Apparently, assert_is_not_none isn't a tool supported by nose in 2.6...

@eligundry
Copy link
Contributor Author

@hoatle let me know if you want this squashed.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.1%) to 97.084% when pulling 20237fc on eligundry:fix-custom-status-codes into 21dcb29 on teracyhq:develop.

@hoatle
Copy link
Contributor

hoatle commented May 13, 2016

@eligundry thank you for your PR. It looks good to me.

Btw, have you tried this? http://flask-classful.teracy.org/#adding-resource-representations-get-real-classy-and-put-on-a-top-hat and its tests are here: https://github.com/teracyhq/flask-classful/blob/develop/test_classful/test_representations.py

I recommend using representations, however, we should also definitely support this case with your PR.

Could you help to squash? After that, I'll merge then.

Thank you again.

@hoatle hoatle added enhancement and removed bug labels May 13, 2016
@eligundry eligundry force-pushed the fix-custom-status-codes branch from 20237fc to 6bdf0b6 Compare May 13, 2016 17:33
@coveralls
Copy link

Coverage Status

Coverage increased (+0.1%) to 97.084% when pulling 6bdf0b6 on eligundry:fix-custom-status-codes into 21dcb29 on teracyhq:develop.


def test_jsonify_post_custom_status_code():
resp = client.post('/jsonify')
eq_(resp.status_code, 201)
Copy link
Contributor

@hoatle hoatle May 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure to check the resp.data for all the tests

@eligundry eligundry force-pushed the fix-custom-status-codes branch from 6bdf0b6 to bd620c0 Compare May 13, 2016 17:49
@coveralls
Copy link

Coverage Status

Coverage increased (+0.1%) to 97.084% when pulling bd620c0 on eligundry:fix-custom-status-codes into 21dcb29 on teracyhq:develop.

Allows flask.jsonify to return status codes and headers

Allows flask.jsonify to return status codes and headers

Allows flask.jsonify to return status codes and headers

Allows flask.jsonify to return status codes and headers

Removes sentinel values from proxy response
@eligundry eligundry force-pushed the fix-custom-status-codes branch from bd620c0 to 3f637de Compare May 13, 2016 17:56
@coveralls
Copy link

Coverage Status

Coverage increased (+0.1%) to 97.103% when pulling 3f637de on eligundry:fix-custom-status-codes into 21dcb29 on teracyhq:develop.

@hoatle hoatle added bug and removed enhancement labels May 13, 2016
@hjc
Copy link
Contributor

hjc commented May 13, 2016

@eligundry - Python 3.x tests are failing across the board 😦. Super odd...

@hoatle - We haven't yet tried representations. I saw those when we found this problem (looking through the source), but didn't start swapping to them. We'll probably do that for our next project. That said, this was definitely a regression so needs a fix either way.

Nice part about representations, we can support XML responses for our API for masochists!

@hoatle
Copy link
Contributor

hoatle commented May 13, 2016

@hjc1710 @eligundry for the failed test, please use: eq_(resp.data, b"Params Decorator Delete 1234") for example instead of eq_(resp.data, "Params Decorator Delete 1234"). That was how I fixed the same kind failing tests on Python 3.x

@hjc
Copy link
Contributor

hjc commented May 13, 2016

@hoatle Thanks!

@hoatle
Copy link
Contributor

hoatle commented May 16, 2016

I've updated the PR to fix failing tests and updated the commit message here at #20

@hoatle hoatle closed this May 16, 2016
@hoatle hoatle removed this from the 0.10.0 milestone May 16, 2016
@hoatle
Copy link
Contributor

hoatle commented May 16, 2016

@eligundry @hjc1710 thank you very much for you help, Flask-Classful v0.10.0 was released https://pypi.python.org/pypi/Flask-Classful

@hjc
Copy link
Contributor

hjc commented May 16, 2016

Awesome! Thanks so much @hoatle!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants