Skip to content

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

[#13405] fix(core): null-guard and defensively copy uris in the ModelVersionInfo map constructor (#13406)

YangJie0355835c4e22
diff --git a/core/src/main/java/org/apache/gravitino/listener/api/info/ModelVersionInfo.java b/core/src/main/java/org/apache/gravitino/listener/api/info/ModelVersionInfo.java
index c11b8414c4..493c31184e 100644
--- a/core/src/main/java/org/apache/gravitino/listener/api/info/ModelVersionInfo.java
+++ b/core/src/main/java/org/apache/gravitino/listener/api/info/ModelVersionInfo.java
@@ -108,7 +108,7 @@ public class ModelVersionInfo {
Map<String, String> properties,
String[] aliases,
Audit auditInfo) {
- this.uris = uris;
+ this.uris = uris == null ? Collections.emptyMap() : ImmutableMap.copyOf(uris);
this.properties = properties == null ? ImmutableMap.of() : ImmutableMap.copyOf(properties);
this.comment = Optional.ofNullable(comment);
this.audit = Optional.ofNullable(auditInfo);
diff --git a/core/src/test/java/org/apache/gravitino/listener/api/info/TestModelVersionInfo.java b/core/src/test/java/org/apache/gravitino/listener/api/info/TestModelVersionInfo.java
new file mode 100644
index 0000000000..d64b345a3b
--- /dev/null
+++ b/core/src/test/java/org/apache/gravitino/listener/api/info/TestModelVersionInfo.java
@@ -0,0 +1,56 @@
+/*
+ * 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.listener.api.info;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.HashMap;
+import java.util.Map;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/** Tests for {@link ModelVersionInfo}. */
+public class TestModelVersionInfo {
+
+ @Test
+ public void testNullUrisYieldEmptyMap() {
+ ModelVersionInfo info =
+ new ModelVersionInfo((Map<String, String>) null, null, null, null, null);
+
+ // Mirrors the uri-based constructor and the properties handling: absent uris
+ // surface as an empty map, not null, and uri() must not throw.
+ Assertions.assertNotNull(info.uris());
+ Assertions.assertTrue(info.uris().isEmpty());
+ Assertions.assertNull(info.uri());
+ }
+
+ @Test
+ public void testUrisAreDefensivelyCopied() {
+ Map<String, String> uris = new HashMap<>();
+ uris.put("unknown", "gs://bucket/model/v1");
+ ModelVersionInfo info = new ModelVersionInfo(uris, null, null, null, null);
+
+ uris.put("unknown", "gs://bucket/model/MUTATED");
+ uris.put("extra", "gs://bucket/model/EXTRA");
+
+ // Event payloads are read-only; mutating the caller's map must not change them.
+ Assertions.assertEquals(
+ ImmutableMap.of("unknown", "gs://bucket/model/v1"), ImmutableMap.copyOf(info.uris()));
+ Assertions.assertEquals("gs://bucket/model/v1", info.uri());
+ }
+}