diff --git a/doc/changes.rst b/doc/changes.rst index 98b93f6..0a85801 100644 --- a/doc/changes.rst +++ b/doc/changes.rst @@ -8,6 +8,7 @@ Changes * Allow disabling complex types auto-register * Documentation edits * Various documentation build fixes +* Fix passing Dict and Array based UserType as params 0.6.1 (2014-05-02) ------------------ diff --git a/wsme/rest/args.py b/wsme/rest/args.py index 0c4f6cf..0c6e1c6 100644 --- a/wsme/rest/args.py +++ b/wsme/rest/args.py @@ -165,6 +165,14 @@ def dict_from_params(datatype, params, path, hit_paths): for key in keys)) +@from_params.when_type(UserType) +def usertype_from_params(datatype, params, path, hit_paths): + value = from_params(datatype.basetype, params, path, hit_paths) + if value is not Unset: + return datatype.frombasetype(value) + return Unset + + def args_from_args(funcdef, args, kwargs): newargs = [] for argdef, arg in zip(funcdef.arguments[:len(args)], args): diff --git a/wsme/tests/test_protocols_commons.py b/wsme/tests/test_protocols_commons.py index 6a4c1a8..5cb0268 100644 --- a/wsme/tests/test_protocols_commons.py +++ b/wsme/tests/test_protocols_commons.py @@ -14,6 +14,10 @@ class MyUserType(UserType): basetype = str +class DictBasedUserType(UserType): + basetype = DictType(int, int) + + class TestProtocolsCommons(unittest.TestCase): def test_from_param_date(self): assert from_param(datetime.date, '2008-02-28') == \ @@ -55,6 +59,15 @@ class TestProtocolsCommons(unittest.TestCase): def test_from_params_dict_unset(self): assert from_params(DictType(int, str), {}, 'a', set()) is Unset + def test_from_params_usertype(self): + value = from_params( + DictBasedUserType(), + {'a[2]': '2'}, + 'a', + set() + ) + self.assertEqual(value, {2: 2}) + def test_args_from_args_usertype(self): class FakeType(UserType):