fix(server): normalize typed DEFAULT_VALUE after JSON reload#3035
Conversation
Normalizes PropertyKey default values to their declared data type upon retrieval. Previously, values stored in userdata could lose their original type during serialization and deserialization (e.g., Date becoming String), leading to type mismatches. The `defaultValue()` method now converts deserialized string representations back to their expected runtime types. This change is verified with extensive tests covering schema parsing, vertex property assignment, and both binary and text serializers. Fixes apache#3028
There was a problem hiding this comment.
Pull request overview
Fixes schema PropertyKey typed ~default_value (Userdata.DEFAULT_VALUE) losing runtime type after JSON (de)serialization by normalizing the stored default value back to the key’s declared DataType when retrieved.
Changes:
- Normalize
PropertyKey.defaultValue()in bothhugegraph-coreandhugegraph-structvia existing type-awarevalidValue(...)conversion. - Add regression coverage for DATE defaults across schema parsing (
fromMap), serializer round-trips (text/binary), and vertex default application after backend reload. - Add a focused
hugegraph-structunit test validatingdefaultValue()returns aDatewhen userdata contains a formatted dateString.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/PropertyKey.java | Normalize DEFAULT_VALUE on read in defaultValue() using validValue(...) |
| hugegraph-struct/src/main/java/org/apache/hugegraph/struct/schema/PropertyKey.java | Mirror the same defaultValue() normalization behavior in struct schema model |
| hugegraph-struct/src/test/java/org/apache/hugegraph/struct/schema/PropertyKeyTest.java | Add unit test ensuring string DATE default normalizes to java.util.Date |
| hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/serializer/TextSerializerTest.java | Add text serializer round-trip test for DATE default value typing |
| hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/serializer/BinarySerializerTest.java | Add binary serializer round-trip test for DATE default value typing |
| hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/SchemaElementTest.java | Add fromMap(...) tests for SINGLE and SET DATE default value normalization |
| hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/core/VertexCoreTest.java | Add end-to-end vertex test ensuring DATE default applies as Date after commit/reload |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
imbajin
left a comment
There was a problem hiding this comment.
Found one SET-cardinality regression in the default-value normalization path.
| // value (e.g. Date) comes back as a String. Normalize it to the | ||
| // runtime type expected by this property key's data type. Idempotent | ||
| // for values already of the expected type. | ||
| return value == null ? null : this.validValue(value); |
There was a problem hiding this comment.
defaultValue() now delegates ~default_value normalization to validValue(), but JSON-reloaded SET defaults arrive as an ArrayList. The existing convValue() then chooses the output collection from the input type, so a Cardinality.SET default can still come back as an ArrayList. The new SET test only asserts Collection, so it misses this.
This can install list-valued defaults for SET properties after schema reload, preserving duplicates and breaking cardinality semantics. Please make both core and struct PropertyKey.convValue() choose the target container from this.cardinality (LinkedHashSet for SET, ArrayList for LIST), and add a regression assertion that a SET default returns a Set and collapses duplicates.
Purpose of the PR
Normalizes
PropertyKeydefault values to their declared data type upon retrieval. Previously, values stored inuserdatacould lose their original type during serialization and deserialization (for example, aDATEdefault reloaded as aString), leading to type mismatches when applying defaults.The
defaultValue()method in bothhugegraph-coreandhugegraph-structnow converts deserialized representations back to their expected runtime types using the existing type-aware converters based onPropertyKey.dataType(). This ensures that typed defaults (such asDATE) remain correctly typed after a JSON round-trip and when applied to vertices and edges.This change is verified with tests covering schema parsing, vertex property assignment, and both binary and text serializers.
Main Changes
Core
PropertyKey.defaultValue()normalizationhugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/PropertyKey.java,defaultValue()now:Userdata.DEFAULT_VALUEfromuserdata,validValue(value), which routes through the existing type-aware converters (convValue/convSingleValue→dataType().valueToDate/Number/UUID/Blob).String(for typed keys such asDATE) are converted back to their runtime type before being used by consumers likeHugeElement.updateToDefaultValueIfNone().Struct
PropertyKey.defaultValue()mirrorhugegraph-struct/src/main/java/org/apache/hugegraph/struct/schema/PropertyKey.javaso that structPropertyKeybehaves consistently with core:defaultValue()also normalizes the stored~default_valueaccording to theDataType.PropertyKeyTest.testDefaultValueNormalizedToDate()inhugegraph-structthat constructs aDATEproperty key with aStringdefault and asserts thatdefaultValue()returns aDate.End-to-end vertex default application
testAddVertexWithDateDefaultValue()toVertexCoreTest:DATEproperty key withUserdata.DEFAULT_VALUEset to aDate,Datewith the expected value, not aString.HugeElement.updateToDefaultValueIfNone().Serializer round-trip for
DEFAULT_VALUETextSerializerTestandBinarySerializerTestwith:testPropertyKeyDefaultValueRoundTripsAsDate():DATEPropertyKeywhoseDEFAULT_VALUEis aDate,defaultValue()on the reloaded key returns aDateequal to the original.Schema parsing normalization (core)
SchemaElementTestwith:testPropertyKeyFromMapNormalizesDateDefaultValue():PropertyKeyfrom aMapwithDATA_TYPE=DATEand~default_valueas a formatted dateString,defaultValue()returns aDate.testPropertyKeyFromMapNormalizesDateSetDefaultValue():Cardinality.SET, with~default_valueas a collection of date strings,defaultValue()returns aCollectionwhose elements are allDateinstances.validValue()forDATEdefaults.Verifying these changes
hugegraph-core:VertexCoreTest.testAddVertexWithDateDefaultValue()SchemaElementTest.testPropertyKeyFromMapNormalizesDateDefaultValue()SchemaElementTest.testPropertyKeyFromMapNormalizesDateSetDefaultValue()TextSerializerTest.testPropertyKeyDefaultValueRoundTripsAsDate()BinarySerializerTest.testPropertyKeyDefaultValueRoundTripsAsDate()hugegraph-struct:PropertyKeyTest.testDefaultValueNormalizedToDate()All existing tests remain green.
Does this PR potentially affect the following parts?
PropertyKey.defaultValue()for malformed or type-incompatible defaults: such values are now validated and may trigger conversion errors instead of leaking unexpected rawStringvalues at read time.Documentation Status
Doc - TODODoc - DoneDoc - No Need