-
Notifications
You must be signed in to change notification settings - Fork 162
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
make_response does not work with pydantic #61
Comments
Thanks for the report! I can try to have a look this weekend. Could you please provide a minimal failing test to verify any changes on? |
Thanks for the quick answer. Example.main.py:
endpoints.py
my_api.yaml
I'm Sorry for that not so minimal example. Since Connexion needs a yaml to work with and also reimports the file of the endpoints, the endpoints needs to be in their own file in order to initialize the histogram only once. Anyway you should be able to run this locally if you install Pydantic and Connexion. ProblemThe expected behavior of the /test endpoint would be: {"foo": "Test version"}, 200.
This should be caused by the "make_response" from base Flask since the method cannot handle Pydantic objects. My quick solution on API side would be to parse everything through jsonify in the return statement. But thats denies the Pydantic typing. If you agree on this, I could fork the project over the weekend and try to add support for Connexion to the decorator. |
I'd rather avoid the jsonifying, but curios to see how you'd implement it if you have time. |
It looks like https://github.com/rycus86/prometheus_flask_exporter/blob/connexion-support/prometheus_flask_exporter/__init__.py#L754 should work, https://github.com/rycus86/prometheus_flask_exporter/blob/connexion-support/examples/connexion-pydantic/main.py has an example. I'll sort out some CI issues then I'll cut a new release with it. 🎉 tl;dr: import connexion
from prometheus_flask_exporter import ConnexionPrometheusMetrics
app = connexion.App(__name__)
metrics = ConnexionPrometheusMetrics(app, export_defaults=None) Then everything should work as you'd expect. 🤞 |
That should solve the problem Indeed. Thank you for the quick help and keep up the good work 👍 |
But before we close this thread is there a to apply this to the UWsgiPrometheusMetrics ?
My original setup looks like this... And not sure right now if how I can make this work if the app initializes the UwsgiMetric. |
Yep -- though a bit more cumbersome --, you can use |
I just realized this will still not work nicely with UWsgi and the recent Connexion response converter. 🤦♂️ |
OK, interestingly, Connexion works behind uWSGI without having to add any extra wrapping. 🤷♂️ In the https://github.com/rycus86/prometheus_flask_exporter/blob/master/examples/uwsgi-connexion/Dockerfile you can see we run the Connexion app with And https://github.com/rycus86/prometheus_flask_exporter/blob/master/examples/uwsgi-connexion/main.py#L5 simply uses a Let me know if this works for you! |
Setup:
The API I'm working with uses Connexion and Pydantic as return wrapper for type safety.
Problem:
Connexion can handle pydantic classes and is able to parse them into a proper response. I.e. that a normal response could be a single pydantic dataclass object even without any status code. (https://pydantic-docs.helpmanual.io/usage/dataclasses/)
As soon as I use a decorator on such an endpoint the response is 500.
It would be really appreciated if this behavior could be deactivated somehow.
prometheus_flask_exporter/prometheus_flask_exporter/__init__.py
Line 629 in d4353f0
The text was updated successfully, but these errors were encountered: