Skip to content
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

Serialization error in Web API with numpy arrays #899

Closed
bonjourmauko opened this issue Jul 16, 2019 · 0 comments · Fixed by #900
Closed

Serialization error in Web API with numpy arrays #899

bonjourmauko opened this issue Jul 16, 2019 · 0 comments · Fixed by #900
Labels
kind:fix Bugs are defects and failure demand.

Comments

@bonjourmauko
Copy link
Member

Hi there!

I really enjoy OpenFisca, but I recently encountered an issue.

Here is what I did:

I visited https://fr.openfisca.org/legislation

Here is what I expected to happen:

To work.

Here is what actually happened:

It didn't.

Here is data (or links to it) that can help you reproduce this issue:

If you have access to the server, try:

journalctl -u openfisca-web-api-fr-latest.service

TypeError: Object of type ndarray is not JSON serializable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/home/openfisca/virtualenvs/api-fr-latest/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 135, in handle
    self.handle_request(listener, req, client, addr)
  File "/home/openfisca/virtualenvs/api-fr-latest/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 176, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/home/openfisca/virtualenvs/api-fr-latest/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/openfisca/virtualenvs/api-fr-latest/lib/python3.7/site-packages/werkzeug/middleware/proxy_fix.py", line 228, in __call__
    return self.app(environ, start_response)
  File "/home/openfisca/virtualenvs/api-fr-latest/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/home/openfisca/virtualenvs/api-fr-latest/lib/python3.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/home/openfisca/virtualenvs/api-fr-latest/lib/python3.7/site-packages/flask/app.py", line 1878, in handle_exception
    server_error = handler(server_error)
  File "/home/openfisca/virtualenvs/api-fr-latest/lib/python3.7/site-packages/openfisca_web_api/app.py", line 182, in internal_server_error
    message = str(e.args[0])

Otherwise, in your terminal:

git clone git@github.com:openfisca/openfisca-core.git
cd openfisca-core
make uninstall
make install
pip install openfisca-france ipython
ipython

Then try the following snippet:

from flask import Flask, jsonify
from openfisca_core.scripts import build_tax_benefit_system
from openfisca_web_api.loader import build_data

def trace():
    tax_benefit_system = build_tax_benefit_system("openfisca_france", None, None)
    data = build_data(tax_benefit_system)
    return data["openAPI_spec"]["definitions"]["Trace"]["example"]["trace"]

def acc(data, key):
    params = data[key]["parameters"]
    return [(key, param, params[param], type(params[param])) for param in params.keys()]

def serializable(value):
    try:
        jsonify({**value})
        return True
    except TypeError:
        return False

with Flask(__name__).app_context():
    data = trace()
    result = [acc(data, key) for key, _ in data.items() if not serializable(data[key])]
Expected
result
>>> []
Actual
result[0][0]
(
  'aide_logement_loyer_plafond<2017-12>',
  'prestations.aides_logement.loyers_plafond.par_zone<2017-12-01>',
  array([51.54]),
  numpy.ndarray),
  )

Context

I identify more as a:

  • Developer (I create tools that use the existing OpenFisca code).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:fix Bugs are defects and failure demand.
Projects
None yet
1 participant