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: https://github.com/elastic/elasticsearch/issues/112296
This commit is contained in:
Benjamin Trent 2024-09-11 10:15:56 -04:00 committed by GitHub
parent f79fb8c25b
commit 281ee04f7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 10 additions and 13 deletions

View File

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

View File

@ -111,7 +111,7 @@ public class JsonXContentParser extends AbstractXContentParser {
}
private void throwOnNoText() {
throw new IllegalStateException("Can't get text on a " + currentToken() + " at " + getTokenLocation());
throw new IllegalArgumentException("Expected text at " + getTokenLocation() + " but found " + currentToken());
}
@Override

View File

@ -42,7 +42,6 @@ import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
import org.elasticsearch.search.lookup.FieldValues;
import org.elasticsearch.search.lookup.SearchLookup;
import org.elasticsearch.xcontent.XContentParser;
import java.io.IOException;
import java.net.InetAddress;
@ -545,8 +544,9 @@ public class IpFieldMapper extends FieldMapper {
@Override
protected void parseCreateField(DocumentParserContext context) throws IOException {
InetAddress address;
String value = context.parser().textOrNull();
try {
address = value(context.parser(), nullValue);
address = value == null ? nullValue : InetAddresses.forString(value);
} catch (IllegalArgumentException e) {
if (ignoreMalformed) {
context.addIgnoredField(fieldType().name());
@ -564,14 +564,6 @@ public class IpFieldMapper extends FieldMapper {
}
}
private static InetAddress value(XContentParser parser, InetAddress nullValue) throws IOException {
String value = parser.textOrNull();
if (value == null) {
return nullValue;
}
return InetAddresses.forString(value);
}
private void indexValue(DocumentParserContext context, InetAddress address) {
if (dimension) {
context.getDimensions().addIp(fieldType().name(), address).validate(context.indexSettings());

View File

@ -66,7 +66,7 @@ public class ReferenceDocsTests extends ESTestCase {
builder.startObject("UNEXPECTED").endObject().endObject();
try (var stream = BytesReference.bytes(builder).streamInput()) {
expectThrows(IllegalStateException.class, () -> ReferenceDocs.readLinksBySymbol(stream));
expectThrows(IllegalArgumentException.class, () -> ReferenceDocs.readLinksBySymbol(stream));
}
}

View File

@ -373,7 +373,7 @@ public class MatchQueryBuilderTests extends AbstractQueryTestCase<MatchQueryBuil
"message1" : ["term1", "term2"]
}
}""";
expectThrows(IllegalStateException.class, () -> parseQuery(json2));
expectThrows(IllegalArgumentException.class, () -> parseQuery(json2));
}
public void testExceptionUsingAnalyzerOnNumericField() {