-
-
Notifications
You must be signed in to change notification settings - Fork 772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow http.HTTPStatus enums as response status codes. #504
Conversation
Python 3.5 introduced a new enumeration "http.HTTPStatus" for representing HTTP response status codes. The default response validation introduced in connexion 1.1.12 highlighted the fact that connexion does not natively support this type and was previously silently ignoring non-integer status code representations. This modifies the response validation code to extract the value when given an enum instead of an int. Somewhat hacky test code is added to check for enum support on python versions that include "http.HTTPStatus".
I like it, but can you had a unit test for custom enum (this would cover the case where someone uses a backport of the enum). |
Do you have a suggestion on a good way to only run the test in python versions where Enum is available, or should I use the same ImportError based approach? |
@jkrukoff there's a way to run the test with only specific python versions. First you need to indicate that the test should be skipped in python versions that do not support enums. You can use the @unittest.skipIf decorator: @unittest.skipIf(sys.version_info < (3, 5), "Not supported in this version")
def test_your_test_name(...): If you want to run your tests with multiple python versions, we're using tox for this. To verify that the above works you can run tox with $ tox -e py27,py34,py36 You can install tox with $ pip install tox tox is currently not in https://github.com/zalando/connexion/blob/master/requirements-devel.txt but I'll make a PR to add it. |
1 similar comment
This reverts to exception based python version checking for both tests, due to the suggested unittest skipping alternative not being supported in all python versions. "unittest.case.SkipTest: Not supported in this version" is the error reported.
Added the test for arbitrary enums, which exposed an assumption inside flask that appears to only work for ints/actual HTTPStatus Enums. Moved the enum value extraction deeper into the stack to compensate. Tried to use @unittest.skipif in the test suite, but that failed with an unsupported error from unittest for python versions <3.5. Reverted back to the previous approach to get tests passing again. |
@jkrukoff The change looks good to me. |
It was pretty close to "unittest.skipif is unsupported in this version". It it's been present since 3.1, would explain why the python 2.7 test suite was exploding with that error. Though, honestly, that might have been my skip message being reflected back at me. Perhaps I was hitting some configuration in the test runner that labelled skips as errors? Rebuilding f789831 will likely trip the error again. |
tests/fakeapi/hello.py
Outdated
@@ -1,4 +1,6 @@ | |||
#!/usr/bin/env python3 | |||
import sys | |||
import unittest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This imports are not being used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yeah, forgot to remove those from the attempt to use uniftest.skipIf.
connexion/apis/flask_api.py
Outdated
@@ -154,6 +154,12 @@ def _build_flask_response(cls, mimetype=None, content_type=None, | |||
flask_response = flask.current_app.response_class(**kwargs) # type: flask.Response | |||
|
|||
if status_code is not None: | |||
try: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use hasattr
here since the most common use case is status_code
being an integer.
hasattr
is faster than try... except AtributeError
for cases where the attribute doesn't exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, and you get to repeat yourself while doing the check. It'd be even faster if just didn't support enums at all!
Will update.
1 similar comment
👍 |
A small bug was introduced in spec-first#500 when sanitizing the request query parameters. Instead of fetching the arguments from `request.query`, the parameters were sanitized from the `request.form`. This causes an error log to be printed, as the parameters are not expected for the query (for example in the case of a POST request). Fixes spec-first#504
A small bug was introduced in spec-first#500 when sanitizing the request query parameters. Instead of fetching the arguments from `request.query`, the parameters were sanitized from the `request.form`. This causes an error log to be printed, as the parameters are not expected for the query (for example in the case of a POST request). Fixes spec-first#504
A small bug was introduced in spec-first#500 when sanitizing the request query parameters. Instead of fetching the arguments from `request.query`, the parameters were sanitized from the `request.form`. This causes an error log to be printed, as the parameters are not expected for the query (for example in the case of a POST request). Fixes spec-first#504
* FIX a typo causing an ERROR log A small bug was introduced in #500 when sanitizing the request query parameters. Instead of fetching the arguments from `request.query`, the parameters were sanitized from the `request.form`. This causes an error log to be printed, as the parameters are not expected for the query (for example in the case of a POST request). Fixes #504 * adds a fixture for testing query param sanitazion Adds a simple fixture to test form and query parameter sanitazion. This is mostly related to #522, in which the `formData` parameters were treated as query parameters. * add a test to validate form data params * introduce testfixtures library
Python 3.5 introduced a new enumeration "http.HTTPStatus" for
representing HTTP response status codes. The default response validation
introduced in connexion 1.1.12 highlighted the fact that connexion does
not natively support this type and was previously silently ignoring
non-integer status code representations.
This modifies the response validation code to extract the value when
given an enum instead of an int. Somewhat hacky test code is added to
check for enum support on python versions that include
"http.HTTPStatus".
Fixes #503
Changes proposed in this pull request: