Skip to content

Commit d7271b1

Browse files
author
Gauvain Pocentek
committed
Fix the json() method for python 3
Also add unit tests and fix pep8 test
1 parent 982f54f commit d7271b1

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

gitlab/objects.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@
2626

2727
import six
2828

29+
import gitlab
2930
from gitlab.exceptions import * # noqa
3031

3132

3233
class jsonEncoder(json.JSONEncoder):
3334
def default(self, obj):
34-
from gitlab import Gitlab
3535
if isinstance(obj, GitlabObject):
36-
return {k: v for k, v in obj.__dict__.iteritems()
37-
if not isinstance(v, BaseManager)}
38-
elif isinstance(obj, Gitlab):
36+
return {k: v for k, v in six.iteritems(obj.__dict__)
37+
if (not isinstance(v, BaseManager)
38+
and not k[0] == '_')}
39+
elif isinstance(obj, gitlab.Gitlab):
3940
return {'url': obj._url}
4041
return json.JSONEncoder.default(self, obj)
4142

gitlab/tests/test_gitlabobject.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from __future__ import division
2222
from __future__ import absolute_import
2323

24+
import json
2425
try:
2526
import unittest
2627
except ImportError:
@@ -150,6 +151,14 @@ def setUp(self):
150151
email="testuser@test.com", password="testpassword",
151152
ssl_verify=True)
152153

154+
def test_json(self):
155+
gl_object = CurrentUser(self.gl, data={"username": "testname"})
156+
json_str = gl_object.json()
157+
data = json.loads(json_str)
158+
self.assertIn("id", data)
159+
self.assertEqual(data["username"], "testname")
160+
self.assertEqual(data["gitlab"]["url"], "http://localhost/api/v3")
161+
153162
def test_list_not_implemented(self):
154163
self.assertRaises(NotImplementedError, CurrentUser.list, self.gl)
155164

0 commit comments

Comments
 (0)