-
-
Notifications
You must be signed in to change notification settings - Fork 771
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
Flask test_client always returns 404 in connexion 3.0.3 #1826
Comments
Happenning the same here but using Swagger UI, so the problem may not be specific to What's more, maybe issue #1823 may help by fixing |
Thanks for your reply! |
This is because Connexion lazily initializes the routing when the first ASGI call is received. Workaround: # Workaround for race condition in connexion initialization
# pylint: disable=protected-access
connexion_app.middleware.app, connexion_app.middleware.middleware_stack = \
connexion_app.middleware._build_middleware_stack() Excuse the comment, (it also causes a race condition in startup if you're using werkzeug with threads, and a wsgi-asgi converter). Correct solution is probably to convert your unit tests to use the provided startlette TestClient, but that involves some work as the API is significantly different. |
Thanks for the reports everyone. I just submitted #1828 to set an upperbound to our Starlette dependency. We'll release it as soon as possible and will investigate the underlying issue afterwards. |
The issue is being discussed on the starlette repo here. |
Temporary fix for #1826 We should release this asap.
3.0.4 has been released containing this fix. |
Closing in favor of #1824 |
@RobbeSneyders Thanks for the quick update! Same error as in the first post: |
Are you still using the Flask test client? Because that is not expected to work anymore. You should use the Connexion test client. |
Yes I tried the Flask test client. Alright thanks, I just switched to the connexion test client and got it working with POST/PUT request. For anyone interested: Before (Flask test client): response = app.put(
"/myroute",
content_type="application/json",
data=json.dumps(body),
query_string={"param": param},
) After (Connexion test client): response = app.put(
"/myroute",
headers={"Content-Type": "application/json"},
data=json.dumps(body),
params={"param": param},
) There are also some changes needed with checking the response, but I will figure it out :) |
Description
I migrated my code from connexion 2 to connexion 3 and I really love the upgrade!
My code works well, but I have a problem with the underlying test_client.
The flask test_client always returns 404. I tried get/post/put/etc. on different routes but all of them return 404 even though the routes are working in the real app.
When I use the connexion test client (
connexion_app.test_client()
instead ofconnexion_app.app.test_client()
) it kind of works, but I get a different behaviour.E.g. The passed body in my function is of type
bytes
instead ofdict
.Also in the test the app expects different parameters, e.g.
app.post(..., query_string="...")
vs.app.post(..., params="...")
, but that's not a big problem.Is this a bug in connexion 3 that the underlying flask test_client does not work or do I need to migrate something in the test client? I would be also okay to use the connexion test_client, but only if the behaviour is the same as in the real app, which is currently not the case.
Documentation that I found about the test_client: https://connexion.readthedocs.io/en/latest/testing.html
I provided a minimal example which works fine with connexion 2 but does not work with connexion 3.
Expected behaviour
Actual behaviour
Summary of the log (full log below)
Full log
Steps to reproduce
Works fine with:
pip install connexion==2.14.2 flask==2.2.5
Does not work:
pip install connexion==3.0.3 flask==2.2.5
pip install connexion==3.0.3 flask==3.0.0
Execute the test:
pytest test_app.py
api.yml
route.py
test_app.py
Additional info:
Output of the commands:
python --version
:Python 3.10.11
pip show connexion | grep "^Version\:"
:Version: 3.0.3
pip show starlette | grep "^Version\:"
:Version: 0.27.0
andVersion: 0.33.0
The text was updated successfully, but these errors were encountered: