odoo / odoo

Odoo. Open Source Apps To Grow Your Business.
https://www.odoo.com
Other
37.88k stars 24.6k forks source link

TypeError: cannot marshal <class 'collections.defaultdict'> objects #113083

Open PauMB85 opened 1 year ago

PauMB85 commented 1 year ago

Impacted versions:

{'server_version': 'saas~16.1+e', 'server_version_info': ['saas~16', 1, 0, 'final', 0, 'e'], 'server_serie': 'saas~16.1', 'protocol_version': 1}

Steps to reproduce:

uid = common.authenticate(db, username, password, {}) models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url)) PURCHARSE_ORDER = models.execute_kw(db, uid, password, 'purchase.order', 'search_read', [])

Current behavior: When execute the PURCHARSE_ORDER, always is generated an belong error

<Fault 1: 'Traceback (most recent call last):\n  File "/usr/lib/python3.10/xmlrpc/client.py", line 522, in __dump\n    f = self.dispatch[type(value)]\nKeyError: <class \'collections.defaultdict\'>\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n  File "/home/odoo/src/odoo/saas-16.1/odoo/addons/base/controllers/rpc.py", line 149, in xmlrpc_2\n    response = self._xmlrpc(service)\n  File "/home/odoo/src/custom/trial/saas_trial/controllers/main.py", line 266, in _xmlrpc\n    res = super(OdooRPC, self)._xmlrpc(service)\n  File "/home/odoo/src/odoo/saas-16.1/odoo/addons/base/controllers/rpc.py", line 130, in _xmlrpc\n    return xmlrpc.client.dumps((result,), methodresponse=1, allow_none=False)\n  File "/usr/lib/python3.10/xmlrpc/client.py", line 981, in dumps\n    data = m.dumps(params)\n  File "/usr/lib/python3.10/xmlrpc/client.py", line 514, in dumps\n    dump(v, write)\n  File "/usr/lib/python3.10/xmlrpc/client.py", line 536, in __dump\n    f(self, value, write)\n  File "/usr/lib/python3.10/xmlrpc/client.py", line 589, in dump_array\n    dump(v, write)\n  File "/usr/lib/python3.10/xmlrpc/client.py", line 536, in __dump\n    f(self, value, write)\n  File "/usr/lib/python3.10/xmlrpc/client.py", line 607, in dump_struct\n    dump(v, write)\n  File "/usr/lib/python3.10/xmlrpc/client.py", line 536, in __dump\n    f(self, value, write)\n  File "/usr/lib/python3.10/xmlrpc/client.py", line 607, in dump_struct\n    dump(v, write)\n  File "/usr/lib/python3.10/xmlrpc/client.py", line 526, in __dump\n    raise TypeError("cannot marshal %s objects" % type(value))

\nTypeError: cannot marshal <class \'collections.defaultdict\'> objects\n'>

in next step I filter the fields and the problem is when use the field: `tax_totals, this field is an object

Expected behavior:

When call the api, at the model 'purchase.order', we don't receive this error.

Video/Screenshot link (optional):

Support ticket number submitted via odoo.com/help (optional):

AbdelAzizMohamedMousa commented 1 year ago

The error message suggests that the issue is related to the inability of the xmlrpc.client module to serialize the object of type defaultdict, which is being returned by the 'purchase.order' model.

To resolve this error, you can try to convert the defaultdict object into a regular dictionary before returning it. You can use the dict() function to achieve this conversion.

For example, you can modify the code as follows: uid = common.authenticate(db, username, password, {}) models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url)) purchase_orders = models.execute_kw(db, uid, password, 'purchase.order', 'search_read', []) for order in purchase_orders: order['tax_totals'] = dict(order['tax_totals']) # Convert defaultdict to regular dictionary This should convert the 'tax_totals' field from a defaultdict object to a regular dictionary, which should be serializable by the xmlrpc.client module.

Alternatively, you can try to use a different serialization method such as JSON instead of XML-RPC to avoid this issue.

yelizariev commented 1 year ago

I can't reproduce the problem in v16. Anyway, the solution should be adding exportable=False and patching check_field_access_rights method.

image https://github.com/odoo/odoo/pull/110699

If you want us make further investigation you should report a ticket via https://odoo.com/help

flotho commented 3 months ago

could it be like https://github.com/odoo/odoo/pull/75417 ?