aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTommy Yu <tommy.yu@auckland.ac.nz>2019-07-11 22:27:22 +1200
committerTommy Yu <tommy.yu@auckland.ac.nz>2019-07-11 22:38:12 +1200
commitabb1308fa8b7c50eb79e9f07ee448a76c7449847 (patch)
tree8d7e81c3dcf0fecd39c4f00c8947ab49de23e90e
parent21afcf2c35f21034124a230abebd1157cbee8265 (diff)
downloaduritemplates-abb1308fa8b7c50eb79e9f07ee448a76c7449847.tar.gz
Correct handling of empty/undef in list values
- Expansion of a list including `None` values, violating 3.2.1 as the undefined value is not ignored, e.g. >>> expand('/test{/x*}', x=['one', None, 'three']) '/test/one/None/three' - A list with empty string was ignored, violating 2.3 as this should not be tread as undefined, example: >>> expand('/test{/x}', x=[]) '/test' >>> expand('/test{/x}', x='') '/test/' >>> expand('/test{/x}', x=['']) '/test' # should be /test/ >>> expand('/test{/x}', x=['', '']) '/test/,' >>> expand('/test{/x*}', x=['', '']) '/test//' >>> expand('/test{/x*}', x=['']) '/test'
-rw-r--r--tests/test_uritemplate.py12
-rw-r--r--uritemplate/variable.py6
2 files changed, 14 insertions, 4 deletions
diff --git a/tests/test_uritemplate.py b/tests/test_uritemplate.py
index 9977ef7..f67ad26 100644
--- a/tests/test_uritemplate.py
+++ b/tests/test_uritemplate.py
@@ -470,12 +470,24 @@ class TestURITemplate(RFCTemplateExamples('RFCMeta', (TestCase,), {})):
'foo', [], True, '/'), None
)
self.assertEqual(t.variables[0]._label_path_expansion(
+ 'foo', [None], True, '/'), None
+ )
+ self.assertEqual(t.variables[0]._label_path_expansion(
+ 'foo', [None, None], True, '/'), None
+ )
+ self.assertEqual(t.variables[0]._label_path_expansion(
'foo', ['one'], True, '/'), 'one'
)
self.assertEqual(t.variables[0]._label_path_expansion(
'foo', ['one', 'two'], True, '/'), 'one/two'
)
self.assertEqual(t.variables[0]._label_path_expansion(
+ 'foo', ['one', None, 'two'], True, '/'), 'one/two'
+ )
+ self.assertEqual(t.variables[0]._label_path_expansion(
+ 'foo', [''], True, '/'), ''
+ )
+ self.assertEqual(t.variables[0]._label_path_expansion(
'foo', ['', ''], True, '/'), '/'
)
diff --git a/uritemplate/variable.py b/uritemplate/variable.py
index a3bd4ce..ce3f652 100644
--- a/uritemplate/variable.py
+++ b/uritemplate/variable.py
@@ -200,10 +200,8 @@ class URIVariable(object):
if not explode:
join_str = ','
- expanded = join_str.join(
- quote(v, safe) for v in value if value is not None
- )
- return expanded if expanded else None
+ fragments = [quote(v, safe) for v in value if v is not None]
+ return join_str.join(fragments) if fragments else None
if dict_test(value) or tuples:
items = items or sorted(value.items())