Skip to content

Commit 51f1bc0

Browse files
Wrap jackson exception on malformed json string (#114445) (#118098)
This commit hides the underlying Jackson parse exception when encountered while parsing string tokens. Co-authored-by: Henrique Paes <[email protected]>
1 parent 2dd9193 commit 51f1bc0

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

docs/changelog/114445.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 114445
2+
summary: Wrap jackson exception on malformed json string
3+
area: Infra/Core
4+
type: bug
5+
issues:
6+
- 114142

libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/json/JsonXContentParser.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ public String text() throws IOException {
108108
if (currentToken().isValue() == false) {
109109
throwOnNoText();
110110
}
111-
return parser.getText();
111+
try {
112+
return parser.getText();
113+
} catch (JsonParseException e) {
114+
throw newXContentParseException(e);
115+
}
112116
}
113117

114118
private void throwOnNoText() {

qa/smoke-test-http/src/javaRestTest/java/org/elasticsearch/http/BulkRestIT.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ public void testBulkInvalidIndexNameString() throws IOException {
7474

7575
ResponseException responseException = expectThrows(ResponseException.class, () -> getRestClient().performRequest(request));
7676
assertThat(responseException.getResponse().getStatusLine().getStatusCode(), equalTo(BAD_REQUEST.getStatus()));
77-
assertThat(responseException.getMessage(), containsString("could not parse bulk request body"));
78-
assertThat(responseException.getMessage(), containsString("json_parse_exception"));
77+
assertThat(responseException.getMessage(), containsString("x_content_parse_exception"));
7978
assertThat(responseException.getMessage(), containsString("Invalid UTF-8"));
8079
}
8180

server/src/test/java/org/elasticsearch/common/xcontent/json/JsonXContentTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
import org.elasticsearch.common.xcontent.BaseXContentTestCase;
1313
import org.elasticsearch.xcontent.XContentGenerator;
14+
import org.elasticsearch.xcontent.XContentParseException;
15+
import org.elasticsearch.xcontent.XContentParser;
16+
import org.elasticsearch.xcontent.XContentParserConfiguration;
1417
import org.elasticsearch.xcontent.XContentType;
1518
import org.elasticsearch.xcontent.json.JsonXContent;
1619

@@ -28,4 +31,14 @@ public void testBigInteger() throws Exception {
2831
XContentGenerator generator = JsonXContent.jsonXContent.createGenerator(os);
2932
doTestBigInteger(generator, os);
3033
}
34+
35+
public void testMalformedJsonFieldThrowsXContentException() throws Exception {
36+
String json = "{\"test\":\"/*/}";
37+
try (XContentParser parser = JsonXContent.jsonXContent.createParser(XContentParserConfiguration.EMPTY, json)) {
38+
parser.nextToken();
39+
parser.nextToken();
40+
parser.nextToken();
41+
assertThrows(XContentParseException.class, () -> parser.text());
42+
}
43+
}
3144
}

0 commit comments

Comments
 (0)