This is an automated email from the ASF dual-hosted git repository.
jerryshao pushed a commit to branch branch-1.3
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/branch-1.3 by this push:
new 182a5ed993 [Cherry-pick to branch-1.3] [#14581] fix(lance-rest):
validate registration and preserve routing errors (#14583) (#14597)
182a5ed993 is described below
commit 182a5ed9930e3a15267e6a1e5296bb990ab182cd
Author: github-actions[bot]
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Wed Nov 4 11:43:18 2026 +0800
[Cherry-pick to branch-1.3] [#14581] fix(lance-rest): validate registration
and preserve routing errors (#14583) (#14597)
**Cherry-pick Information:**
- Original commit: b0982cba52e62c30fc3b72d15288f8e310c26216
- Target branch: `branch-1.3`
- Status: Clean cherry-pick followed by a branch-1.3 compatibility fix.
**Branch-1.3 adaptation:**
The routing-error mapper uses this branch's existing `getStackTrace()`
handling. The original cherry-pick referenced main's `errorDetail()`
helper, which is absent from branch-1.3 and caused compilation to fail.
This preserves the branch's existing exception-detail behavior.
**Validation:**
- Lance REST compilation and all 92 non-Docker unit tests passed, with
no failures or skips, including Jersey 405/406/415 and
registration-validation coverage.
- Module Spotless checks and `git diff --check` passed.
- MySQL/deploy integration tests were not run locally; the reported
failure occurred during compilation, before those tests could execute.
---------
Co-authored-by: Qi Yu <[email protected]>
---
.../lance/service/LanceExceptionMapper.java | 23 ++++++
.../lance/service/rest/LanceTableOperations.java | 6 +-
.../lance/service/TestLanceExceptionMapper.java | 42 +++++++++++
.../service/rest/TestRegisterTableValidation.java | 86 ++++++++++++++++++++++
4 files changed, 154 insertions(+), 3 deletions(-)
diff --git
a/lance/lance-rest-server/src/main/java/org/apache/gravitino/lance/service/LanceExceptionMapper.java
b/lance/lance-rest-server/src/main/java/org/apache/gravitino/lance/service/LanceExceptionMapper.java
index a3de0c46d6..c5b136604b 100644
---
a/lance/lance-rest-server/src/main/java/org/apache/gravitino/lance/service/LanceExceptionMapper.java
+++
b/lance/lance-rest-server/src/main/java/org/apache/gravitino/lance/service/LanceExceptionMapper.java
@@ -20,6 +20,11 @@ package org.apache.gravitino.lance.service;
import static org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace;
+import javax.ws.rs.NotAcceptableException;
+import javax.ws.rs.NotAllowedException;
+import javax.ws.rs.NotSupportedException;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@@ -49,6 +54,24 @@ public class LanceExceptionMapper implements
ExceptionMapper<Throwable> {
public static Response toRESTResponse(String instance, Throwable ex) {
ServerHealth.getInstance().recordFailure(ex);
+ if (ex instanceof NotAllowedException
+ || ex instanceof NotAcceptableException
+ || ex instanceof NotSupportedException) {
+ WebApplicationException routingError = (WebApplicationException) ex;
+ InvalidInputException invalidInput =
+ new InvalidInputException(ex.getMessage(), getStackTrace(ex),
instance);
+ try (Response mapped = handleLanceNamespaceException(invalidInput)) {
+ Response.ResponseBuilder response =
+ Response.status(routingError.getResponse().getStatus())
+ .entity(mapped.getEntity())
+ .type(MediaType.APPLICATION_JSON_TYPE);
+ if (ex instanceof NotAllowedException) {
+ response.allow(routingError.getResponse().getAllowedMethods());
+ }
+ return response.build();
+ }
+ }
+
LanceNamespaceException lanceException =
ex instanceof LanceNamespaceException
? (LanceNamespaceException) ex
diff --git
a/lance/lance-rest-server/src/main/java/org/apache/gravitino/lance/service/rest/LanceTableOperations.java
b/lance/lance-rest-server/src/main/java/org/apache/gravitino/lance/service/rest/LanceTableOperations.java
index 5eb2f731ed..fb763de1fb 100644
---
a/lance/lance-rest-server/src/main/java/org/apache/gravitino/lance/service/rest/LanceTableOperations.java
+++
b/lance/lance-rest-server/src/main/java/org/apache/gravitino/lance/service/rest/LanceTableOperations.java
@@ -333,9 +333,9 @@ public class LanceTableOperations {
// No specific fields to validate for now
}
- private void validateRegisterTableRequest(
- @SuppressWarnings("unused") RegisterTableRequest request) {
- // No specific fields to validate for now
+ private void validateRegisterTableRequest(RegisterTableRequest request) {
+ Preconditions.checkArgument(request != null, "Request body must not be
null");
+ Preconditions.checkArgument(request.getLocation() != null, "location must
not be null");
}
private void validateDeregisterTableRequest(
diff --git
a/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/service/TestLanceExceptionMapper.java
b/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/service/TestLanceExceptionMapper.java
index 37eb0327a1..edaa1fbed9 100644
---
a/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/service/TestLanceExceptionMapper.java
+++
b/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/service/TestLanceExceptionMapper.java
@@ -19,8 +19,12 @@
package org.apache.gravitino.lance.service;
import java.io.IOException;
+import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
+import javax.ws.rs.POST;
import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@@ -83,11 +87,25 @@ public class TestLanceExceptionMapper extends JerseyTest {
* @return never returns normally
*/
@GET
+ @Produces(MediaType.APPLICATION_JSON)
public String fail() {
AssertionError error = new AssertionError("assertion failure");
error.initCause(new IllegalStateException("root cause"));
throw error;
}
+
+ /**
+ * Accepts JSON to exercise unsupported request media types.
+ *
+ * @param body the request body
+ * @return the accepted body
+ */
+ @POST
+ @Path("consume")
+ @Consumes(MediaType.APPLICATION_JSON)
+ public String consume(String body) {
+ return body;
+ }
}
/**
@@ -109,6 +127,30 @@ public class TestLanceExceptionMapper extends JerseyTest {
.register(JacksonFeature.class);
}
+ @Test
+ void testMethodNotAllowed() {
+ try (Response response = target("error").request().put(Entity.json("{}")))
{
+ Assertions.assertEquals(405, response.getStatus());
+ Assertions.assertTrue(response.getAllowedMethods().contains("GET"));
+ Assertions.assertEquals(13,
response.readEntity(ErrorResponse.class).getCode());
+ }
+ }
+
+ @Test
+ void testUnsupportedMediaType() {
+ try (Response response =
target("error/consume").request().post(Entity.text("body"))) {
+ Assertions.assertEquals(415, response.getStatus());
+ Assertions.assertEquals(13,
response.readEntity(ErrorResponse.class).getCode());
+ }
+ }
+
+ @Test
+ void testNotAcceptable() {
+ try (Response response =
target("error").request(MediaType.TEXT_PLAIN).get()) {
+ Assertions.assertEquals(406, response.getStatus());
+ }
+ }
+
/** Verifies that an uncaught error is converted to a Lance internal error
response. */
@Test
public void testErrorResponse() {
diff --git
a/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/service/rest/TestRegisterTableValidation.java
b/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/service/rest/TestRegisterTableValidation.java
new file mode 100644
index 0000000000..2db9c699f2
--- /dev/null
+++
b/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/service/rest/TestRegisterTableValidation.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.lance.service.rest;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.when;
+
+import javax.ws.rs.core.Response;
+import org.apache.gravitino.lance.common.ops.NamespaceWrapper;
+import org.junit.jupiter.api.Test;
+import org.lance.namespace.model.ErrorResponse;
+import org.lance.namespace.model.RegisterTableRequest;
+import org.lance.namespace.model.RegisterTableResponse;
+
+class TestRegisterTableValidation {
+ @Test
+ void testValidLocationReachesBackend() {
+ NamespaceWrapper backend = mock(NamespaceWrapper.class,
RETURNS_DEEP_STUBS);
+ RegisterTableRequest request = new RegisterTableRequest();
+ request.setLocation("s3://bucket/existing.lance");
+ when(backend
+ .asTableOps()
+ .registerTable(
+ eq("cat$schema$table"),
+ eq("create"),
+ eq("$"),
+ argThat(
+ properties ->
"s3://bucket/existing.lance".equals(properties.get("location")))))
+ .thenReturn(new RegisterTableResponse());
+ try (Response response =
+ new LanceTableOperations(backend).registerTable("cat$schema$table",
"$", null, request)) {
+ assertEquals(200, response.getStatus());
+ verify(backend.asTableOps())
+ .registerTable(
+ eq("cat$schema$table"),
+ eq("create"),
+ eq("$"),
+ argThat(
+ properties ->
"s3://bucket/existing.lance".equals(properties.get("location"))));
+ }
+ }
+
+ @Test
+ void testMissingLocation() {
+ NamespaceWrapper backend = mock(NamespaceWrapper.class);
+ LanceTableOperations resource = new LanceTableOperations(backend);
+ try (Response response =
+ resource.registerTable("cat$schema$table", "$", null, new
RegisterTableRequest())) {
+ assertEquals(400, response.getStatus());
+ assertEquals(13, ((ErrorResponse) response.getEntity()).getCode());
+ verifyNoInteractions(backend);
+ }
+ }
+
+ @Test
+ void testMissingBody() {
+ NamespaceWrapper backend = mock(NamespaceWrapper.class);
+ try (Response response =
+ new LanceTableOperations(backend).registerTable("cat$schema$table",
"$", null, null)) {
+ assertEquals(400, response.getStatus());
+ verifyNoInteractions(backend);
+ }
+ }
+}