Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #942 handle only one element in an array for masking #946

Merged
merged 1 commit into from May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 4 additions & 15 deletions mask/src/main/java/com/networknt/mask/Mask.java
Expand Up @@ -223,29 +223,18 @@ private static void maskList(DocumentContext ctx, String jsonPath, String expres
*/
if(pathList != null && pathList.size() == 1) {
String path = pathList.get(0);


/* I tried to implement as below and its working as expected
instead of doing List values = ctx.read(path);

// the value might be a string or a list
Object readValues = ctx.read(path);
List values = new ArrayList();
if(readValues instanceof String){
values.add(readValues);
} else {
values = ctx.read(path);
}
else {
values = (List)ctx.read(path);
}
*/


//here ctx.read(path) returning string "4586996854721123 and failing on "cannot cast string to list"
List values = ctx.read(path);

List maskedValue = new ArrayList();
//mask each value in the list of the same path
values.forEach(o -> maskedValue.add(replaceWithMask(o.toString(), MASK_REPLACEMENT_CHAR.charAt(0), expression)));
ctx.set(path, maskedValue);
ctx.set(path, maskedValue.size() == 1 ? maskedValue.get(0) : maskedValue);
} else {
for (String path : Optional.ofNullable(pathList).orElse(Collections.emptyList())) {
Object value = ctx.read(path);
Expand Down
13 changes: 1 addition & 12 deletions mask/src/test/java/com/networknt/mask/MaskTest.java
Expand Up @@ -132,17 +132,6 @@ public void testMaskIssue942()
String input = "{\"name\":\"Steve\", \"list\":[{\"name\":\"Josh\", \"creditCardNumber\":\"4586996854721123\"}],\"password\":\"secret\"}";
String output = Mask.maskJson(input, "testIssue942");
System.out.println(output);
Assert.assertEquals(output, "{\"name\":\"Steve\", \"list\":[{\"name\":\"Josh\", \"creditCardNumber\":\"****************\"}],\"password\":\"secret\"}");

/*
this is test result that I got when using the commented code in Mask.java, when using the original code I am getting ClassCastException.
Expected :{"name":"Steve","list":[{"name":"Josh","creditCardNumber":["****************"]}],"password":"secret"}
Actual :{"name":"Steve", "list":[{"name":"Josh", "creditCardNumber":"****************"}],"password":"secret"}


also I am dont understand why in the expected I am getting list of creditCardNumber

*/
Assert.assertEquals(output, "{\"name\":\"Steve\",\"list\":[{\"name\":\"Josh\",\"creditCardNumber\":\"****************\"}],\"password\":\"secret\"}");
}

}
2 changes: 1 addition & 1 deletion mask/src/test/resources/config/mask.yml
Expand Up @@ -35,6 +35,6 @@ json:
"$.list1": "(.*)"
"$.password": "(.*)"

# I want to mask creditCardNumber field in all the list elemnts which in my test case include just one element
# I want to mask creditCardNumber field in all the list elements which in my test case include just one element
testIssue942:
"$.list.[*].creditCardNumber": "(.*)"