Validator should report serialized field name 20/121620/1
authorJim Hahn <jrh3@att.com>
Tue, 1 Jun 2021 14:18:13 +0000 (10:18 -0400)
committerJim Hahn <jrh3@att.com>
Tue, 1 Jun 2021 14:20:05 +0000 (10:20 -0400)
The validator reports field names as they appear in the class rather
than as they appear on the wire, which may be confusing to the client.
Modified the code to use the serialized name instead.
Note: this will require tweaks to some junits in some of the other
policy repos.

Issue-ID: POLICY-3333
Change-Id: I867dafdc87cd78dec3d3c6fe0236a744284314a3
Signed-off-by: Jim Hahn <jrh3@att.com>
common-parameters/pom.xml
common-parameters/src/main/java/org/onap/policy/common/parameters/FieldValidator.java
common-parameters/src/test/java/org/onap/policy/common/parameters/TestFieldValidator.java

index e22b6ea..342cb98 100644 (file)
@@ -66,7 +66,6 @@
         <dependency>
             <groupId>com.google.code.gson</groupId>
             <artifactId>gson</artifactId>
-            <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>org.yaml</groupId>
index 51c5d57..d441c28 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.onap.policy.common.parameters;
 
+import com.google.gson.annotations.SerializedName;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.AnnotatedParameterizedType;
 import java.lang.reflect.AnnotatedType;
@@ -57,6 +58,11 @@ public class FieldValidator extends ValueValidator {
      */
     private final Field field;
 
+    /**
+     * Name of the field when serialized (i.e., as the client would know it).
+     */
+    private final String serializedName;
+
     /**
      * Method to retrieve the field's value.
      */
@@ -76,9 +82,13 @@ public class FieldValidator extends ValueValidator {
 
         String fieldName = field.getName();
         if (fieldName.contains("$")) {
+            serializedName = fieldName;
             return;
         }
 
+        SerializedName serAnnot = field.getAnnotation(SerializedName.class);
+        serializedName = (serAnnot != null ? serAnnot.value() : fieldName);
+
         validator.addValidators(this);
         addListValidator(validator);
         addMapValidator(validator);
@@ -184,7 +194,7 @@ public class FieldValidator extends ValueValidator {
         // get the value
         Object value = getValue(object, accessor);
 
-        validateValue(result, field.getName(), value);
+        validateValue(result, serializedName, value);
     }
 
     /**
index 29b4b0e..f8f9749 100644 (file)
@@ -23,6 +23,7 @@ package org.onap.policy.common.parameters;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
+import com.google.gson.annotations.SerializedName;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.util.List;
@@ -55,6 +56,7 @@ public class TestFieldValidator extends ValidatorUtil {
     @Getter
     private Map<@NotBlank String, @Min(1) Integer> intMap;
 
+    @SerializedName("annotated_key_map")
     @Getter
     private Map<@NotBlank String, Integer> annotatedKeyMap;
 
@@ -205,7 +207,7 @@ public class TestFieldValidator extends ValidatorUtil {
 
         annotatedKeyMap = Map.of(" ", -10);
         validator.validateField(result, this);
-        assertThat(result.getResult()).contains("blank").doesNotContain("-10");
+        assertThat(result.getResult()).contains("annotated_key_map", "blank").doesNotContain("-10");
 
         // only the value is annotated
         validator = new FieldValidator(bean, TestFieldValidator.class, getField("annotatedValueMap"));
@@ -218,7 +220,7 @@ public class TestFieldValidator extends ValidatorUtil {
 
         annotatedValueMap = Map.of(" ", -10);
         validator.validateField(result, this);
-        assertThat(result.getResult()).doesNotContain("blank").contains("\" \"", "-10");
+        assertThat(result.getResult()).doesNotContain("blank").contains("annotatedValueMap", "\" \"", "-10");
     }
 
     @Test