imbajin commented on code in PR #3026:
URL: https://github.com/apache/hugegraph/pull/3026#discussion_r3257390894
##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/Userdata.java:
##########
@@ -34,7 +35,31 @@ public Userdata() {
}
public Userdata(Map<String, Object> map) {
- this.putAll(map);
+ for (Map.Entry<String, Object> e : map.entrySet()) {
+ this.put(e.getKey(), normalizeValue(e.getKey(), e.getValue()));
+ }
+ }
+
+ /**
+ * Normalize internal userdata values whose runtime type can diverge from
+ * their serialized form. The only such key today is {@link #CREATE_TIME}:
+ * it is written as a {@link java.util.Date} but persisted as a formatted
+ * JSON string by the backend serializers, and Jackson cannot re-type a
+ * value to {@code Date} when the target is a raw {@code Map}. This method
+ * restores the original type after deserialization. Idempotent for values
+ * already of the expected type.
+ */
+ public static Object normalizeValue(String key, Object value) {
Review Comment:
⚠️ `normalizeValue` is invoked from the `Userdata(Map)` constructor but
**not** from `put()` / `putAll()`.
Callers can bypass normalization by writing directly to a `Userdata`
instance:
```java
Userdata ud = new Userdata();
ud.put(Userdata.CREATE_TIME, "2026-01-01 00:00:00.000"); // still a String
```
The object then holds an inconsistent type until it is passed to
`SchemaElement.userdata(Userdata)`. This pattern already appears in
`SchemaElementTest#testBulkSetterNormalizesCreateTimeAndKeepsOtherEntries`,
which calls `bulk.put(CREATE_TIME, formatted)` directly and relies on
`SchemaElement` to fix it up.
If the invariant is that `CREATE_TIME` must *always* be `Date` inside a
`Userdata`, the guarantee should live in `put()` as well:
```java
@Override
public Object put(String key, Object value) {
return super.put(key, normalizeValue(key, value));
}
```
##########
hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/SchemaElementTest.java:
##########
@@ -0,0 +1,202 @@
+/*
+ * 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.hugegraph.unit.core;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.hugegraph.backend.id.IdGenerator;
+import org.apache.hugegraph.schema.PropertyKey;
+import org.apache.hugegraph.schema.SchemaElement;
+import org.apache.hugegraph.schema.Userdata;
+import org.apache.hugegraph.schema.VertexLabel;
+import org.apache.hugegraph.testutil.Assert;
+import org.apache.hugegraph.unit.FakeObjects;
+import org.apache.hugegraph.util.DateUtil;
+import org.junit.Test;
+
+public class SchemaElementTest {
+
+ private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
Review Comment:
🧹 `DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"` is hardcoded here and duplicated
identically in `TextSerializerTest` line 36. `DateUtil` uses this format
internally but does not expose it as a public constant. If the default format
ever changes, both tests will silently diverge.
Consider exposing a `DateUtil.FORMAT_MS` constant, or use the single-arg
`DateUtil.parse(str)` overload in tests to avoid coupling to a format literal
at all.
##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/Userdata.java:
##########
@@ -34,7 +35,31 @@ public Userdata() {
}
public Userdata(Map<String, Object> map) {
Review Comment:
⚠️ All four `fromMap()` paths (`PropertyKey`, `VertexLabel`, `EdgeLabel`,
`IndexLabel`) call:
```java
schema.userdata(new Userdata((Map<String, Object>) entry.getValue()));
```
This normalizes `CREATE_TIME` twice: once here in `Userdata(Map)`, and once
again in `SchemaElement.userdata(Userdata)`. Normalization is currently
idempotent (`Date → Date` is a no-op), so there is no bug today. However, the
redundancy makes the invariant harder to reason about and would silently break
if normalization ever becomes non-idempotent. Consider consolidating to a
single layer (e.g., normalize only in `SchemaElement`, or only in
`Userdata.put()` as noted above).
##########
hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/serializer/TextSerializerTest.java:
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.hugegraph.unit.serializer;
+
+import java.util.Date;
+
+import org.apache.hugegraph.backend.id.IdGenerator;
+import org.apache.hugegraph.backend.serializer.TextSerializer;
+import org.apache.hugegraph.backend.store.BackendEntry;
+import org.apache.hugegraph.config.HugeConfig;
+import org.apache.hugegraph.schema.PropertyKey;
+import org.apache.hugegraph.schema.Userdata;
+import org.apache.hugegraph.testutil.Assert;
+import org.apache.hugegraph.unit.BaseUnitTest;
+import org.apache.hugegraph.unit.FakeObjects;
+import org.apache.hugegraph.util.DateUtil;
+import org.junit.Test;
+
+public class TextSerializerTest extends BaseUnitTest {
+
+ private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
+
+ @Test
+ public void testPropertyKeyUserdataCreateTimeRoundTripsAsDate() {
+ HugeConfig config = FakeObjects.newConfig();
+ TextSerializer ser = new TextSerializer(config);
+
+ FakeObjects objects = new FakeObjects();
+ PropertyKey original = objects.newPropertyKey(IdGenerator.of(1L),
+ "name");
+ Date created = DateUtil.parse("2026-05-14 10:11:12.345", DATE_FORMAT);
+ original.userdata(Userdata.CREATE_TIME, created);
+
+ BackendEntry entry = ser.writePropertyKey(original);
+ PropertyKey reloaded = ser.readPropertyKey(objects.graph(), entry);
+
+ Object value = reloaded.userdata().get(Userdata.CREATE_TIME);
+ Assert.assertTrue("CREATE_TIME should be a Date after round-trip, " +
+ "was " + (value == null ? "null" : value.getClass()),
+ value instanceof Date);
+ Assert.assertEquals(created, value);
+ }
Review Comment:
⚠️ The PR description states that `MysqlSerializer` and
`CassandraSerializer` are also affected by the same `CREATE_TIME`
deserialization gap:
> *"`userdata(String, Object)` covers serializer reload paths such as
TextSerializer, BinarySerializer, **MysqlSerializer**, and
**CassandraSerializer**"*
Only `TextSerializer` and `BinarySerializer` have round-trip tests added.
Adding equivalent `testPropertyKeyUserdataCreateTimeRoundTripsAsDate` tests for
`MysqlSerializer` and `CassandraSerializer` would close this coverage gap for
the stated fix surface.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]