[#10171] fix(server): Add null request body checks to REST create/register endpoints#10182
[#10171] fix(server): Add null request body checks to REST create/register endpoints#10182shaojunying wants to merge 1 commit intoapache:mainfrom
Conversation
…te/register endpoints Add null checks for request body parameters before accessing request fields in REST endpoints. Previously, if request deserialization yielded null, accessing request.getName() etc. before the try block would trigger an uncaught NullPointerException, bypassing the ExceptionHandlers error handling path. Affected endpoints: - TableOperations.createTable - FilesetOperations.createFileset - FunctionOperations.registerFunction - ModelOperations.registerModel - SchemaOperations.createSchema - JobOperations.registerJobTemplate - JobOperations.runJob Each endpoint now returns a structured BAD_REQUEST error response when the request body is null, consistent with the existing pattern in MetalakeOperations.createMetalake. Added unit tests for null request body behavior in all matching REST test classes. Closes apache#10171
|
Looks good, but a few minor improvements needed:
|
|
Just show me a real case on how to reproduce this issue. AFAIK, this will not happen in the real environment. |
|
Thanks for the review @justinmclean! Here's my plan for addressing your feedback:
I'll push the fixes shortly. |
|
Hi @jerryshao, thanks for the question. Here's a concrete reproduction case: Steps to reproduce: # Start a Gravitino server, then send a null JSON body to the createTable endpoint:
curl -X POST "http://localhost:8090/api/metalakes/my_metalake/catalogs/my_catalog/schemas/my_schema/tables" \
-H "Content-Type: application/json" \
-H "Accept: application/vnd.gravitino.v1+json" \
-d "null"When JAX-RS (Jersey) deserializes the literal JSON LOG.info("Received create table request: {}.{}.{}.{}", metalake, catalog, schema, request.getName());
// ^^^^^^^^^^^^^^^ NPE here
try {This happens before the You're right that normal Gravitino clients won't send
This is essentially a defensive programming improvement to ensure all REST endpoints return proper error responses at system boundaries, consistent with the existing |
|
If this is the case, this will affect all the requests, is there a centralized place that can fix all the related issues, not just do the null check in every place? I think we need to fix it in the Jackson / Jersey object mapper configuration. |
|
Not how we currently have this set up as far as I'm aware. ObjectMapper can enforce many field-level rules, but not null method arguments at JAX-RS entry. |
What changes were proposed in this pull request?
Add null checks for request body parameters before accessing request fields in REST endpoints. Previously, if request deserialization yielded
null(e.g., empty body, literal JSONnull, or binding edge cases), accessingrequest.getName()or similar methods before thetryblock would trigger an uncaughtNullPointerException, bypassing the expectedExceptionHandlerserror handling path.Why are the changes needed?
Several REST endpoints dereference
requestfields (e.g.,request.getName(),request.getJobTemplateName()) before entering theirtryblock. This causes uncaught NPEs that bypass the structured error response path, returning unhelpful 500 errors instead of proper 400 Bad Request responses.Fix #10171
Does this PR introduce any user-facing change?
No API changes. Null request bodies now return a structured
400 Bad Requesterror response instead of an unhandled500 Internal Server Error.How was this patch tested?
Added unit tests for null request body behavior in all matching REST test classes:
TestTableOperations.testCreateTableWithNullRequestBodyTestFilesetOperations.testCreateFilesetWithNullRequestBodyTestFunctionOperations.testRegisterFunctionWithNullRequestBodyTestModelOperations.testRegisterModelWithNullRequestBodyTestSchemaOperations.testCreateSchemaWithNullRequestBodyTestJobOperations.testRegisterJobTemplateWithNullRequestBodyTestJobOperations.testRunJobWithNullRequestBodyAffected endpoints
TableOperations.javacreateTableFilesetOperations.javacreateFilesetFunctionOperations.javaregisterFunctionModelOperations.javaregisterModelSchemaOperations.javacreateSchemaJobOperations.javaregisterJobTemplateJobOperations.javarunJob