Skip to content

Commit

Permalink
Fix to suppress field validation in case the path is not present. Als…
Browse files Browse the repository at this point in the history
…o test cases update.
  • Loading branch information
deepakarora3 committed Nov 5, 2024
1 parent 2f6aa2c commit b2985dc
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 40 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<groupId>com.americanexpress.unify.jdocs</groupId>
<artifactId>unify-jdocs</artifactId>
<version>1.7.0</version>
<version>1.7.1</version>
<packaging>jar</packaging>

<name>unify-jdocs</name>
Expand Down Expand Up @@ -108,6 +108,12 @@
<version>${jfiglet.version}</version>
</dependency>

<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>0.10.4</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ JDocs is available as a jar file in Maven central with the following latest Mave
````pom
<groupId>com.americanexpress.unify.jdocs</groupId>
<artifactId>unify-jdocs</artifactId>
<version>1.7.0</version>
<version>1.7.1</version>
````

---
Expand Down
117 changes: 84 additions & 33 deletions src/main/java/com/americanexpress/unify/jdocs/JDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.node.*;
import io.vavr.Tuple2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -1020,15 +1021,17 @@ private void validatePath1(String path, CONSTS_JDOCS.API api, List<Token> tokenL
}
}

protected Object getValue(String path, Class clazz, List<Token> tokenList) {
protected Tuple2<Object, Boolean> getValue(String path, Class clazz, List<Token> tokenList) {
JsonNode node = null;
Object value = null;
Boolean isPathPresent = true;

while (true) {
node = traverse(rootNode, tokenList, false);

if (node == null) {
value = null;
isPathPresent = false;
break;
}

Expand Down Expand Up @@ -1112,7 +1115,7 @@ protected Object getValue(String path, Class clazz, List<Token> tokenList) {
break;
}

return value;
return new Tuple2(value, isPathPresent);
}

private void setLeafNode(ObjectNode node, String field, Object value) {
Expand Down Expand Up @@ -1442,8 +1445,12 @@ public Object getValue(String path, String... vargs) {
path = getStaticPath(path, vargs);
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET, PathAccessType.VALUE);
String modelPath = checkPathInModel(path, tokenList);
Object value = getValue(path, null, tokenList);
checkFieldValue(path, modelPath, value, false);
Tuple2<Object, Boolean> tuple2 = getValue(path, null, tokenList);
Object value = tuple2._1;
boolean isPathPresent = tuple2._2;
if (isPathPresent == true) {
checkFieldValue(path, modelPath, value, false);
}
return value;
}

Expand All @@ -1452,8 +1459,12 @@ public String getString(String path, String... vargs) {
path = getStaticPath(path, vargs);
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET, PathAccessType.VALUE);
String modelPath = checkPathInModel(path, tokenList);
String value = (String)getValue(path, String.class, tokenList);
checkFieldValue(path, modelPath, value, false);
Tuple2<Object, Boolean> tuple2 = getValue(path, String.class, tokenList);
String value = (String)tuple2._1;
boolean isPathPresent = tuple2._2;
if (isPathPresent == true) {
checkFieldValue(path, modelPath, value, false);
}
return value;
}

Expand All @@ -1479,8 +1490,12 @@ public Integer getInteger(String path, String... vargs) {
path = getStaticPath(path, vargs);
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET, PathAccessType.VALUE);
String modelPath = checkPathInModel(path, tokenList);
Integer value = (Integer)getValue(path, Integer.class, tokenList);
checkFieldValue(path, modelPath, value, false);
Tuple2<Object, Boolean> tuple2 = getValue(path, Integer.class, tokenList);
Integer value = (Integer)tuple2._1;
boolean isPathPresent = tuple2._2;
if (isPathPresent == true) {
checkFieldValue(path, modelPath, value, false);
}
return value;
}

Expand All @@ -1489,8 +1504,12 @@ public Boolean getBoolean(String path, String... vargs) {
path = getStaticPath(path, vargs);
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET, PathAccessType.VALUE);
String modelPath = checkPathInModel(path, tokenList);
Boolean value = (Boolean)getValue(path, Boolean.class, tokenList);
checkFieldValue(path, modelPath, value, false);
Tuple2<Object, Boolean> tuple2 = getValue(path, Boolean.class, tokenList);
Boolean value = (Boolean)tuple2._1;
boolean isPathPresent = tuple2._2;
if (isPathPresent == true) {
checkFieldValue(path, modelPath, value, false);
}
return value;
}

Expand All @@ -1499,8 +1518,12 @@ public Long getLong(String path, String... vargs) {
path = getStaticPath(path, vargs);
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET, PathAccessType.VALUE);
String modelPath = checkPathInModel(path, tokenList);
Long value = (Long)getValue(path, Long.class, tokenList);
checkFieldValue(path, modelPath, value, false);
Tuple2<Object, Boolean> tuple2 = getValue(path, Long.class, tokenList);
Long value = (Long)tuple2._1;
boolean isPathPresent = tuple2._2;
if (isPathPresent == true) {
checkFieldValue(path, modelPath, value, false);
}
return value;
}

Expand All @@ -1509,8 +1532,12 @@ public BigDecimal getBigDecimal(String path, String... vargs) {
path = getStaticPath(path, vargs);
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET, PathAccessType.VALUE);
String modelPath = checkPathInModel(path, tokenList);
BigDecimal value = (BigDecimal)getValue(path, BigDecimal.class, tokenList);
checkFieldValue(path, modelPath, value, false);
Tuple2<Object, Boolean> tuple2 = getValue(path, BigDecimal.class, tokenList);
BigDecimal value = (BigDecimal)tuple2._1;
boolean isPathPresent = tuple2._2;
if (isPathPresent == true) {
checkFieldValue(path, modelPath, value, false);
}
return value;
}

Expand All @@ -1519,8 +1546,12 @@ public Object getArrayValue(String path, String... vargs) {
path = getStaticPath(path, vargs);
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET_ARRAY_VALUE, PathAccessType.VALUE);
String modelPath = checkPathInModel(path, tokenList);
Object value = getValue(path, null, tokenList);
checkFieldValue(path, modelPath, value, true);
Tuple2<Object, Boolean> tuple2 = getValue(path, null, tokenList);
Object value = tuple2._1;
boolean isPathPresent = tuple2._2;
if (isPathPresent == true) {
checkFieldValue(path, modelPath, value, true);
}
return value;
}

Expand All @@ -1529,8 +1560,12 @@ public String getArrayValueString(String path, String... vargs) {
path = getStaticPath(path, vargs);
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET_ARRAY_VALUE, PathAccessType.VALUE);
String modelPath = checkPathInModel(path, tokenList);
String value = (String)getValue(path, String.class, tokenList);
checkFieldValue(path, modelPath, value, true);
Tuple2<Object, Boolean> tuple2 = getValue(path, String.class, tokenList);
String value = (String)tuple2._1;
boolean isPathPresent = tuple2._2;
if (isPathPresent == true) {
checkFieldValue(path, modelPath, value, true);
}
return value;
}

Expand All @@ -1539,8 +1574,12 @@ public Integer getArrayValueInteger(String path, String... vargs) {
path = getStaticPath(path, vargs);
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET_ARRAY_VALUE, PathAccessType.VALUE);
String modelPath = checkPathInModel(path, tokenList);
Integer value = (Integer)getValue(path, Integer.class, tokenList);
checkFieldValue(path, modelPath, value, true);
Tuple2<Object, Boolean> tuple2 = getValue(path, Integer.class, tokenList);
Integer value = (Integer)tuple2._1;
boolean isPathPresent = tuple2._2;
if (isPathPresent == true) {
checkFieldValue(path, modelPath, value, true);
}
return value;
}

Expand All @@ -1549,8 +1588,12 @@ public Boolean getArrayValueBoolean(String path, String... vargs) {
path = getStaticPath(path, vargs);
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET_ARRAY_VALUE, PathAccessType.VALUE);
String modelPath = checkPathInModel(path, tokenList);
Boolean value = (Boolean)getValue(path, Boolean.class, tokenList);
checkFieldValue(path, modelPath, value, true);
Tuple2<Object, Boolean> tuple2 = getValue(path, Boolean.class, tokenList);
Boolean value = (Boolean)tuple2._1;
boolean isPathPresent = tuple2._2;
if (isPathPresent == true) {
checkFieldValue(path, modelPath, value, true);
}
return value;
}

Expand All @@ -1559,8 +1602,12 @@ public Long getArrayValueLong(String path, String... vargs) {
path = getStaticPath(path, vargs);
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET_ARRAY_VALUE, PathAccessType.VALUE);
String modelPath = checkPathInModel(path, tokenList);
Long value = (Long)getValue(path, Long.class, tokenList);
checkFieldValue(path, modelPath, value, true);
Tuple2<Object, Boolean> tuple2 = getValue(path, Long.class, tokenList);
Long value = (Long)tuple2._1;
boolean isPathPresent = tuple2._2;
if (isPathPresent == true) {
checkFieldValue(path, modelPath, value, true);
}
return value;
}

Expand All @@ -1569,8 +1616,12 @@ public BigDecimal getArrayValueBigDecimal(String path, String... vargs) {
path = getStaticPath(path, vargs);
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET_ARRAY_VALUE, PathAccessType.VALUE);
String modelPath = checkPathInModel(path, tokenList);
BigDecimal value = (BigDecimal)getValue(path, BigDecimal.class, tokenList);
checkFieldValue(path, modelPath, value, true);
Tuple2<Object, Boolean> tuple2 = getValue(path, BigDecimal.class, tokenList);
BigDecimal value = (BigDecimal)tuple2._1;
boolean isPathPresent = tuple2._2;
if (isPathPresent == true) {
checkFieldValue(path, modelPath, value, true);
}
return value;
}

Expand Down Expand Up @@ -2304,7 +2355,7 @@ private void throwExceptionOrSetErrorList(String errorCode, String path, List<St
else {
String msg = ERRORS_JDOCS.getErrorMessage(errorCode);
if (msg == null) {
logger.error("Error code {} not found in ErrorMap", errorCode);
logger.warn("Error code {} not found in ErrorMap", errorCode);
msg = "";
}
else {
Expand Down Expand Up @@ -2516,7 +2567,7 @@ private void validateFilterNames(String path, List<Token> tokenList) {

case BOOLEAN:
if (BaseUtils.compareWithMany(fieldValue, "true", "false") == true) {
value = new Boolean(fieldValue);
value = Boolean.valueOf(fieldValue);
}
else {
throw new UnifyException("jdoc_err_53", fieldName, path);
Expand All @@ -2528,11 +2579,11 @@ private void validateFilterNames(String path, List<Token> tokenList) {
break;

case INTEGER:
value = new Integer(fieldValue);
value = Integer.valueOf(fieldValue);
break;

case LONG:
value = new Long(fieldValue);
value = Long.valueOf(fieldValue);
break;

case DECIMAL:
Expand Down Expand Up @@ -2725,7 +2776,7 @@ private void setFilterFieldNode(ObjectNode filterNode, String filterField, Strin

case BOOLEAN:
if (BaseUtils.compareWithMany(filterValue, "true", "false") == true) {
filterNode.put(filterField, new Boolean(filterValue));
filterNode.put(filterField, Boolean.valueOf(filterValue));
}
else {
throw new UnifyException("jdoc_err_53", filterField, path);
Expand All @@ -2737,11 +2788,11 @@ private void setFilterFieldNode(ObjectNode filterNode, String filterField, Strin
break;

case INTEGER:
filterNode.put(filterField, new Integer(filterValue));
filterNode.put(filterField, Integer.valueOf(filterValue));
break;

case LONG:
filterNode.put(filterField, new Long(filterValue));
filterNode.put(filterField, Long.valueOf(filterValue));
break;

case DECIMAL:
Expand Down
36 changes: 31 additions & 5 deletions src/test/java/com/americanexpress/unify/jdocs/DocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ void testMisc() {
d.setString("$.[1].name", "Nitika");
Document d1 = new JDocument();
d1.setContent(d, "$.[]", "$.application[]");

{
// this will run ok
d = new JDocument();
d.setString("$.pod_info.fname", "Deepak");
d.setString("$.pod_info.lname", "Arora");
Document rd = new JDocument("[]");
rd.setContent(d, "$.pod_info", "$.[0]");
}
}

@Test
Expand All @@ -150,12 +159,12 @@ void testRead() {
assertEquals("9999999999", d.getString("$.members[sex=male].phones[type=mobile].number"));

// check int and long reads
assertEquals(new Integer(0), d.getInteger("$.members[0].index"));
assertEquals(new Long(0), d.getLong("$.members[0].index"));
assertEquals(Integer.valueOf(0), d.getInteger("$.members[0].index"));
assertEquals(Long.valueOf(0), d.getLong("$.members[0].index"));

// check boolean reads
assertEquals(new Boolean(true), d.getBoolean("$.members[0].is_married"));
assertEquals(new Boolean(false), d.getBoolean("$.members[1].is_married"));
assertEquals(Boolean.valueOf(true), d.getBoolean("$.members[0].is_married"));
assertEquals(Boolean.valueOf(false), d.getBoolean("$.members[1].is_married"));

// check null read
assertEquals(null, d.getString("$.info.iid"));
Expand Down Expand Up @@ -411,7 +420,7 @@ void testNativeArrayValue() {
assertEquals(s, "AZ");

Integer code = (Integer)d.getArrayValue("$.valid_states[0].codes[0]");
assertEquals(code, new Integer(1));
assertEquals(code, Integer.valueOf(1));
}

@Test
Expand Down Expand Up @@ -1443,4 +1452,21 @@ void testMissingParams() {

}

@Test
void testNumber() {
try {
// all primitive types at root are valid JSONs
Document d = new JDocument("14");
// use below to read the value of such a JSON document
// String s = d.getJson();
// long value = Long.valueOf(s);
d = new JDocument("14.1");
d = new JDocument("true");
d = new JDocument("\"hello\"");
}
catch (Exception e) {
assert(false);
}
}

}

0 comments on commit b2985dc

Please sign in to comment.