Chaffelson / nipyapi

A convenient Python wrapper for Apache NiFi
Other
245 stars 76 forks source link

Utils.dump() cannot serialize entities #234

Closed ottobackwards closed 3 years ago

ottobackwards commented 3 years ago

Description

def test_deser_flow():
    r = canvas.get_flow('root')
    assert isinstance(r, ProcessGroupFlowEntity)
    s = utils.dump(r,'json')
    f = utils.load(s,('nifi', 'ProcessGroupFlowEntity'))
    assert isinstance(f, ProcessGroupFlowEntity)
Testing started at 00:12 ...
/Users/ottobackwards/.pyenv/versions/3.8.2/bin/python "/Users/ottobackwards/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/202.7660.27/PyCharm.app/Contents/plugins/python/helpers/pycharm/_jb_pytest_runner.py" --target test_canvas.py::test_deser_flow
Launching pytest with arguments test_canvas.py::test_deser_flow in /Users/ottobackwards/src/github/ottobackwards/forks/nipyapi/tests

============================= test session starts ==============================
platform darwin -- Python 3.8.2, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- /Users/ottobackwards/.pyenv/versions/3.8.2/bin/python
cachedir: .pytest_cache
rootdir: /Users/ottobackwards/src/github/ottobackwards/forks/nipyapi
collecting ... collected 1 item

test_canvas.py::test_deser_flow FAILED                                   [100%]
tests/test_canvas.py:36 (test_deser_flow)
def test_deser_flow():
        r = canvas.get_flow('root')
        assert isinstance(r, ProcessGroupFlowEntity)
>       s = utils.dump(r,'json')

test_canvas.py:40: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../nipyapi/utils.py:54: in dump
    raise e
../nipyapi/utils.py:47: in dump
    out = json.dumps(
/Users/ottobackwards/.pyenv/versions/3.8.2/lib/python3.8/json/__init__.py:234: in dumps
    return cls(
/Users/ottobackwards/.pyenv/versions/3.8.2/lib/python3.8/json/encoder.py:201: in encode
    chunks = list(chunks)
/Users/ottobackwards/.pyenv/versions/3.8.2/lib/python3.8/json/encoder.py:438: in _iterencode
    o = _default(o)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <json.encoder.JSONEncoder object at 0x1143dc9d0>
o = {'permissions': {'can_read': True, 'can_write': True},
 'process_group_flow': {'breadcrumb': {'breadcrumb': {'id': '66...                     'uri': 'http://localhost:8080/nifi-api/flow/process-groups/6619ee4f-0175-1000-125a-4d56b49f15ea'}}

    def default(self, o):
        """Implement this method in a subclass such that it returns
        a serializable object for ``o``, or calls the base implementation
        (to raise a ``TypeError``).

        For example, to support arbitrary iterators, you could
        implement default like this::

            def default(self, o):
                try:
                    iterable = iter(o)
                except TypeError:
                    pass
                else:
                    return list(iterable)
                # Let the base class default method raise the TypeError
                return JSONEncoder.default(self, o)

        """
>       raise TypeError(f'Object of type {o.__class__.__name__} '
                        f'is not JSON serializable')
E       TypeError: Object of type ProcessGroupFlowEntity is not JSON serializable

/Users/ottobackwards/.pyenv/versions/3.8.2/lib/python3.8/json/encoder.py:179: TypeError

Assertion failed

=================================== FAILURES ===================================
_______________________________ test_deser_flow ________________________________

    def test_deser_flow():
        r = canvas.get_flow('root')
        assert isinstance(r, ProcessGroupFlowEntity)
>       s = utils.dump(r,'json')

test_canvas.py:40: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../nipyapi/utils.py:54: in dump
    raise e
../nipyapi/utils.py:47: in dump
    out = json.dumps(
/Users/ottobackwards/.pyenv/versions/3.8.2/lib/python3.8/json/__init__.py:234: in dumps
    return cls(
/Users/ottobackwards/.pyenv/versions/3.8.2/lib/python3.8/json/encoder.py:201: in encode
    chunks = list(chunks)
/Users/ottobackwards/.pyenv/versions/3.8.2/lib/python3.8/json/encoder.py:438: in _iterencode
    o = _default(o)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <json.encoder.JSONEncoder object at 0x1143dc9d0>
o = {'permissions': {'can_read': True, 'can_write': True},
 'process_group_flow': {'breadcrumb': {'breadcrumb': {'id': '66...                     'uri': 'http://localhost:8080/nifi-api/flow/process-groups/6619ee4f-0175-1000-125a-4d56b49f15ea'}}

    def default(self, o):
        """Implement this method in a subclass such that it returns
        a serializable object for ``o``, or calls the base implementation
        (to raise a ``TypeError``).

        For example, to support arbitrary iterators, you could
        implement default like this::

            def default(self, o):
                try:
                    iterable = iter(o)
                except TypeError:
                    pass
                else:
                    return list(iterable)
                # Let the base class default method raise the TypeError
                return JSONEncoder.default(self, o)

        """
>       raise TypeError(f'Object of type {o.__class__.__name__} '
                        f'is not JSON serializable')
E       TypeError: Object of type ProcessGroupFlowEntity is not JSON serializable

/Users/ottobackwards/.pyenv/versions/3.8.2/lib/python3.8/json/encoder.py:179: TypeError
=========================== short test summary info ============================
FAILED test_canvas.py::test_deser_flow - TypeError: Object of type ProcessGro...
============================== 1 failed in 3.55s ===============================

Process finished with exit code 1

Assertion failed

Assertion failed

What I Did

Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.

Urgency

Please give a brief description of how critical this issue is to you.
For example, if it's blocking your Production environment, or perhaps you are just notifying us of something you found but isn't blocking your workflow.

ottobackwards commented 3 years ago

the following works:

def test_deser_flow():
    r = canvas.get_flow('root')
    assert isinstance(r, ProcessGroupFlowEntity)
    obj = config.nifi_config.api_client.sanitize_for_serialization(r)
    s = utils.dump(obj, 'json')
    f = utils.load(s,('nifi', 'ProcessGroupFlowEntity'))
    assert isinstance(f, ProcessGroupFlowEntity)
Chaffelson commented 3 years ago

Closing issue as fixed with PR, now released in 0.16