fix: return 400 for native queries that fail JSON binding #20830
<!-- ccr-overview-v2 -->
🟢 Approval recommended
No active review issues were identified in the query-binding error-handling changes.
No open issues 🎉
<details>
<summary><strong>What changed in this PR</strong></summary>
Extends native-query error handling so well-formed JSON that cannot bind to a query returns HTTP 400 with an actionable message instead of HTTP 500, building on the constructor-validation handling from #19775.
Changes:
| File | Description |
| ---- | ----------- |
| server/src/test/java/org/apache/druid/server/QueryResourceTest.java | Tests HTTP 400 responses and error details for query-binding failures. |
| server/src/main/java/org/apache/druid/server/QueryResource.java | Converts additional JSON mapping failures into client errors. |
| processing/src/main/java/org/apache/druid/query/BadJsonQueryException.java | Formats mapping-error messages with reference paths when available. |
</details>
<sub>Review effort: Balanced</sub>
CodeQL / Confusing overloading of methods
Method BadJsonQueryException.getErrorMessage(..) could be confused with overloaded method [getErrorMessage](1), since dispatch depends on static types.
🟡 Changes recommended
Do not merge until the new mapping catch excludes server-side mapping-definition failures. JsonMappingException includes InvalidDefinitionException, so a broken registered query subtype can now become a 400 BadJsonQueryException instead of following the existing 500 path; ordinary input-side failures are otherwise routed correctly.
Reviewed 4 of 4 changed files; findings in 1 file.
Validation: git diff --check 7e300b9ccf6cca26d7bbe632f790415b31f123c1 fa8e5a08c4e3656e2ee8a3ab768225924e99fdc8 passed; no build or tests run for this static review.
| Severity | Findings |
| --- | ---: |
| P0 | 0 |
| P1 | 0 |
| P2 | 1 |
| P3 | 0 |
| Total | 1 |
<!-- mergelens:review -->
---
This is an automated review by Codex GPT-5.6-Luna(max)
After addressing the findings or replying to the comments, you can request another review from me to trigger a new automated review.
[P2] Broad mapping catch hides definition failures
Finding: JsonMappingException also covers InvalidDefinitionException, which Jackson uses when the target query type itself is invalid, such as a registered subtype with no usable creator. Those failures are independent of request JSON, but this catch wraps them in BadJsonQueryException; doPost then returns HTTP 400 and skips the catch-all warning/500 path, making a broker-side configuration or code defect look like a client error.
Suggestion: Limit this conversion to input-side mapping exceptions such as MismatchedInputException and InvalidTypeIdException, while allowing InvalidDefinitionException to reach the server-error path.
Fixed in 0f3d5a5254: InvalidDefinitionException is now rethrown before the JsonMappingException catch, so it still goes to the catch-all 500 path. testQueryWithBrokenDefinitionIsServerError covers it. I kept the broad catch so other input-side subclasses, such as UnrecognizedPropertyException and InvalidFormatException, still return 400.
🟢 Approval recommended
The current head fixes the prior mapping-definition issue while preserving 400 responses for ordinary JSON mapping failures and user-facing deserializer validation. No actionable issues remain in the reviewed changes.
Reviewed 4 of 4 changed files.
Validation: git diff --check 7e300b9ccf6cca26d7bbe632f790415b31f123c1 53f0c71e23714ca85b46923ed3961a852d37f358 passed; no build or tests run for this static review.
<!-- mergelens:review -->
---
This is an automated review by Codex GPT-5.6-Luna(max)
Approved these changes.
GWphua
FrankChen021
Description
#19775 made
QueryResourcereturn a 400 for native queries that fail with aValueInstantiationException. OtherJsonMappingExceptions raised while binding well-formed JSON to aQuerystill fall through to the catch-all inQueryResource#doPost. For example:queryType(InvalidTypeIdException)"aggregations": "rows"(MismatchedInputException)For each such request, the broker logs a full stack trace at
WARN("Uncaught exception from query processing. This should be caught and handled directly.") and returns a 500Unhandled exception made it to the topthat names only the exception class. These are client errors, so they flood the broker log and don't tell the caller what is wrong.Fix
readQuerynow also catchesJsonMappingException, after the existingValueInstantiationExceptioncatch. It is converted into aBadJsonQueryException, so the request goes through the existingcatch (QueryException e)path: a 400Json parse failedresponse with no stack trace logged. The error message isInvalid native query: <original message> (through reference chain: <path>), for example:errorClassis the actual Jackson exception class, consistent with theValueInstantiationExceptionhandling from #19775.Not every
JsonMappingExceptionis a client error, so two cases still go to the catch-all and return 500 with a logged stack trace:InvalidDefinitionException: Jackson cannot bind to the query class itself, such as a registered subtype with no usable creator. This is a server-side defect and does not depend on the request.JsonMappingExceptionthat wraps an exception thrown by a deserializer or setter, unless the wrapped exception points at bad input. Jackson wraps such exceptions with the original as the cause. The request returns 400 only when that cause is anIllegalArgumentException(includingIAE), aDruidExceptiontargeted atPersona.USER(such asInvalidInput), or aJsonProcessingException. Any other cause, such as aNullPointerExceptionor a defensiveDruidException, is treated as a bug and returns 500.Before this PR all of these returned 500, so a case that falls outside the 400 rules behaves as it did before. The custom deserializers in
processingandserverthat can run on query input all reject bad values withIAE/InvalidInputor a Jackson exception, so their errors now return 400.Release note
Native queries that are valid JSON but cannot be deserialized into a query, such as an unknown
queryTypeor a field of the wrong type, now return HTTP 400 with a descriptive error instead of HTTP 500, and no longer log a stack trace on the Broker.<hr>
##### Key changed/added classes in this PR
QueryResourceQueryResource#isServerErrorDuringBindingBadJsonQueryExceptionQueryResourceTest<hr>
This PR has: