dpol1 opened a new pull request, #3035:
URL: https://github.com/apache/hugegraph/pull/3035

   <!-- 
     Thank you very much for contributing to Apache HugeGraph, we are happy 
that you want to help us improve it!
   -->
   
   ## Purpose of the PR
   
   - fix #3028
   
   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 (for example, a `DATE` default 
reloaded as a `String`), leading to type mismatches when applying defaults.
   
   The `defaultValue()` method in both `hugegraph-core` and `hugegraph-struct` 
now converts deserialized representations back to their expected runtime types 
using the existing type-aware converters based on `PropertyKey.dataType()`. 
This ensures that typed defaults (such as `DATE`) 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()` normalization**
     - In 
`hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/PropertyKey.java`,
 `defaultValue()` now:
       - reads `Userdata.DEFAULT_VALUE` from `userdata`,
       - if non-null, normalizes it via `validValue(value)`, which routes 
through the existing type-aware converters (`convValue` / `convSingleValue` → 
`dataType().valueToDate/Number/UUID/Blob`).
     - This ensures that defaults reloaded from JSON as `String` (for typed 
keys such as `DATE`) are converted back to their runtime type before being used 
by consumers like `HugeElement.updateToDefaultValueIfNone()`.
   
   - **Struct `PropertyKey.defaultValue()` mirror**
     - Applies the same normalization logic to 
`hugegraph-struct/src/main/java/org/apache/hugegraph/struct/schema/PropertyKey.java`
 so that struct `PropertyKey` behaves consistently with core:
       - `defaultValue()` also normalizes the stored `~default_value` according 
to the `DataType`.
     - Adds a focused unit test 
`PropertyKeyTest.testDefaultValueNormalizedToDate()` in `hugegraph-struct` that 
constructs a `DATE` property key with a `String` default and asserts that 
`defaultValue()` returns a `Date`.
   
   - **End-to-end vertex default application**
     - Adds `testAddVertexWithDateDefaultValue()` to `VertexCoreTest`:
       - defines a `DATE` property key with `Userdata.DEFAULT_VALUE` set to a 
`Date`,
       - creates a vertex without explicitly setting that property,
       - commits, reloads the vertex from the backend (forcing schema reload),
       - asserts that the applied property is a `Date` with the expected value, 
not a `String`.
     - This reproduces and locks in the user-facing symptom from #3028 through 
`HugeElement.updateToDefaultValueIfNone()`.
   
   - **Serializer round-trip for `DEFAULT_VALUE`**
     - Extends `TextSerializerTest` and `BinarySerializerTest` with:
       - `testPropertyKeyDefaultValueRoundTripsAsDate()`:
         - serializes a `DATE` `PropertyKey` whose `DEFAULT_VALUE` is a `Date`,
         - deserializes it back,
         - asserts that `defaultValue()` on the reloaded key returns a `Date` 
equal to the original.
     - Ensures that both text and binary serializers preserve the typed default 
after a full round-trip.
   
   - **Schema parsing normalization (core)**
     - Extends `SchemaElementTest` with:
       - `testPropertyKeyFromMapNormalizesDateDefaultValue()`:
         - builds a `PropertyKey` from a `Map` with `DATA_TYPE=DATE` and 
`~default_value` as a formatted date `String`,
         - asserts that `defaultValue()` returns a `Date`.
       - `testPropertyKeyFromMapNormalizesDateSetDefaultValue()`:
         - same as above but for `Cardinality.SET`, with `~default_value` as a 
collection of date strings,
         - asserts that `defaultValue()` returns a `Collection` whose elements 
are all `Date` instances.
     - This validates both the scalar and collection branches of `validValue()` 
for `DATE` defaults.
   
   ## Verifying these changes
   
   - [x] Need tests and can be verified as follows:
     - `hugegraph-core`:
       - `VertexCoreTest.testAddVertexWithDateDefaultValue()`
       - `SchemaElementTest.testPropertyKeyFromMapNormalizesDateDefaultValue()`
       - 
`SchemaElementTest.testPropertyKeyFromMapNormalizesDateSetDefaultValue()`
     - Serializers:
       - `TextSerializerTest.testPropertyKeyDefaultValueRoundTripsAsDate()`
       - `BinarySerializerTest.testPropertyKeyDefaultValueRoundTripsAsDate()`
     - `hugegraph-struct`:
       - `PropertyKeyTest.testDefaultValueNormalizedToDate()`
   
   All existing tests remain green.
   
   ## Does this PR potentially affect the following parts?
   
   - [ ] Dependencies
   - [ ] Modify configurations
   - [ ] The public API
   - [x] Other affects (typed here)
     - Tightens the behavior of `PropertyKey.defaultValue()` for malformed or 
type-incompatible defaults: such values are now validated and may trigger 
conversion errors instead of leaking unexpected raw `String` values at read 
time.
   - [ ] Nope
   
   ## Documentation Status
   
   - [ ] `Doc - TODO`
   - [ ] `Doc - Done`
   - [x] `Doc - No Need`


-- 
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]

Reply via email to