Juan Hernandez has uploaded a new change for review.

Change subject: restapi: Empty String Validation (#1005296)
......................................................................

restapi: Empty String Validation (#1005296)

The generic input validation method in the API - validateParameters() uses 
reflection
to invoke isSetX() for the given field. In the particular case of when the 
filed is
a string, .isEmpty() check should be added. This is what this patch does.

This fix is done in the context of a specific bug - 1005296 - which complains
about empty <address> field passing validation when creating a new storage
server connection

Bug-Url: http://bugzilla.redhat.com/1005296
Change-Id: Ib0556725ad2ddc607d587c079bad451e4b74972d
Signed-off-by: Ori Liel <ol...@redhat.com>
(cherry picked from commit 0107892b7c0b3a6c0ccad0da0c077a1966fc1f8a)
---
M 
backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/ReflectionHelper.java
1 file changed, 25 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/45/24545/1

diff --git 
a/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/ReflectionHelper.java
 
b/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/ReflectionHelper.java
index 1b95961..b6c4040 100644
--- 
a/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/ReflectionHelper.java
+++ 
b/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/ReflectionHelper.java
@@ -78,12 +78,35 @@
     }
 
     public static boolean isSet(Object o, String name) {
+        boolean set = false;
         if(o != null){
             Method m = getMethod(o, IS_SET_ROOT + name);
             Object ret = invoke(o, m);
-            return ret != null && ret instanceof Boolean && 
((Boolean)ret).booleanValue();
+            if (ret != null && ret instanceof Boolean && ((Boolean) 
ret).booleanValue()) {
+                // (isSetX() method only tells us if the value is not null).
+                // for Strings we also have to check that the value is not 
empty.
+                if (getReturnType(o, name).equals(String.class)) {
+                    Object result = invoke(o, getGetter(o, name));
+                    String resultAsString = (String) result;
+                    if (!resultAsString.isEmpty()) {
+                        set = true;
+                    }
+                } else {
+                    set = true;
+                }
+            }
         }
-        return false;
+        return set;
+    }
+
+    public static Method getGetter(Object o, String name) {
+        String getterName = GET_ROOT + capitalize(name);
+        return getMethod(o, getterName);
+    }
+
+    public static Class<?> getReturnType(Object o, String name) {
+        Method getter = getGetter(o, name);
+        return getter.getReturnType();
     }
 
     public static boolean different(Object lhs, Object rhs, String name) {


-- 
To view, visit http://gerrit.ovirt.org/24545
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib0556725ad2ddc607d587c079bad451e4b74972d
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: ovirt-engine-3.4
Gerrit-Owner: Juan Hernandez <juan.hernan...@redhat.com>
Gerrit-Reviewer: Ori Liel <ol...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to