Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

return NULL if PathNotFoundException is thrown #5049

Closed
wants to merge 5 commits into from
Closed
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
Expand Up @@ -64,9 +64,10 @@ public void testInvalidPathThrowsException() throws TransformationException {
processor.transform("$$", jsonArray);
}

@Test(expected = TransformationException.class)
@Test
public void testPathMismatchReturnNull() throws TransformationException {
processor.transform("$[5].id", jsonArray);
String transformedResponse = processor.transform("$[5].id", jsonArray);
assertEquals("NULL", transformedResponse);
}

@Test(expected = TransformationException.class)
Expand Down
Expand Up @@ -52,11 +52,11 @@ public String transform(String jsonPathExpression, String source) throws Transfo
throw new TransformationException("the given parameters 'JSonPath' and 'source' must not be null");
}

logger.debug("about to transform '{}' by the function '{}'", source, jsonPathExpression);
logger.debug("About to transform '{}' by the function '{}'", source, jsonPathExpression);

try {
Object transformationResult = JsonPath.read(source, jsonPathExpression);
logger.debug("transformation resulted in '{}'", transformationResult);
logger.debug("Transformation resulted in '{}'", transformationResult);
if (transformationResult == null) {
return UnDefType.NULL.toFullString();
} else if (transformationResult instanceof List) {
Expand All @@ -65,11 +65,11 @@ public String transform(String jsonPathExpression, String source) throws Transfo
return transformationResult.toString();
}
} catch (PathNotFoundException e) {
throw new TransformationException("Invalid path '" + jsonPathExpression + "' in '" + source + "'");
logger.debug("Given JSONPath selector expression '" + jsonPathExpression + "' does not match the input string '" + source + "'");
return UnDefType.NULL.toFullString();
} catch (InvalidPathException | InvalidJsonException e) {
throw new TransformationException("An error occurred while transforming JSON expression.", e);
}

}

private String flattenList(List<?> list) {
Expand All @@ -83,5 +83,4 @@ private String flattenList(List<?> list) {
}
return UnDefType.NULL.toFullString();
}

}