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

Implement fields.Method handler for marshmallow #174

Open
psaiz opened this issue Jun 30, 2023 · 0 comments
Open

Implement fields.Method handler for marshmallow #174

psaiz opened this issue Jun 30, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@psaiz
Copy link
Contributor

psaiz commented Jun 30, 2023

Package version (if known):1.5.0

Describe the bug

The convertion from marshmallow schema to json_schema fails for fields that are declared as method. For instance, for the user management, the conversion of the is_current_user fails.

Steps to Reproduce

With version 1.5.0:

Traceback (most recent call last):
  File "/Users/psaiz/github/invenio-administration/invenio_administration/marshmallow_utils.py", line 104, in jsonify_schema
    "type": custom_mapping[field_type_name],
KeyError: <class 'marshmallow.fields.Method'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/psaiz/.local/share/virtualenvs/psaiz-master-PcfYpoPq/lib/python3.9/site-packages/flask/app.py", line 2552, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/psaiz/.local/share/virtualenvs/psaiz-master-PcfYpoPq/lib/python3.9/site-packages/werkzeug/middleware/proxy_fix.py", line 187, in __call__
    return self.app(environ, start_response)
  File "/Users/psaiz/.local/share/virtualenvs/psaiz-master-PcfYpoPq/lib/python3.9/site-packages/werkzeug/middleware/dispatcher.py", line 78, in __call__
    return app(environ, start_response)
  File "/Users/psaiz/.local/share/virtualenvs/psaiz-master-PcfYpoPq/lib/python3.9/site-packages/flask/app.py", line 2532, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/psaiz/.local/share/virtualenvs/psaiz-master-PcfYpoPq/lib/python3.9/site-packages/flask/app.py", line 2529, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/psaiz/.local/share/virtualenvs/psaiz-master-PcfYpoPq/lib/python3.9/site-packages/flask/app.py", line 1825, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/psaiz/.local/share/virtualenvs/psaiz-master-PcfYpoPq/lib/python3.9/site-packages/flask/app.py", line 1823, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/psaiz/.local/share/virtualenvs/psaiz-master-PcfYpoPq/lib/python3.9/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/Users/psaiz/.local/share/virtualenvs/psaiz-master-PcfYpoPq/lib/python3.9/site-packages/flask_principal.py", line 199, in _decorated
    rv = f(*args, **kw)
  File "/Users/psaiz/.local/share/virtualenvs/psaiz-master-PcfYpoPq/lib/python3.9/site-packages/flask/views.py", line 107, in view
    return current_app.ensure_sync(self.dispatch_request)(**kwargs)
  File "/Users/psaiz/.local/share/virtualenvs/psaiz-master-PcfYpoPq/lib/python3.9/site-packages/flask/views.py", line 188, in dispatch_request
    return current_app.ensure_sync(meth)(**kwargs)
  File "/Users/psaiz/github/invenio-administration/invenio_administration/views/base.py", line 388, in get
    serialized_schema = self._schema_to_json(schema)
  File "/Users/psaiz/github/invenio-administration/invenio_administration/views/base.py", line 195, in _schema_to_json
    return jsonify_schema(schema)
  File "/Users/psaiz/github/invenio-administration/invenio_administration/marshmallow_utils.py", line 109, in jsonify_schema
    raise Exception(f"Unrecognised schema field {field}: {field_type_name}")
Exception: Unrecognised schema field is_current_user: <class 'marshmallow.fields.Method'>

As a workaround, the following code was added:

+++ b/invenio_administration/marshmallow_utils.py
@@ -36,6 +36,7 @@ custom_mapping = {
     fields.Date: "date",
     fields.TimeDelta: "timedelta",
     fields.Decimal: "decimal",
+    fields.Method: None,

This might need some better implementation

Expected behavior

Screenshots (if applicable)

Additional context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant