Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#14580] fix(rest): validate null request fields before dispatch (#14582)

Qi Yu8f19ee03cb88
diff --git a/common/src/main/java/org/apache/gravitino/dto/rel/ColumnDTO.java b/common/src/main/java/org/apache/gravitino/dto/rel/ColumnDTO.java
index 0b9ec6427a..8f13201a3d 100644
--- a/common/src/main/java/org/apache/gravitino/dto/rel/ColumnDTO.java
+++ b/common/src/main/java/org/apache/gravitino/dto/rel/ColumnDTO.java
@@ -20,6 +20,8 @@ package org.apache.gravitino.dto.rel;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonSetter;
+import com.fasterxml.jackson.annotation.Nulls;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.base.Preconditions;
@@ -55,6 +57,7 @@ public class ColumnDTO implements Column {
private boolean autoIncrement = false;
@JsonProperty("defaultValue")
+ @JsonSetter(nulls = Nulls.SKIP)
// the NON_EMPTY annotation is used to avoid serializing the defaultValue field if it is not set
// or null.
@JsonInclude(JsonInclude.Include.NON_EMPTY)
diff --git a/common/src/main/java/org/apache/gravitino/dto/requests/CatalogCreateRequest.java b/common/src/main/java/org/apache/gravitino/dto/requests/CatalogCreateRequest.java
index acf8442c4d..43827b87f5 100644
--- a/common/src/main/java/org/apache/gravitino/dto/requests/CatalogCreateRequest.java
+++ b/common/src/main/java/org/apache/gravitino/dto/requests/CatalogCreateRequest.java
@@ -146,6 +146,7 @@ public class CatalogCreateRequest implements RESTRequest {
*/
@Override
public void validate() throws IllegalArgumentException {
+ RequestValidation.properties(properties);
Preconditions.checkArgument(
StringUtils.isNotBlank(name), "\"name\" field is required and cannot be empty");
Preconditions.checkArgument(type != null, "\"type\" field is required and cannot be empty");
diff --git a/common/src/main/java/org/apache/gravitino/dto/requests/CatalogSetRequest.java b/common/src/main/java/org/apache/gravitino/dto/requests/CatalogSetRequest.java
index d2c9c744b2..0dc0ae92e9 100644
--- a/common/src/main/java/org/apache/gravitino/dto/requests/CatalogSetRequest.java
+++ b/common/src/main/java/org/apache/gravitino/dto/requests/CatalogSetRequest.java
@@ -19,6 +19,8 @@
package org.apache.gravitino.dto.requests;
import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.base.Preconditions;
+import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
@@ -31,11 +33,12 @@ import org.apache.gravitino.rest.RESTRequest;
public class CatalogSetRequest implements RESTRequest {
@JsonProperty("inUse")
- private final boolean inUse;
+ @Getter(AccessLevel.NONE)
+ private final Boolean inUse;
/** Default constructor for CatalogSetRequest. */
public CatalogSetRequest() {
- this(false);
+ this.inUse = null;
}
/**
@@ -48,12 +51,23 @@ public class CatalogSetRequest implements RESTRequest {
}
/**
- * Validates the request. No validation needed.
+ * Validates that the requested state was explicitly provided.
*
* @throws IllegalArgumentException If the request is invalid.
*/
@Override
public void validate() throws IllegalArgumentException {
- // No validation needed
+ Preconditions.checkArgument(inUse != null, "inUse field is required and must not be null");
+ }
+
+ /**
+ * Returns the explicitly requested state.
+ *
+ * @return whether the object should be in use
+ * @throws IllegalArgumentException if the state was not provided
+ */
+ public boolean isInUse() {
+ validate();
+ return inUse;
}
}
diff --git a/common/src/main/java/org/apache/gravitino/dto/requests/FilesetCreateRequest.java b/common/src/main/java/org/apache/gravitino/dto/requests/FilesetCreateRequest.java
index 55940ef3c5..90e9cc9c9c 100644
--- a/common/src/main/java/org/apache/gravitino/dto/requests/FilesetCreateRequest.java
+++ b/common/src/main/java/org/apache/gravitino/dto/requests/FilesetCreateRequest.java
@@ -113,6 +113,7 @@ public class FilesetCreateRequest implements RESTRequest {
*/
@Override
public void validate() throws IllegalArgumentException {
+ RequestValidation.properties(properties);
Preconditions.checkArgument(
StringUtils.isNotBlank(name), "\"name\" field is required and cannot be empty");
}
diff --git a/common/src/main/java/org/apache/gravitino/dto/requests/MetalakeCreateRequest.java b/common/src/main/java/org/apache/gravitino/dto/requests/MetalakeCreateRequest.java
index 5e993a3f70..ff1b23b3f0 100644
--- a/common/src/main/java/org/apache/gravitino/dto/requests/MetalakeCreateRequest.java
+++ b/common/src/main/java/org/apache/gravitino/dto/requests/MetalakeCreateRequest.java
@@ -73,6 +73,7 @@ public class MetalakeCreateRequest implements RESTRequest {
*/
@Override
public void validate() throws IllegalArgumentException {
+ RequestValidation.properties(properties);
Preconditions.checkArgument(
StringUtils.isNotBlank(name), "\"name\" field is required and cannot be empty");
}
diff --git a/common/src/main/java/org/apache/gravitino/dto/requests/MetalakeSetRequest.java b/common/src/main/java/org/apache/gravitino/dto/requests/MetalakeSetRequest.java
index f87abef25e..354b842db1 100644
--- a/common/src/main/java/org/apache/gravitino/dto/requests/MetalakeSetRequest.java
+++ b/common/src/main/java/org/apache/gravitino/dto/requests/MetalakeSetRequest.java
@@ -19,6 +19,8 @@
package org.apache.gravitino.dto.requests;
import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.base.Preconditions;
+import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
@@ -31,11 +33,12 @@ import org.apache.gravitino.rest.RESTRequest;
public class MetalakeSetRequest implements RESTRequest {
@JsonProperty("inUse")
- private final boolean inUse;
+ @Getter(AccessLevel.NONE)
+ private final Boolean inUse;
/** Default constructor for MetalakeSetRequest. */
public MetalakeSetRequest() {
- this(false);
+ this.inUse = null;
}
/**
@@ -48,12 +51,23 @@ public class MetalakeSetRequest implements RESTRequest {
}
/**
- * Validates the request. No validation needed.
+ * Validates that the requested state was explicitly provided.
*
* @throws IllegalArgumentException If the request is invalid.
*/
@Override
public void validate() throws IllegalArgumentException {
- // No validation needed
+ Preconditions.checkArgument(inUse != null, "inUse field is required and must not be null");
+ }
+
+ /**
+ * Returns the explicitly requested state.
+ *
+ * @return whether the object should be in use
+ * @throws IllegalArgumentException if the state was not provided
+ */
+ public boolean isInUse() {
+ validate();
+ return inUse;
}
}
diff --git a/common/src/main/java/org/apache/gravitino/dto/requests/RequestValidation.java b/common/src/main/java/org/apache/gravitino/dto/requests/RequestValidation.java
new file mode 100644
index 0000000000..8028d56d1f
--- /dev/null
+++ b/common/src/main/java/org/apache/gravitino/dto/requests/RequestValidation.java
@@ -0,0 +1,44 @@
+/*
+ * 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.dto.requests;
+
+import com.google.common.base.Preconditions;
+import java.util.Map;
+
+final class RequestValidation {
+ private RequestValidation() {}
+
+ static void properties(Map<String, String> properties) {
+ if (properties != null) {
+ properties.forEach(
+ (key, value) -> {
+ Preconditions.checkArgument(key != null, "properties must not contain null keys");
+ Preconditions.checkArgument(value != null, "properties[%s] must not be null", key);
+ });
+ }
+ }
+
+ static void elements(Object[] elements, String field) {
+ if (elements != null) {
+ for (int i = 0; i < elements.length; i++) {
+ Preconditions.checkArgument(elements[i] != null, "%s[%s] must not be null", field, i);
+ }
+ }
+ }
+}
diff --git a/common/src/main/java/org/apache/gravitino/dto/requests/SchemaCreateRequest.java b/common/src/main/java/org/apache/gravitino/dto/requests/SchemaCreateRequest.java
index 3745686012..3709f140a5 100644
--- a/common/src/main/java/org/apache/gravitino/dto/requests/SchemaCreateRequest.java
+++ b/common/src/main/java/org/apache/gravitino/dto/requests/SchemaCreateRequest.java
@@ -106,6 +106,7 @@ public class SchemaCreateRequest implements RESTRequest {
*/
@Override
public void validate() throws IllegalArgumentException {
+ RequestValidation.properties(properties);
Preconditions.checkArgument(
StringUtils.isNotBlank(name), "\"name\" field is required and cannot be empty");
}
diff --git a/common/src/main/java/org/apache/gravitino/dto/requests/SchemaUpdatesRequest.java b/common/src/main/java/org/apache/gravitino/dto/requests/SchemaUpdatesRequest.java
index bb5c61c7b5..ffdd416000 100644
--- a/common/src/main/java/org/apache/gravitino/dto/requests/SchemaUpdatesRequest.java
+++ b/common/src/main/java/org/apache/gravitino/dto/requests/SchemaUpdatesRequest.java
@@ -57,6 +57,10 @@ public class SchemaUpdatesRequest implements RESTRequest {
@Override
public void validate() throws IllegalArgumentException {
Preconditions.checkArgument(updates != null, "updates must not be null");
- updates.forEach(RESTRequest::validate);
+ updates.forEach(
+ update -> {
+ Preconditions.checkArgument(update != null, "updates must not contain null elements");
+ update.validate();
+ });
}
}
diff --git a/common/src/main/java/org/apache/gravitino/dto/requests/TableCreateRequest.java b/common/src/main/java/org/apache/gravitino/dto/requests/TableCreateRequest.java
index d5d73a0bd1..20e266c9a7 100644
--- a/common/src/main/java/org/apache/gravitino/dto/requests/TableCreateRequest.java
+++ b/common/src/main/java/org/apache/gravitino/dto/requests/TableCreateRequest.java
@@ -121,9 +121,13 @@ public class TableCreateRequest implements RESTRequest {
*/
@Override
public void validate() throws IllegalArgumentException {
+ RequestValidation.properties(properties);
Preconditions.checkArgument(
StringUtils.isNotBlank(name), "\"name\" field is required and cannot be empty");
+ RequestValidation.elements(partitioning, "partitioning");
+ RequestValidation.elements(indexes, "indexes");
+
// DTO conversion can normalize null literals and discard their declared types.
WritableDataTypeValidator.validateTable(columns, partitioning, distribution, sortOrders);
diff --git a/common/src/main/java/org/apache/gravitino/dto/requests/TableUpdatesRequest.java b/common/src/main/java/org/apache/gravitino/dto/requests/TableUpdatesRequest.java
index b95df9ec93..ba5c3fbd58 100644
--- a/common/src/main/java/org/apache/gravitino/dto/requests/TableUpdatesRequest.java
+++ b/common/src/main/java/org/apache/gravitino/dto/requests/TableUpdatesRequest.java
@@ -59,6 +59,7 @@ public class TableUpdatesRequest implements RESTRequest {
Preconditions.checkArgument(updates != null, "updates must not be null");
Preconditions.checkArgument(!updates.isEmpty(), "updates must not be empty");
for (int i = 0; i < updates.size(); i++) {
+ Preconditions.checkArgument(updates.get(i) != null, "updates[%s] must not be null", i);
updates.get(i).validateWithPrefix("updates[" + i + "].");
}
}
diff --git a/common/src/main/java/org/apache/gravitino/dto/requests/TopicCreateRequest.java b/common/src/main/java/org/apache/gravitino/dto/requests/TopicCreateRequest.java
index 7cf7214484..5d73b88cf4 100644
--- a/common/src/main/java/org/apache/gravitino/dto/requests/TopicCreateRequest.java
+++ b/common/src/main/java/org/apache/gravitino/dto/requests/TopicCreateRequest.java
@@ -69,6 +69,7 @@ public class TopicCreateRequest implements RESTRequest {
@Override
public void validate() throws IllegalArgumentException {
+ RequestValidation.properties(properties);
Preconditions.checkArgument(
StringUtils.isNotBlank(name), "\"name\" field is required and cannot be empty");
}
diff --git a/common/src/main/java/org/apache/gravitino/dto/requests/ViewCreateRequest.java b/common/src/main/java/org/apache/gravitino/dto/requests/ViewCreateRequest.java
index c00481dfac..fe30b28324 100644
--- a/common/src/main/java/org/apache/gravitino/dto/requests/ViewCreateRequest.java
+++ b/common/src/main/java/org/apache/gravitino/dto/requests/ViewCreateRequest.java
@@ -109,6 +109,7 @@ public class ViewCreateRequest implements RESTRequest {
@Override
public void validate() throws IllegalArgumentException {
+ RequestValidation.properties(properties);
Preconditions.checkArgument(
StringUtils.isNotBlank(name), "\"name\" field is required and cannot be empty");
Preconditions.checkArgument(
diff --git a/common/src/main/java/org/apache/gravitino/dto/requests/ViewUpdatesRequest.java b/common/src/main/java/org/apache/gravitino/dto/requests/ViewUpdatesRequest.java
index d0d24f8b0c..9b18e27e62 100644
--- a/common/src/main/java/org/apache/gravitino/dto/requests/ViewUpdatesRequest.java
+++ b/common/src/main/java/org/apache/gravitino/dto/requests/ViewUpdatesRequest.java
@@ -54,6 +54,7 @@ public class ViewUpdatesRequest implements RESTRequest {
Preconditions.checkArgument(updates != null, "updates must not be null");
Preconditions.checkArgument(!updates.isEmpty(), "updates must not be empty");
for (int i = 0; i < updates.size(); i++) {
+ Preconditions.checkArgument(updates.get(i) != null, "updates[%s] must not be null", i);
updates.get(i).validateWithPrefix("updates[" + i + "].");
}
}
diff --git a/common/src/test/java/org/apache/gravitino/dto/requests/TestNullRequestValidation.java b/common/src/test/java/org/apache/gravitino/dto/requests/TestNullRequestValidation.java
new file mode 100644
index 0000000000..d4bb31de30
--- /dev/null
+++ b/common/src/test/java/org/apache/gravitino/dto/requests/TestNullRequestValidation.java
@@ -0,0 +1,165 @@
+/*
+ * 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.dto.requests;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.gravitino.dto.rel.ColumnDTO;
+import org.apache.gravitino.json.JsonUtils;
+import org.apache.gravitino.rel.Column;
+import org.apache.gravitino.rel.expressions.literals.Literals;
+import org.apache.gravitino.rel.types.Types;
+import org.apache.gravitino.rest.RESTRequest;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+class TestNullRequestValidation {
+ @ParameterizedTest
+ @ValueSource(strings = {"{}", "{\"inUse\":null}"})
+ void testMissingState(String json) throws Exception {
+ for (Class<? extends RESTRequest> type :
+ List.of(CatalogSetRequest.class, MetalakeSetRequest.class)) {
+ RESTRequest request = JsonUtils.objectMapper().readValue(json, type);
+ assertThrows(IllegalArgumentException.class, request::validate);
+ }
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void testExplicitState(boolean enabled) throws Exception {
+ String json = "{\"inUse\":" + enabled + "}";
+ CatalogSetRequest catalog = JsonUtils.objectMapper().readValue(json, CatalogSetRequest.class);
+ MetalakeSetRequest metalake =
+ JsonUtils.objectMapper().readValue(json, MetalakeSetRequest.class);
+ catalog.validate();
+ metalake.validate();
+ assertEquals(enabled, catalog.isInUse());
+ assertEquals(enabled, metalake.isInUse());
+ assertEquals(enabled, new CatalogSetRequest(enabled).isInUse());
+ assertEquals(enabled, new MetalakeSetRequest(enabled).isInUse());
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"columns", "partitioning", "sortOrders", "indexes"})
+ void testNullTableElement(String field) throws Exception {
+ String json =
+ "{\"name\":\"t\",\"columns\":[{\"name\":\"id\",\"type\":\"integer\"}],\""
+ + field
+ + "\":[null]}";
+ TableCreateRequest request = JsonUtils.objectMapper().readValue(json, TableCreateRequest.class);
+ IllegalArgumentException error =
+ assertThrows(IllegalArgumentException.class, request::validate);
+ assertTrue(error.getMessage().contains(field));
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"{\"name\":\"t\"}", "{\"name\":\"t\",\"columns\":null}"})
+ void testMissingColumns(String json) throws Exception {
+ TableCreateRequest request = JsonUtils.objectMapper().readValue(json, TableCreateRequest.class);
+ IllegalArgumentException error =
+ assertThrows(IllegalArgumentException.class, request::validate);
+ assertEquals("\"columns\" field is required and cannot be null", error.getMessage());
+ }
+
+ @Test
+ void testNullUpdates() throws Exception {
+ for (Class<? extends RESTRequest> type :
+ List.of(TableUpdatesRequest.class, SchemaUpdatesRequest.class, ViewUpdatesRequest.class)) {
+ RESTRequest request = JsonUtils.objectMapper().readValue("{\"updates\":[null]}", type);
+ assertThrows(IllegalArgumentException.class, request::validate);
+ }
+ }
+
+ @Test
+ void testNullPropertyValues() throws Exception {
+ for (Class<? extends RESTRequest> type :
+ List.of(
+ MetalakeCreateRequest.class,
+ CatalogCreateRequest.class,
+ SchemaCreateRequest.class,
+ FilesetCreateRequest.class,
+ TopicCreateRequest.class,
+ TableCreateRequest.class,
+ ViewCreateRequest.class)) {
+ String extra =
+ type == CatalogCreateRequest.class
+ ? ",\"type\":\"RELATIONAL\",\"provider\":\"hive\""
+ : "";
+ if (type == TableCreateRequest.class) {
+ extra += ",\"columns\":[{\"name\":\"id\",\"type\":\"integer\"}]";
+ }
+ if (type == ViewCreateRequest.class) {
+ extra +=
+ ",\"representations\":[{\"type\":\"sql\",\"dialect\":\"trino\",\"sql\":\"SELECT 1\"}]";
+ }
+ String valid = "{\"name\":\"t\",\"properties\":null" + extra + "}";
+ RESTRequest baseline = JsonUtils.objectMapper().readValue(valid, type);
+ assertDoesNotThrow(baseline::validate);
+ RESTRequest populated =
+ JsonUtils.objectMapper()
+ .readValue(
+ valid.replace("\"properties\":null", "\"properties\":{\"k\":\"v\"}"), type);
+ assertDoesNotThrow(populated::validate);
+ RESTRequest request =
+ JsonUtils.objectMapper()
+ .readValue(valid.replace("\"properties\":null", "\"properties\":{\"k\":null}"), type);
+ IllegalArgumentException error =
+ assertThrows(IllegalArgumentException.class, request::validate);
+ assertTrue(error.getMessage().contains("properties"));
+ }
+ }
+
+ @Test
+ void testNullPropertyKey() {
+ Map<String, String> properties = new HashMap<>();
+ properties.put(null, "value");
+ SchemaCreateRequest request = new SchemaCreateRequest("schema", null, properties);
+ assertThrows(IllegalArgumentException.class, request::validate);
+ }
+
+ @Test
+ void testNullableDefaultsAndOptionalFields() throws Exception {
+ ColumnDTO unset =
+ JsonUtils.objectMapper()
+ .readValue(
+ "{\"name\":\"id\",\"type\":\"integer\",\"defaultValue\":null}", ColumnDTO.class);
+ assertSame(Column.DEFAULT_VALUE_NOT_SET, unset.defaultValue());
+ ColumnDTO sqlNull =
+ ColumnDTO.builder()
+ .withName("id")
+ .withDataType(Types.IntegerType.get())
+ .withDefaultValue(Literals.NULL)
+ .build();
+ assertSame(Literals.NULL, sqlNull.defaultValue());
+ TableCreateRequest request =
+ JsonUtils.objectMapper()
+ .readValue(
+ "{\"name\":\"t\",\"columns\":[{\"name\":\"id\",\"type\":\"integer\"}],\"properties\":null,\"partitioning\":null,\"sortOrders\":null,\"indexes\":null}",
+ TableCreateRequest.class);
+ assertDoesNotThrow(request::validate);
+ }
+}
diff --git a/server/src/main/java/org/apache/gravitino/server/web/rest/CatalogOperations.java b/server/src/main/java/org/apache/gravitino/server/web/rest/CatalogOperations.java
index d47dc71b30..79f60a44f2 100644
--- a/server/src/main/java/org/apache/gravitino/server/web/rest/CatalogOperations.java
+++ b/server/src/main/java/org/apache/gravitino/server/web/rest/CatalogOperations.java
@@ -297,6 +297,12 @@ public class CatalogOperations {
}
LOG.info("Received set request for catalog: {}.{}", metalake, catalogName);
+ try {
+ request.validate();
+ } catch (IllegalArgumentException e) {
+ return ExceptionHandlers.handleCatalogException(OperationType.SET, catalogName, metalake, e);
+ }
+
OperationType op = request.isInUse() ? OperationType.ENABLE : OperationType.DISABLE;
try {
diff --git a/server/src/main/java/org/apache/gravitino/server/web/rest/MetalakeOperations.java b/server/src/main/java/org/apache/gravitino/server/web/rest/MetalakeOperations.java
index c577bd3298..ea390591a6 100644
--- a/server/src/main/java/org/apache/gravitino/server/web/rest/MetalakeOperations.java
+++ b/server/src/main/java/org/apache/gravitino/server/web/rest/MetalakeOperations.java
@@ -189,6 +189,12 @@ public class MetalakeOperations {
}
LOG.info("Received set request for metalake: {}", metalakeName);
+ try {
+ request.validate();
+ } catch (IllegalArgumentException e) {
+ return ExceptionHandlers.handleMetalakeException(OperationType.SET, metalakeName, e);
+ }
+
OperationType op = request.isInUse() ? OperationType.ENABLE : OperationType.DISABLE;
try {
diff --git a/server/src/test/java/org/apache/gravitino/server/web/rest/TestCatalogOperations.java b/server/src/test/java/org/apache/gravitino/server/web/rest/TestCatalogOperations.java
index d4a90a3642..dbf0b311ea 100644
--- a/server/src/test/java/org/apache/gravitino/server/web/rest/TestCatalogOperations.java
+++ b/server/src/test/java/org/apache/gravitino/server/web/rest/TestCatalogOperations.java
@@ -735,6 +735,27 @@ public class TestCatalogOperations extends BaseOperationsTest {
}
}
+ @Test
+ void testSetCatalogRequiresExplicitState() {
+ for (String json : new String[] {"{}", "{\"inUse\":null}"}) {
+ try (Response response =
+ target("/metalakes/metalake1/catalogs/catalog1")
+ .property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true)
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.gravitino.v1+json")
+ .method("PATCH", Entity.entity(json, MediaType.APPLICATION_JSON_TYPE))) {
+ Assertions.assertEquals(400, response.getStatus());
+ ErrorResponse error = response.readEntity(ErrorResponse.class);
+ Assertions.assertEquals(
+ "Failed to operate catalog(s) [catalog1] operation [SET] under metalake [metalake1], "
+ + "reason [inUse field is required and must not be null]",
+ error.getMessage());
+ }
+ }
+ Mockito.verify(manager, Mockito.never()).enableCatalog(any());
+ Mockito.verify(manager, Mockito.never()).disableCatalog(any());
+ }
+
@Test
public void testSetCatalog() {
// Test enable catalog
diff --git a/server/src/test/java/org/apache/gravitino/server/web/rest/TestMetalakeOperations.java b/server/src/test/java/org/apache/gravitino/server/web/rest/TestMetalakeOperations.java
index be125c3f34..6f112ce747 100644
--- a/server/src/test/java/org/apache/gravitino/server/web/rest/TestMetalakeOperations.java
+++ b/server/src/test/java/org/apache/gravitino/server/web/rest/TestMetalakeOperations.java
@@ -268,6 +268,22 @@ public class TestMetalakeOperations extends BaseOperationsTest {
assertNullRequestBodyRejected(resp);
}
+ @Test
+ void testSetMetalakeRequiresExplicitState() {
+ for (String json : new String[] {"{}", "{\"inUse\":null}"}) {
+ try (Response response =
+ target("/metalakes/test")
+ .property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true)
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.gravitino.v1+json")
+ .method("PATCH", Entity.entity(json, MediaType.APPLICATION_JSON_TYPE))) {
+ Assertions.assertEquals(400, response.getStatus());
+ }
+ }
+ Mockito.verify(metalakeManager, Mockito.never()).enableMetalake(any());
+ Mockito.verify(metalakeManager, Mockito.never()).disableMetalake(any());
+ }
+
@Test
public void testSetMetalake() {
Mockito.doNothing().when(metalakeManager).enableMetalake(any());
diff --git a/server/src/test/java/org/apache/gravitino/server/web/rest/TestTableOperations.java b/server/src/test/java/org/apache/gravitino/server/web/rest/TestTableOperations.java
index 5d14f8ea50..3cd369ab79 100644
--- a/server/src/test/java/org/apache/gravitino/server/web/rest/TestTableOperations.java
+++ b/server/src/test/java/org/apache/gravitino/server/web/rest/TestTableOperations.java
@@ -197,6 +197,7 @@ public class TestTableOperations extends BaseOperationsTest {
@Test
public void testRejectNullTableInputsBeforeDispatch() {
String[][] invalidRequests = {
+ {"{\"name\":\"t\"}", "columns"},
{"{\"name\":\"t\",\"columns\":null}", "columns"},
{"{\"name\":\"t\",\"columns\":[null]}", "columns[0]"},
{