Skip to content

Commit 281ee04

Browse files
authored
JSON parse failures should be 4xx codes (#112703)
It seemed if there wasn't any text to parse, this is not an internal issue but instead an argument issue. I simply changed the exception thrown. If we don't agree with this, I can adjust `query` parsing directly, but this seemed like the better choice. closes: #112296
1 parent f79fb8c commit 281ee04

File tree

5 files changed

+10
-13
lines changed

5 files changed

+10
-13
lines changed

docs/changelog/112703.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 112703
2+
summary: JSON parse failures should be 4xx codes
3+
area: Infra/Core
4+
type: bug
5+
issues: []

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public String text() throws IOException {
111111
}
112112

113113
private void throwOnNoText() {
114-
throw new IllegalStateException("Can't get text on a " + currentToken() + " at " + getTokenLocation());
114+
throw new IllegalArgumentException("Expected text at " + getTokenLocation() + " but found " + currentToken());
115115
}
116116

117117
@Override

server/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java

+2-10
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
4343
import org.elasticsearch.search.lookup.FieldValues;
4444
import org.elasticsearch.search.lookup.SearchLookup;
45-
import org.elasticsearch.xcontent.XContentParser;
4645

4746
import java.io.IOException;
4847
import java.net.InetAddress;
@@ -545,8 +544,9 @@ protected String contentType() {
545544
@Override
546545
protected void parseCreateField(DocumentParserContext context) throws IOException {
547546
InetAddress address;
547+
String value = context.parser().textOrNull();
548548
try {
549-
address = value(context.parser(), nullValue);
549+
address = value == null ? nullValue : InetAddresses.forString(value);
550550
} catch (IllegalArgumentException e) {
551551
if (ignoreMalformed) {
552552
context.addIgnoredField(fieldType().name());
@@ -564,14 +564,6 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio
564564
}
565565
}
566566

567-
private static InetAddress value(XContentParser parser, InetAddress nullValue) throws IOException {
568-
String value = parser.textOrNull();
569-
if (value == null) {
570-
return nullValue;
571-
}
572-
return InetAddresses.forString(value);
573-
}
574-
575567
private void indexValue(DocumentParserContext context, InetAddress address) {
576568
if (dimension) {
577569
context.getDimensions().addIp(fieldType().name(), address).validate(context.indexSettings());

server/src/test/java/org/elasticsearch/common/ReferenceDocsTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void testResourceValidation() throws Exception {
6666
builder.startObject("UNEXPECTED").endObject().endObject();
6767

6868
try (var stream = BytesReference.bytes(builder).streamInput()) {
69-
expectThrows(IllegalStateException.class, () -> ReferenceDocs.readLinksBySymbol(stream));
69+
expectThrows(IllegalArgumentException.class, () -> ReferenceDocs.readLinksBySymbol(stream));
7070
}
7171
}
7272

server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ public void testParseFailsWithTermsArray() {
373373
"message1" : ["term1", "term2"]
374374
}
375375
}""";
376-
expectThrows(IllegalStateException.class, () -> parseQuery(json2));
376+
expectThrows(IllegalArgumentException.class, () -> parseQuery(json2));
377377
}
378378

379379
public void testExceptionUsingAnalyzerOnNumericField() {

0 commit comments

Comments
 (0)