laserninja commented on code in PR #12859:
URL: https://github.com/apache/gravitino/pull/12859#discussion_r4135739233


##########
clients/client-python/gravitino/dto/semantic/semantic_model_definition_dto.py:
##########
@@ -0,0 +1,121 @@
+# 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.
+
+from dataclasses import dataclass, field
+from typing import Optional
+
+from dataclasses_json import DataClassJsonMixin, config
+
+from gravitino.api.semantic.semantic_model_definition import 
SemanticModelDefinition
+from gravitino.dto.semantic.ai_context_dto import AIContextDTO
+from gravitino.dto.semantic.custom_extension_dto import CustomExtensionDTO
+from gravitino.dto.semantic.dataset_dto import DatasetDTO
+from gravitino.dto.semantic.json_serdes.ai_context_serdes import 
AIContextSerdes
+from gravitino.dto.semantic.metric_dto import MetricDTO
+from gravitino.dto.semantic.relationship_dto import RelationshipDTO
+from gravitino.dto.semantic.semantic_dto_utils import convert_list, is_none
+
+
+@dataclass
+class SemanticModelDefinitionDTO(DataClassJsonMixin):
+    """Represents a Semantic Model definition DTO."""
+
+    _ai_context: Optional[AIContextDTO] = field(
+        default=None,
+        metadata=config(
+            field_name="ai_context",

Review Comment:
   Fixed in bbd8c09bc. All eight field mappings now use the Java camelCase wire 
names. Added an explicit Java-contract JSON regression that deserializes a 
complete definition into the API and checks the outgoing JSON, including 
relationships and optional fields.



##########
clients/client-python/gravitino/dto/semantic/json_serdes/ai_context_serdes.py:
##########
@@ -0,0 +1,104 @@
+# 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.
+
+from typing import Any, Optional, Union
+
+from gravitino.dto.semantic.ai_context_dto import AIContextDTO
+from gravitino.dto.semantic.ai_context_object_dto import AIContextObjectDTO
+from gravitino.utils.precondition import Precondition
+
+_INSTRUCTIONS = "instructions"
+_SYNONYMS = "synonyms"
+_EXAMPLES = "examples"
+
+
+class AIContextSerdes:
+    """Serdes for AI context DTOs.
+
+    An AI context is either a bare JSON string or a JSON object whose unknown
+    properties are retained losslessly.
+    """
+
+    @staticmethod
+    def serialize(
+        value: Optional[AIContextDTO],
+    ) -> Optional[Union[str, dict[str, Any]]]:
+        """Encode an AI context DTO to a string or a dictionary."""
+        if value is None:
+            return None
+        if value.text() is not None:
+            return value.text()
+        return _serialize_object(value.object())
+
+    @staticmethod
+    def deserialize(
+        value: Optional[Union[str, dict[str, Any]]],
+    ) -> Optional[AIContextDTO]:
+        """Decode an AI context DTO from a string or a dictionary."""
+        if value is None:
+            return None
+        if isinstance(value, str):
+            return AIContextDTO(text=value)
+        Precondition.check_argument(
+            isinstance(value, dict), "AI context must be a string or object"
+        )
+        return AIContextDTO(obj=_deserialize_object(value))
+
+
+def _serialize_object(value: AIContextObjectDTO) -> dict[str, Any]:
+    serialized: dict[str, Any] = {}
+    if value.instructions() is not None:
+        serialized[_INSTRUCTIONS] = value.instructions()
+    if value.synonyms() is not None:
+        serialized[_SYNONYMS] = value.synonyms()
+    if value.examples() is not None:
+        serialized[_EXAMPLES] = value.examples()
+    serialized.update(value.additional_properties())

Review Comment:
   Fixed in bbd8c09bc. Semantic DTOs now share a decimal-aware JSON serializer 
using simplejson, and from_json parses fractional numbers as Decimal by 
default. Regression tests cover exact nested numeric values in both directions, 
all AI-context-bearing DTOs, enclosing models, and low decimal precision.



##########
clients/client-python/gravitino/dto/semantic/json_serdes/ai_context_serdes.py:
##########
@@ -0,0 +1,104 @@
+# 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.
+
+from typing import Any, Optional, Union
+
+from gravitino.dto.semantic.ai_context_dto import AIContextDTO
+from gravitino.dto.semantic.ai_context_object_dto import AIContextObjectDTO
+from gravitino.utils.precondition import Precondition
+
+_INSTRUCTIONS = "instructions"
+_SYNONYMS = "synonyms"
+_EXAMPLES = "examples"
+
+
+class AIContextSerdes:
+    """Serdes for AI context DTOs.
+
+    An AI context is either a bare JSON string or a JSON object whose unknown
+    properties are retained losslessly.
+    """
+
+    @staticmethod
+    def serialize(
+        value: Optional[AIContextDTO],
+    ) -> Optional[Union[str, dict[str, Any]]]:
+        """Encode an AI context DTO to a string or a dictionary."""
+        if value is None:
+            return None
+        if value.text() is not None:
+            return value.text()
+        return _serialize_object(value.object())
+
+    @staticmethod
+    def deserialize(
+        value: Optional[Union[str, dict[str, Any]]],
+    ) -> Optional[AIContextDTO]:
+        """Decode an AI context DTO from a string or a dictionary."""
+        if value is None:
+            return None
+        if isinstance(value, str):
+            return AIContextDTO(text=value)
+        Precondition.check_argument(
+            isinstance(value, dict), "AI context must be a string or object"
+        )
+        return AIContextDTO(obj=_deserialize_object(value))
+
+
+def _serialize_object(value: AIContextObjectDTO) -> dict[str, Any]:
+    serialized: dict[str, Any] = {}
+    if value.instructions() is not None:
+        serialized[_INSTRUCTIONS] = value.instructions()
+    if value.synonyms() is not None:
+        serialized[_SYNONYMS] = value.synonyms()
+    if value.examples() is not None:
+        serialized[_EXAMPLES] = value.examples()
+    serialized.update(value.additional_properties())
+    return serialized
+
+
+def _deserialize_object(value: dict[str, Any]) -> AIContextObjectDTO:
+    additional_properties = {
+        name: item
+        for name, item in value.items()
+        if name not in (_INSTRUCTIONS, _SYNONYMS, _EXAMPLES)
+    }
+    return AIContextObjectDTO(
+        instructions=_read_string(value, _INSTRUCTIONS),
+        synonyms=_read_string_list(value, _SYNONYMS),
+        examples=_read_string_list(value, _EXAMPLES),
+        additional_properties=additional_properties,
+    )
+
+
+def _read_string(value: dict[str, Any], name: str) -> Optional[str]:
+    if name not in value:
+        return None
+    item = value[name]
+    Precondition.check_argument(isinstance(item, str), f"{name} must be a 
string")

Review Comment:
   Fixed in bbd8c09bc. Both helpers now treat explicit null and missing 
properties as unset. Tests cover omission on serialization, preservation of 
unknown null properties and empty arrays, and continued rejection of null array 
elements and non-string instructions.



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

Reply via email to