normalize typed DEFAULT_VALUE after JSON reload#3035
Open
dpol1 wants to merge 1 commit into
Open
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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