Skip to content

Commit

Permalink
added null test
Browse files Browse the repository at this point in the history
Signed-off-by: Kenrick Yap <[email protected]>
  • Loading branch information
kenrickyap committed Jan 14, 2025
1 parent 31ad2a4 commit 2b2a8f3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
5 changes: 5 additions & 0 deletions core/src/main/java/org/opensearch/sql/utils/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public class JsonUtils {
*/
public static ExprValue isValidJson(ExprValue jsonExprValue) {
ObjectMapper objectMapper = new ObjectMapper();

if (jsonExprValue.isNull() || jsonExprValue.isMissing()) {
return ExprValueUtils.LITERAL_FALSE;
}

try {
objectMapper.readTree(jsonExprValue.stringValue());
return ExprValueUtils.LITERAL_TRUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ public void test_not_json_valid() throws IOException {
"source=%s | where not json_valid(json_string) | fields test_name",
TEST_INDEX_JSON_TEST));
verifySchema(result, schema("test_name", null, "string"));
verifyDataRows(result, rows("json invalid object"));
verifyDataRows(result, rows("json invalid object"), rows("json null"));
}
}
2 changes: 2 additions & 0 deletions integ-test/src/test/resources/json_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
{"test_name":"json empty string","json_string":""}
{"index":{"_id":"5"}}
{"test_name":"json invalid object", "json_string":"{\"invalid\":\"json\", \"string\"}"}
{"index":{"_id":"6"}}
{"test_name":"json null", "json_string":null}

0 comments on commit 2b2a8f3

Please sign in to comment.