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 f3492067f5 [Cherry-pick to branch-1.3] [#14580] fix(rest): validate
null request fields before dispatch (#14582) (#14598)
f3492067f5 is described below
commit f3492067f5c3411f2aeae26e07feb568f9e80576
Author: github-actions[bot]
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Wed Nov 4 11:43:48 2026 +0800
[Cherry-pick to branch-1.3] [#14580] fix(rest): validate null request
fields before dispatch (#14582) (#14598)
### What changes were proposed in this pull request?
Cherry-pick 8f19ee03cb88351353f1166fd238026f4ee5b7e0 (#14582) to
`branch-1.3`, with conflicts resolved against the 1.3 request-validation
APIs.
Reject missing/null catalog and metalake state, null property
keys/values in the affected create requests, null elements in table
columns/partitioning/sortOrders/indexes, and null Table/Schema/View
update entries before dispatch. Preserve the public boolean
constructor/getter signatures and normalize explicit JSON null column
defaults to the unset-default sentinel.
The backport uses explicit null guards and existing `validate()` methods
because 1.3 does not have main's writable-type validator or
`validateWithPrefix()` API. Main-only type-validation context and its
tests are not included.
### Why are the changes needed?
Malformed requests can silently disable catalogs/metalakes or fail with
HTTP 500. Reject them as HTTP 400 before dispatch.
Backport of #14582 for #14580.
### Does this PR introduce _any_ user-facing change?
Omitted/null inUse and table columns, and the null property/array/update
entries listed above, are rejected. Catalog/Metalake/Fileset/Topic
update validation is outside this backport. Explicit true/false and
nullable optional containers retain their behavior. SQL NULL defaults
remain distinct from unset defaults. No public method signatures or
metadata schemas change.
### How was this patch tested?
- 49 relevant common tests and 105 REST operation tests passed on
`branch-1.3`, with no failures or skips. Tests cover request JSON, DTO
defaults, create/update validation, and rejection before dispatcher
calls.
- Removing the 1.3 table null guards makes all six targeted regression
cases fail; all six pass after restoration.
- Common/server Spotless, RAT, conflict-marker scans and git diff checks
passed.
- No full core suite or live backend integration tests were run.
---------
Co-authored-by: Qi Yu <[email protected]>
---
.../org/apache/gravitino/dto/rel/ColumnDTO.java | 3 +
.../dto/requests/CatalogCreateRequest.java | 1 +
.../gravitino/dto/requests/CatalogSetRequest.java | 22 ++-
.../dto/requests/FilesetCreateRequest.java | 1 +
.../dto/requests/MetalakeCreateRequest.java | 1 +
.../gravitino/dto/requests/MetalakeSetRequest.java | 22 ++-
.../gravitino/dto/requests/RequestValidation.java | 44 ++++++
.../dto/requests/SchemaCreateRequest.java | 1 +
.../dto/requests/SchemaUpdatesRequest.java | 6 +-
.../gravitino/dto/requests/TableCreateRequest.java | 8 +
.../dto/requests/TableUpdatesRequest.java | 5 +-
.../gravitino/dto/requests/TopicCreateRequest.java | 1 +
.../gravitino/dto/requests/ViewCreateRequest.java | 1 +
.../gravitino/dto/requests/ViewUpdatesRequest.java | 5 +-
.../dto/requests/TestNullRequestValidation.java | 165 +++++++++++++++++++++
.../server/web/rest/CatalogOperations.java | 6 +
.../server/web/rest/MetalakeOperations.java | 6 +
.../server/web/rest/TestCatalogOperations.java | 21 +++
.../server/web/rest/TestMetalakeOperations.java | 16 ++
.../server/web/rest/TestTableOperations.java | 25 ++++
20 files changed, 349 insertions(+), 11 deletions(-)
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 3223fce6c8..ef8b12b9ae 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
@@ -106,6 +106,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 f4c79bca34..7a0f4bb1d3 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
@@ -71,6 +71,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 ec103c0629..2b6ed69ebf 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
@@ -70,6 +70,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 e9a33e6d77..a5a663da4c 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
@@ -120,9 +120,17 @@ 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");
+ Preconditions.checkArgument(
+ columns != null, "\"columns\" field is required and cannot be null");
+ RequestValidation.elements(columns, "columns");
+ RequestValidation.elements(partitioning, "partitioning");
+ RequestValidation.elements(sortOrders, "sortOrders");
+ RequestValidation.elements(indexes, "indexes");
+
if (sortOrders != null) {
Arrays.stream(sortOrders).forEach(sortOrder ->
sortOrder.validate(columns));
}
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 e347f9b681..a441fc14b2 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
@@ -58,6 +58,9 @@ public class TableUpdatesRequest implements RESTRequest {
public void validate() throws IllegalArgumentException {
Preconditions.checkArgument(updates != null, "updates must not be null");
Preconditions.checkArgument(!updates.isEmpty(), "updates must not be
empty");
- updates.forEach(RESTRequest::validate);
+ for (int i = 0; i < updates.size(); i++) {
+ Preconditions.checkArgument(updates.get(i) != null, "updates[%s] must
not be null", i);
+ updates.get(i).validate();
+ }
}
}
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 8c0d02305a..c5828c92d2 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
@@ -108,6 +108,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 8cf29b7788..c10df98511 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
@@ -53,6 +53,9 @@ public class ViewUpdatesRequest implements RESTRequest {
public void validate() throws IllegalArgumentException {
Preconditions.checkArgument(updates != null, "updates must not be null");
Preconditions.checkArgument(!updates.isEmpty(), "updates must not be
empty");
- updates.forEach(RESTRequest::validate);
+ for (int i = 0; i < updates.size(); i++) {
+ Preconditions.checkArgument(updates.get(i) != null, "updates[%s] must
not be null", i);
+ updates.get(i).validate();
+ }
}
}
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 1b862e48db..34d6cd2d99 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
@@ -292,6 +292,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 b8596792a1..a16e462877 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);
+ }
+
try {
return Utils.doAs(
httpRequest,
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 cbd66e52dd..9246200d9f 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
@@ -734,6 +734,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 6eeb2b7669..6314a6fcf0 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
@@ -267,6 +267,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 9864b208d5..824b356a90 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
@@ -154,6 +154,31 @@ public class TestTableOperations extends
BaseOperationsTest {
return resourceConfig;
}
+ @Test
+ public void testRejectNullTableInputsBeforeDispatch() {
+ String[][] invalidRequests = {
+ {"{\"name\":\"t\"}", "\"columns\" field is required and cannot be null"},
+ {"{\"name\":\"t\",\"columns\":null}", "\"columns\" field is required and
cannot be null"},
+ {"{\"name\":\"t\",\"columns\":[null]}", "columns[0] must not be null"},
+ {
+
"{\"name\":\"t\",\"columns\":[{\"name\":\"c\",\"type\":\"string\"}],\"sortOrders\":[null]}",
+ "sortOrders[0] must not be null"
+ }
+ };
+ for (String[] invalidRequest : invalidRequests) {
+ try (Response response =
+ target(tablePath(metalake, catalog, schema))
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.gravitino.v1+json")
+ .post(Entity.json(invalidRequest[0]))) {
+ Assertions.assertEquals(Status.BAD_REQUEST.getStatusCode(),
response.getStatus());
+ Assertions.assertTrue(
+
response.readEntity(ErrorResponse.class).getMessage().contains(invalidRequest[1]));
+ }
+ }
+ Mockito.verifyNoInteractions(dispatcher);
+ }
+
@Test
public void testListTables() {
NameIdentifier table1 = NameIdentifier.of(metalake, catalog, schema,
"table1");