thefactory/marathon-python

Marathon Json encoder can't handle unicode strings

Closed this issue · 2 comments

@kevinschoon I believe I have found a regression and traced back to #32. Probably efbaa67

We used to be able to have python-style Unicode strings like:

'image': u'localhost/fake_docker_url'

But this now produces this stacktrace:

     Traceback (most recent call last):
        File "/nail/home/kwa/Projects/paasta_tools/.tox/paasta_itests/local/lib/python2.7/site-packages/behave/model.py", line 1173, in run
          match.run(runner.context)
        File "/nail/home/kwa/Projects/paasta_tools/.tox/paasta_itests/local/lib/python2.7/site-packages/behave/model.py", line 1589, in run
          self.func(context, *args, **kwargs)
        File "/nail/home/kwa/Projects/paasta_tools/paasta_itests/steps/setup_marathon_job_steps.py", line 42, in create_complete_app
          fake_service_marathon_config,
        File "/nail/home/kwa/Projects/paasta_tools/paasta_tools/setup_marathon_job.py", line 300, in setup_service
          service_marathon_config.get_bounce_health_params(),
        File "/nail/home/kwa/Projects/paasta_tools/paasta_tools/setup_marathon_job.py", line 241, in deploy_service
          bounce_method, serviceinstance, cluster, instance_name, marathon_jobid, client)
        File "/nail/home/kwa/Projects/paasta_tools/paasta_tools/setup_marathon_job.py", line 138, in do_bounce
          bounce_lib.create_marathon_app(marathon_jobid, config, client)
        File "/nail/home/kwa/Projects/paasta_tools/paasta_tools/bounce_lib.py", line 159, in create_marathon_app
          client.create_app(app_id, MarathonApp(**config))
        File "/nail/home/kwa/Projects/paasta_tools/.tox/paasta_itests/local/lib/python2.7/site-packages/marathon/client.py", line 111, in create_app
          data = app.to_json()
        File "/nail/home/kwa/Projects/paasta_tools/.tox/paasta_itests/local/lib/python2.7/site-packages/marathon/models/base.py", line 41, in to_json
          return json.dumps(self.json_repr(minimal=True), cls=MarathonMinimalJsonEncoder, sort_keys=True)
        File "/usr/lib64/python2.7/json/__init__.py", line 250, in dumps
          sort_keys=sort_keys, **kw).encode(obj)
        File "/usr/lib64/python2.7/json/encoder.py", line 209, in encode
          chunks = list(chunks)
        File "/usr/lib64/python2.7/json/encoder.py", line 434, in _iterencode
          for chunk in _iterencode_dict(o, _current_indent_level):
        File "/usr/lib64/python2.7/json/encoder.py", line 408, in _iterencode_dict
          for chunk in chunks:
        File "/usr/lib64/python2.7/json/encoder.py", line 442, in _iterencode
          o = _default(o)
        File "/nail/home/kwa/Projects/paasta_tools/.tox/paasta_itests/local/lib/python2.7/site-packages/marathon/util.py", line 36, in default
          return self.default(obj.json_repr(minimal=True))
        File "/nail/home/kwa/Projects/paasta_tools/.tox/paasta_itests/local/lib/python2.7/site-packages/marathon/util.py", line 43, in default
          return {k: self.default(v) for k,v in obj.items() if (v or v == False)}
        File "/nail/home/kwa/Projects/paasta_tools/.tox/paasta_itests/local/lib/python2.7/site-packages/marathon/util.py", line 43, in <dictcomp>
          return {k: self.default(v) for k,v in obj.items() if (v or v == False)}
        File "/nail/home/kwa/Projects/paasta_tools/.tox/paasta_itests/local/lib/python2.7/site-packages/marathon/util.py", line 36, in default
          return self.default(obj.json_repr(minimal=True))
        File "/nail/home/kwa/Projects/paasta_tools/.tox/paasta_itests/local/lib/python2.7/site-packages/marathon/util.py", line 43, in default
          return {k: self.default(v) for k,v in obj.items() if (v or v == False)}
        File "/nail/home/kwa/Projects/paasta_tools/.tox/paasta_itests/local/lib/python2.7/site-packages/marathon/util.py", line 43, in <dictcomp>
          return {k: self.default(v) for k,v in obj.items() if (v or v == False)}
        File "/nail/home/kwa/Projects/paasta_tools/.tox/paasta_itests/local/lib/python2.7/site-packages/marathon/util.py", line 45, in default
          return [self.default(e) for e in obj if (e or e == False)]
        File "/nail/home/kwa/Projects/paasta_tools/.tox/paasta_itests/local/lib/python2.7/site-packages/marathon/util.py", line 45, in default
          return [self.default(e) for e in obj if (e or e == False)]

If I remove the u, this no longer happens.

Do you know why this might be a problem?

cc @mrtyler @keshavdv

@solarkennedy Sorry for the troubles. Can you let me know how to reproduce this and I'll try to come up with a fix. Any particular reason you need to pass unicode literals? It looks like this might be the cause but I'm not 100% sure.

Yea, I think that it probably it.

In this case it is not a string, but it is a Unicode something.

This is very easy to reproduce with the CI we have now. Here is a PR:
#51