amoghrajesh commented on code in PR #77523:
URL: https://github.com/apache/airflow/pull/77523#discussion_r4678587067


##########
task-sdk/src/airflow/sdk/serde/serializers/kubernetes.py:
##########
@@ -55,10 +60,46 @@ def safe_get_name(pod):
                 return None
 
         try:
-            return PodGenerator.serialize_pod(o), qualname(o), __version__, 
True
+            return ApiClient().sanitize_for_serialization(o), qualname(o), 
__version__, True
         except Exception:
             log.warning("Serialization failed for pod %s", safe_get_name(o))
             log.debug("traceback for serialization error", exc_info=True)
             return "", "", 0, False
 
     return "", "", 0, False
+
+
+def deserialize(cls: type, version: int, data: dict) -> Any:
+    if version > __version__:
+        raise TypeError(f"serialized version {version} is newer than class 
version {__version__}")
+
+    if not is_pydantic_model(cls):
+        raise TypeError(f"do not know how to deserialize {qualname(cls)}")
+    model = cast("type[pydantic.BaseModel]", cls)
+
+    try:
+        return model.model_validate(data)
+    except pydantic.ValidationError as e:
+        try:
+            coerced = _coerce_legacy_values(data, e)
+        except (KeyError, TypeError, ValueError):
+            raise e
+        return model.model_validate(coerced)
+
+
+def _coerce_legacy_values(data: dict, error: pydantic.ValidationError) -> dict:
+    # kubernetes<37 stored numeric quantities/labels and string booleans that 
the
+    # pydantic models of kubernetes>=37 reject in strict mode.
+    for err in error.errors():
+        *path, key = err["loc"]
+        parent: Any = data
+        for part in path:
+            parent = parent[part]
+        value = parent[key]
+        if err["type"] == "string_type" and isinstance(value, bool):
+            parent[key] = str(value).lower()
+        elif err["type"] == "string_type" and isinstance(value, (int, float)):
+            parent[key] = str(value)
+        elif err["type"] == "bool_type" and str(value).lower() in ("true", 
"false"):
+            parent[key] = str(value).lower() == "true"
+    return data

Review Comment:
   Handled in 417e25a1213



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