Skip to content

Commit 466d594

Browse files
author
Gabriel Monroy
committed
Merge pull request #1871 from gabrtv/fix-config-ints
Fix integer handling when encoding configuration
2 parents 8297faa + ffd2d8a commit 466d594

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

api/tests/test_config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ def test_config_set_unicode(self):
148148
self.assertEqual(response.status_code, 201)
149149
self.assertIn('POWERED_BY', json.loads(response.data['values']))
150150
self.assertEqual(json.loads(response.data['values'])['POWERED_BY'], 'Кроликов')
151+
# set an integer to test unicode regression
152+
body = {'values': json.dumps({'INTEGER': 1})}
153+
response = self.client.post(url, json.dumps(body), content_type='application/json')
154+
self.assertEqual(response.status_code, 201)
155+
self.assertIn('INTEGER', json.loads(response.data['values']))
156+
self.assertEqual(json.loads(response.data['values'])['INTEGER'], 1)
151157

152158
@mock.patch('requests.post', mock_import_repository_task)
153159
def test_config_str(self):

api/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ def fingerprint(key):
108108
return ':'.join(a + b for a, b in zip(fp_plain[::2], fp_plain[1::2]))
109109

110110

111+
def encode(obj):
112+
"""Return UTF-8 encoding for string objects."""
113+
if isinstance(obj, basestring):
114+
return obj.encode('utf-8')
115+
else:
116+
return obj
117+
118+
111119
if __name__ == "__main__":
112120
import doctest
113121
doctest.testmod()

registry/private.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import urlparse
77
import uuid
88

9+
from django.conf import settings
910
from docker.utils import utils
1011

11-
from django.conf import settings
12+
from api.utils import encode
1213

1314

1415
def publish_release(source, config, target):
@@ -158,7 +159,6 @@ def _put_tag(image_id, repository_path, tag):
158159

159160
# utility functions
160161

161-
162162
def _construct_env(env, config):
163163
"Update current environment with latest config"
164164
new_env = []
@@ -168,10 +168,10 @@ def _construct_env(env, config):
168168
if k in config:
169169
# update values defined by config
170170
v = config.pop(k)
171-
new_env.append("{}={}".format(k.encode('utf-8'), v.encode('utf-8')))
171+
new_env.append("{}={}".format(encode(k), encode(v)))
172172
# add other config ENV items
173173
for k, v in config.items():
174-
new_env.append("{}={}".format(k.encode('utf-8'), v.encode('utf-8')))
174+
new_env.append("{}={}".format(encode(k), encode(v)))
175175
return new_env
176176

177177

0 commit comments

Comments
 (0)