diff --git a/CHANGELOG.md b/CHANGELOG.md index 5309dd0d66b69..7a244d8895189 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Remove package org.opensearch.transport.grpc and replace with org.opensearch.plugin.transport.grpc ([#18031](https://github.com/opensearch-project/OpenSearch/pull/18031)) - Fix the native plugin installation error cause by the pgp public key change ([#18147](https://github.com/opensearch-project/OpenSearch/pull/18147)) - Fix object field exists query ([#17843](https://github.com/opensearch-project/OpenSearch/pull/17843)) +- Use Bad Request status for InputCoercionEcception ([#18161](https://github.com/opensearch-project/OpenSearch/pull/18161)) ### Security diff --git a/libs/core/src/main/java/org/opensearch/ExceptionsHelper.java b/libs/core/src/main/java/org/opensearch/ExceptionsHelper.java index 9692d20a050ff..5d5eeb41118c8 100644 --- a/libs/core/src/main/java/org/opensearch/ExceptionsHelper.java +++ b/libs/core/src/main/java/org/opensearch/ExceptionsHelper.java @@ -33,6 +33,7 @@ package org.opensearch; import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.exc.InputCoercionException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -98,6 +99,8 @@ public static RestStatus status(Throwable t) { return ((OpenSearchException) t).status(); } else if (t instanceof IllegalArgumentException) { return RestStatus.BAD_REQUEST; + } else if (t instanceof InputCoercionException) { + return RestStatus.BAD_REQUEST; } else if (t instanceof JsonParseException) { return RestStatus.BAD_REQUEST; } else if (t instanceof OpenSearchRejectedExecutionException) { @@ -115,6 +118,8 @@ public static String summaryMessage(Throwable t) { return getExceptionSimpleClassName(t) + "[" + t.getMessage() + "]"; } else if (t instanceof IllegalArgumentException) { return "Invalid argument"; + } else if (t instanceof InputCoercionException) { + return "Incompatible JSON value"; } else if (t instanceof JsonParseException) { return "Failed to parse JSON"; } else if (t instanceof OpenSearchRejectedExecutionException) { diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search/30_limits.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search/30_limits.yml index 1f550d114cf29..753d0b8a6dea9 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/search/30_limits.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search/30_limits.yml @@ -44,6 +44,24 @@ setup: scroll: 5m size: 10010 +--- +"Request with size exceeding max integer value": + - skip: + version: "- 3.0.99" + reason: "returns 500 before 3.1.0" + - do: + catch: /Numeric value \(2147483648\) out of range of int/ + search: + rest_total_hits_as_int: true + index: test_1 + body: + query: + match_all: {} + size: 2147483648 + + - match: { status: 400 } + - match: { error.type: input_coercion_exception } + --- "Rescore window limits": - do: diff --git a/server/src/test/java/org/opensearch/ExceptionsHelperTests.java b/server/src/test/java/org/opensearch/ExceptionsHelperTests.java index e2b22ccbba768..20527eb4e7dc0 100644 --- a/server/src/test/java/org/opensearch/ExceptionsHelperTests.java +++ b/server/src/test/java/org/opensearch/ExceptionsHelperTests.java @@ -33,6 +33,7 @@ package org.opensearch; import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.exc.InputCoercionException; import org.apache.commons.codec.DecoderException; import org.apache.lucene.index.CorruptIndexException; @@ -110,12 +111,17 @@ private void assertError(final Throwable cause, final Error error) { public void testStatus() { assertThat(ExceptionsHelper.status(new IllegalArgumentException("illegal")), equalTo(RestStatus.BAD_REQUEST)); + assertThat(ExceptionsHelper.status(new InputCoercionException(null, "illegal", null, null)), equalTo(RestStatus.BAD_REQUEST)); assertThat(ExceptionsHelper.status(new JsonParseException(null, "illegal")), equalTo(RestStatus.BAD_REQUEST)); assertThat(ExceptionsHelper.status(new OpenSearchRejectedExecutionException("rejected")), equalTo(RestStatus.TOO_MANY_REQUESTS)); } public void testSummaryMessage() { assertThat(ExceptionsHelper.summaryMessage(new IllegalArgumentException("illegal")), equalTo("Invalid argument")); + assertThat( + ExceptionsHelper.summaryMessage(new InputCoercionException(null, "illegal", null, null)), + equalTo("Incompatible JSON value") + ); assertThat(ExceptionsHelper.summaryMessage(new JsonParseException(null, "illegal")), equalTo("Failed to parse JSON")); assertThat(ExceptionsHelper.summaryMessage(new OpenSearchRejectedExecutionException("rejected")), equalTo("Too many requests")); }