From 2cb266eadb8a0147328433ea85370cb68cf30e6f Mon Sep 17 00:00:00 2001
From: Mehdi Abaakouk <sileht@sileht.net>
Date: Mon, 3 Aug 2015 14:30:31 +0200
Subject: [PATCH] Fixes exception path with the datatype is a Object

If the object type is ArrayType or DictType, the datatype
is an object not a class. Currently a 500 error is raise
just because the exception path for invalid input handle only
when the datatype is an UserType or Class, not object.

This change fixes that.

Change-Id: Ifadef698a4dca0d33167bd4d5a567c43fe015108
Closes-bug: #1428185
---
 wsme/rest/args.py                    |  4 +++-
 wsme/tests/test_protocols_commons.py | 17 ++++++++++++++++-
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/wsme/rest/args.py b/wsme/rest/args.py
index 80613e2..55fb4eb 100644
--- a/wsme/rest/args.py
+++ b/wsme/rest/args.py
@@ -181,8 +181,10 @@ def args_from_args(funcdef, args, kwargs):
         except Exception:
             if isinstance(argdef.datatype, UserType):
                 datatype_name = argdef.datatype.name
-            else:
+            elif isinstance(argdef.datatype, type):
                 datatype_name = argdef.datatype.__name__
+            else:
+                datatype_name = argdef.datatype.__class__.__name__
             raise InvalidInput(
                 argdef.name,
                 arg,
diff --git a/wsme/tests/test_protocols_commons.py b/wsme/tests/test_protocols_commons.py
index 5cb0268..6d99c45 100644
--- a/wsme/tests/test_protocols_commons.py
+++ b/wsme/tests/test_protocols_commons.py
@@ -7,7 +7,11 @@ from wsme.api import FunctionArgument, FunctionDefinition
 from wsme.rest.args import from_param, from_params, args_from_args
 from wsme.exc import InvalidInput
 
-from wsme.types import UserType, Unset, ArrayType, DictType
+from wsme.types import UserType, Unset, ArrayType, DictType, Base
+
+
+class MyBaseType(Base):
+    test = str
 
 
 class MyUserType(UserType):
@@ -89,6 +93,17 @@ class TestProtocolsCommons(unittest.TestCase):
         else:
             self.fail('Should have thrown an InvalidInput')
 
+    def test_args_from_args_array_type(self):
+        fake_type = ArrayType(MyBaseType)
+        fd = FunctionDefinition(FunctionDefinition)
+        fd.arguments.append(FunctionArgument('fake-arg', fake_type, True, []))
+        try:
+            args_from_args(fd, [['invalid-argument']], {})
+        except InvalidInput as e:
+            assert ArrayType.__name__ in str(e)
+        else:
+            self.fail('Should have thrown an InvalidInput')
+
 
 class ArgTypeConversion(unittest.TestCase):