Skip to content

Commit 1e8dc75

Browse files
committed
get_value_validation_fix
1 parent 54b5383 commit 1e8dc75

File tree

5 files changed

+127
-73
lines changed

5 files changed

+127
-73
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<groupId>com.americanexpress.unify.jdocs</groupId>
2121
<artifactId>unify-jdocs</artifactId>
22-
<version>1.4.1</version>
22+
<version>1.5.0</version>
2323
<packaging>jar</packaging>
2424

2525
<name>unify-jdocs</name>

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ JDocs is available as a jar file in Maven central with the following latest Mave
1515
````pom
1616
<groupId>com.americanexpress.unify.jdocs</groupId>
1717
<artifactId>unify-jdocs</artifactId>
18-
<version>1.4.1</version>
18+
<version>1.5.0</version>
1919
````
2020

2121
---

src/main/java/com/americanexpress/unify/jdocs/ERRORS_JDOCS.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public static void load() {
9292
map.put("jdoc_err_69", "Path specified cannot be a leaf node. Path -> {0}");
9393
map.put("jdoc_err_70", "Empty value not allowed for a date at path -> {0}");
9494
map.put("jdoc_err_71", "Date format missing for path -> {0}");
95+
map.put("jdoc_err_73", "Type cannot be null or empty");
96+
map.put("jdoc_err_74", "The type of a document cannot be changed");
9597
}
9698

9799
}

src/main/java/com/americanexpress/unify/jdocs/JDocument.java

Lines changed: 92 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.americanexpress.unify.base.BaseUtils;
1818
import com.americanexpress.unify.base.CONSTS_BASE;
19+
import com.americanexpress.unify.base.ERRORS_BASE;
1920
import com.americanexpress.unify.base.UnifyException;
2021
import com.fasterxml.jackson.core.JsonParser;
2122
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -63,6 +64,9 @@ public class JDocument implements Document {
6364

6465
private boolean validateAtReadWriteOnly = false;
6566

67+
// variable that tells us if the document has been validated against its type. Only applicable for typed documents
68+
private boolean isValidated = false;
69+
6670
// logger
6771
private static final Logger logger = LoggerFactory.getLogger(JDocument.class);
6872

@@ -75,13 +79,12 @@ public class JDocument implements Document {
7579
private static final ObjectWriter objectWriter = objectMapper.writer(new DefaultPrettyPrinter().withObjectIndenter(new DefaultIndenter().withLinefeed("\n")));
7680

7781
public static void init() {
78-
// should be done once at the start
79-
ERRORS_JDOCS.load();
80-
JDocument.defaultValidateAtReadWriteOnly = false;
82+
init(false);
8183
}
8284

8385
public static void init(boolean defaultValidateAtReadWriteOnly) {
8486
// should be done once at the start
87+
ERRORS_BASE.load();
8588
ERRORS_JDOCS.load();
8689
JDocument.defaultValidateAtReadWriteOnly = defaultValidateAtReadWriteOnly;
8790
}
@@ -194,29 +197,25 @@ public boolean isTyped() {
194197

195198
@Override
196199
public void setType(String type) {
197-
if ((type != null) && (type.isEmpty() == false)) {
198-
199-
validateAtReadWriteOnly = defaultValidateAtReadWriteOnly;
200-
201-
if (validateAtReadWriteOnly == false) {
202-
validate(type);
203-
}
204-
205-
this.type = type;
206-
}
200+
setType(type, defaultValidateAtReadWriteOnly);
207201
}
208202

209203
@Override
210204
public void setType(String type, boolean validateAtReadWriteOnly) {
211-
if ((type != null) && (type.isEmpty() == false)) {
212-
this.validateAtReadWriteOnly = validateAtReadWriteOnly;
205+
if (BaseUtils.isNullOrEmpty(type) == true) {
206+
throw new UnifyException("jdoc_err_73");
207+
}
213208

214-
if (validateAtReadWriteOnly == false) {
215-
validate(type);
216-
}
209+
// if this is already a typed document and we are trying to set it to a different type throw an exception
210+
if ((this.type.isEmpty() == false) && (type.equals(this.type) == false)) {
211+
throw new UnifyException("jdoc_err_74");
212+
}
217213

218-
this.type = type;
214+
this.validateAtReadWriteOnly = validateAtReadWriteOnly;
215+
if (validateAtReadWriteOnly == false) {
216+
validate(type);
219217
}
218+
this.type = type;
220219
}
221220

222221
// Base document methods
@@ -243,6 +242,7 @@ public final void validate(String type) {
243242
}
244243
List<String> errorList = validate(((JDocument)md).rootNode, rootNode, "$.", type);
245244
processErrors(errorList);
245+
isValidated = true;
246246
}
247247

248248
private static JsonNode getMatchingArrayElementByField(ArrayNode node, String field, String value) {
@@ -1360,120 +1360,137 @@ protected List<Token> validatePath(String path, CONSTS_JDOCS.API api, PathAccess
13601360
public Object getValue(String path, String... vargs) {
13611361
path = getStaticPath(path, vargs);
13621362
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET, PathAccessType.VALUE);
1363-
if (isTyped()) {
1364-
checkPathExistsInModel(getModelPath(path));
1365-
}
1366-
return getValue(path, null, tokenList);
1363+
String modelPath = checkPathInModel(path, tokenList);
1364+
Object value = getValue(path, null, tokenList);
1365+
checkFieldValue(path, modelPath, value, false);
1366+
return value;
13671367
}
13681368

13691369
@Override
13701370
public String getString(String path, String... vargs) {
13711371
path = getStaticPath(path, vargs);
13721372
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET, PathAccessType.VALUE);
1373+
String modelPath = checkPathInModel(path, tokenList);
1374+
String value = (String)getValue(path, String.class, tokenList);
1375+
checkFieldValue(path, modelPath, value, false);
1376+
return value;
1377+
}
1378+
1379+
private void checkFieldValue(String path, String modelPath, Object value, boolean isValueArray) {
1380+
if ((isTyped() == true) && (isValidated == false) && (validateAtReadWriteOnly == true)) {
1381+
String format = getFieldFormat(path, modelPath, isValueArray);
1382+
validateField(format, value, modelPath, null, type);
1383+
}
1384+
}
1385+
1386+
private String checkPathInModel(String path, List<Token> tokenList) {
1387+
String modelPath = null;
13731388
if (isTyped()) {
1374-
checkPathExistsInModel(getModelPath(path));
1389+
validateFilterNames(path, tokenList);
1390+
modelPath = getModelPath(path);
1391+
checkPathExistsInModel(modelPath);
13751392
}
1376-
return (String)getValue(path, String.class, tokenList);
1393+
return modelPath;
13771394
}
13781395

13791396
@Override
13801397
public Integer getInteger(String path, String... vargs) {
13811398
path = getStaticPath(path, vargs);
13821399
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET, PathAccessType.VALUE);
1383-
if (isTyped()) {
1384-
checkPathExistsInModel(getModelPath(path));
1385-
}
1386-
return (Integer)getValue(path, Integer.class, tokenList);
1400+
String modelPath = checkPathInModel(path, tokenList);
1401+
Integer value = (Integer)getValue(path, Integer.class, tokenList);
1402+
checkFieldValue(path, modelPath, value, false);
1403+
return value;
13871404
}
13881405

13891406
@Override
13901407
public Boolean getBoolean(String path, String... vargs) {
13911408
path = getStaticPath(path, vargs);
13921409
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET, PathAccessType.VALUE);
1393-
if (isTyped()) {
1394-
checkPathExistsInModel(getModelPath(path));
1395-
}
1396-
return (Boolean)getValue(path, Boolean.class, tokenList);
1410+
String modelPath = checkPathInModel(path, tokenList);
1411+
Boolean value = (Boolean)getValue(path, Boolean.class, tokenList);
1412+
checkFieldValue(path, modelPath, value, false);
1413+
return value;
13971414
}
13981415

13991416
@Override
14001417
public Long getLong(String path, String... vargs) {
14011418
path = getStaticPath(path, vargs);
14021419
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET, PathAccessType.VALUE);
1403-
if (isTyped()) {
1404-
checkPathExistsInModel(getModelPath(path));
1405-
}
1406-
return (Long)getValue(path, Long.class, tokenList);
1420+
String modelPath = checkPathInModel(path, tokenList);
1421+
Long value = (Long)getValue(path, Long.class, tokenList);
1422+
checkFieldValue(path, modelPath, value, false);
1423+
return value;
14071424
}
14081425

14091426
@Override
14101427
public BigDecimal getBigDecimal(String path, String... vargs) {
14111428
path = getStaticPath(path, vargs);
14121429
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET, PathAccessType.VALUE);
1413-
if (isTyped()) {
1414-
checkPathExistsInModel(getModelPath(path));
1415-
}
1416-
return (BigDecimal)getValue(path, BigDecimal.class, tokenList);
1430+
String modelPath = checkPathInModel(path, tokenList);
1431+
BigDecimal value = (BigDecimal)getValue(path, BigDecimal.class, tokenList);
1432+
checkFieldValue(path, modelPath, value, false);
1433+
return value;
14171434
}
14181435

14191436
@Override
14201437
public Object getArrayValue(String path, String... vargs) {
14211438
path = getStaticPath(path, vargs);
14221439
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET_ARRAY_VALUE, PathAccessType.VALUE);
1423-
if (isTyped()) {
1424-
checkPathExistsInModel(getModelPath(path));
1425-
}
1426-
return getValue(path, null, tokenList);
1440+
String modelPath = checkPathInModel(path, tokenList);
1441+
Object value = getValue(path, null, tokenList);
1442+
checkFieldValue(path, modelPath, value, true);
1443+
return value;
14271444
}
14281445

14291446
@Override
14301447
public String getArrayValueString(String path, String... vargs) {
14311448
path = getStaticPath(path, vargs);
14321449
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET_ARRAY_VALUE, PathAccessType.VALUE);
1433-
if (isTyped()) {
1434-
checkPathExistsInModel(getModelPath(path));
1435-
}
1436-
return (String)getValue(path, String.class, tokenList);
1450+
String modelPath = checkPathInModel(path, tokenList);
1451+
String value = (String)getValue(path, String.class, tokenList);
1452+
checkFieldValue(path, modelPath, value, true);
1453+
return value;
14371454
}
14381455

14391456
@Override
14401457
public Integer getArrayValueInteger(String path, String... vargs) {
14411458
path = getStaticPath(path, vargs);
14421459
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET_ARRAY_VALUE, PathAccessType.VALUE);
1443-
if (isTyped()) {
1444-
checkPathExistsInModel(getModelPath(path));
1445-
}
1446-
return (Integer)getValue(path, Integer.class, tokenList);
1460+
String modelPath = checkPathInModel(path, tokenList);
1461+
Integer value = (Integer)getValue(path, Integer.class, tokenList);
1462+
checkFieldValue(path, modelPath, value, true);
1463+
return value;
14471464
}
14481465

14491466
@Override
14501467
public Boolean getArrayValueBoolean(String path, String... vargs) {
14511468
path = getStaticPath(path, vargs);
14521469
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET_ARRAY_VALUE, PathAccessType.VALUE);
1453-
if (isTyped()) {
1454-
checkPathExistsInModel(getModelPath(path));
1455-
}
1456-
return (Boolean)getValue(path, Boolean.class, tokenList);
1470+
String modelPath = checkPathInModel(path, tokenList);
1471+
Boolean value = (Boolean)getValue(path, Boolean.class, tokenList);
1472+
checkFieldValue(path, modelPath, value, true);
1473+
return value;
14571474
}
14581475

14591476
@Override
14601477
public Long getArrayValueLong(String path, String... vargs) {
14611478
path = getStaticPath(path, vargs);
14621479
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET_ARRAY_VALUE, PathAccessType.VALUE);
1463-
if (isTyped()) {
1464-
checkPathExistsInModel(getModelPath(path));
1465-
}
1466-
return (Long)getValue(path, Long.class, tokenList);
1480+
String modelPath = checkPathInModel(path, tokenList);
1481+
Long value = (Long)getValue(path, Long.class, tokenList);
1482+
checkFieldValue(path, modelPath, value, true);
1483+
return value;
14671484
}
14681485

14691486
@Override
14701487
public BigDecimal getArrayValueBigDecimal(String path, String... vargs) {
14711488
path = getStaticPath(path, vargs);
14721489
List<Token> tokenList = validatePath(path, CONSTS_JDOCS.API.GET_ARRAY_VALUE, PathAccessType.VALUE);
1473-
if (isTyped()) {
1474-
checkPathExistsInModel(getModelPath(path));
1475-
}
1476-
return (BigDecimal)getValue(path, BigDecimal.class, tokenList);
1490+
String modelPath = checkPathInModel(path, tokenList);
1491+
BigDecimal value = (BigDecimal)getValue(path, BigDecimal.class, tokenList);
1492+
checkFieldValue(path, modelPath, value, true);
1493+
return value;
14771494
}
14781495

14791496
@Override
@@ -1649,11 +1666,17 @@ public void setContent(Document fromDoc, String fromPath, String toPath, String.
16491666
}
16501667
}
16511668

1669+
private void copyInstanceFields(JDocument d, String type, boolean validateAtReadWriteOnly, boolean isValidated) {
1670+
d.type = type;
1671+
d.validateAtReadWriteOnly = validateAtReadWriteOnly;
1672+
d.isValidated = isValidated;
1673+
}
1674+
16521675
@Override
16531676
public synchronized Document deepCopy() {
16541677
JDocument d = new JDocument();
16551678
d.rootNode = rootNode.deepCopy();
1656-
d.type = type;
1679+
copyInstanceFields(d, type, validateAtReadWriteOnly, isValidated);
16571680
return d;
16581681
}
16591682

@@ -1785,7 +1808,7 @@ protected List<Token> parse(String path) {
17851808

17861809
@Override
17871810
public Document getContent(String path, boolean returnTypedDocument, boolean includeFullPath, String... vargs) {
1788-
Document d = null;
1811+
JDocument d = null;
17891812

17901813
if (vargs.length > 0) {
17911814
path = getStaticPath(path, vargs);
@@ -1806,7 +1829,8 @@ public Document getContent(String path, boolean returnTypedDocument, boolean inc
18061829
}
18071830

18081831
if (isTyped() && (returnTypedDocument == true)) {
1809-
d = new JDocument(type, null);
1832+
d = new JDocument(type, null, validateAtReadWriteOnly);
1833+
d.isValidated = isValidated;
18101834
}
18111835
else {
18121836
d = new JDocument();

src/test/java/com/americanexpress/unify/jdocs/DocumentTest.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package com.americanexpress.unify.jdocs;
1616

1717
import com.americanexpress.unify.base.BaseUtils;
18-
import com.americanexpress.unify.base.ERRORS_BASE;
1918
import com.americanexpress.unify.base.UnifyException;
2019
import org.junit.jupiter.api.BeforeAll;
2120
import org.junit.jupiter.api.Test;
@@ -34,8 +33,7 @@ public class DocumentTest {
3433

3534
@BeforeAll
3635
private static void setup() {
37-
ERRORS_BASE.load();
38-
ERRORS_JDOCS.load();
36+
JDocument.init();
3937
}
4038

4139
private String getCompressedJson(String filePath) {
@@ -1242,4 +1240,34 @@ void testIgnoreRegexIfEmpty() {
12421240
});
12431241
}
12441242

1243+
@Test
1244+
void testFieldValidationInGetMethods() {
1245+
setDocModel("sample_26_model");
1246+
1247+
Document d = new JDocument();
1248+
d.setString("$.id1", "GO2");
1249+
d.setType("sample_26_model", true);
1250+
d.getString("$.id1");
1251+
1252+
d = new JDocument();
1253+
d.setString("$.id1", "giberish");
1254+
d.setType("sample_26_model", true);
1255+
try {
1256+
d.getString("$.id1");
1257+
}
1258+
catch (Exception e) {
1259+
assertEquals(true, true);
1260+
}
1261+
1262+
d = new JDocument();
1263+
d.setString("$.id1", "giberish");
1264+
1265+
try {
1266+
d.setType("sample_26_model", false);
1267+
}
1268+
catch (Exception e) {
1269+
assertEquals(true, true);
1270+
}
1271+
}
1272+
12451273
}

0 commit comments

Comments
 (0)