fix bug:
1. Service_type do not set after add server 2. Issue relate pricing when "Update from OpenAPI" 3. Failed to set SQLAlchemy cache for key Error 4. Request Body Logging in Internal Service 5. Deleted API Key Remains Valid for 5 Minutes
This commit is contained in:
1 parent
284880a170
commit
1846aafc1e
10 files changed
+75
-83
No files matched your search
@@ -1,5 +1,8 @@
|
||||
from typing import get_origin, get_args
|
||||
from enum import Enum as PyEnum
|
||||
from decimal import Decimal
|
||||
from datetime import datetime
|
||||
from sqlalchemy import DateTime, Numeric
|
||||
|
||||
|
||||
class SqlalchemyUtils:
|
||||
@@ -7,10 +10,13 @@ class SqlalchemyUtils:
|
||||
@staticmethod
|
||||
def model_to_dict(model):
|
||||
result = {column.name: getattr(model, column.name) for column in model.__table__.columns}
|
||||
# Normalize Enum values to primitive for safe serialization
|
||||
for key, value in list(result.items()):
|
||||
if isinstance(value, PyEnum):
|
||||
result[key] = value.value
|
||||
elif isinstance(value, Decimal):
|
||||
result[key] = str(value)
|
||||
elif isinstance(value, datetime):
|
||||
result[key] = value.isoformat()
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
@@ -27,31 +33,42 @@ class SqlalchemyUtils:
|
||||
"""
|
||||
instance = model_class()
|
||||
|
||||
# Get all column names for the model
|
||||
column_names = {column.name for column in model_class.__table__.columns}
|
||||
# Map column definitions by name for type-aware restoration
|
||||
columns_by_name = {column.name: column for column in model_class.__table__.columns}
|
||||
|
||||
# Set attributes that exist in both the dictionary and the model
|
||||
for key, value in data_dict.items():
|
||||
if key in column_names and hasattr(instance, key):
|
||||
# Attempt to restore Enum types from strings using type annotations
|
||||
annotations = getattr(model_class, "__annotations__", {})
|
||||
ann = annotations.get(key)
|
||||
target_type = None
|
||||
if ann is not None:
|
||||
origin = get_origin(ann)
|
||||
if origin is None:
|
||||
target_type = ann
|
||||
else:
|
||||
args = get_args(ann)
|
||||
if args:
|
||||
target_type = args[0]
|
||||
column = columns_by_name.get(key)
|
||||
if not column:
|
||||
continue
|
||||
|
||||
if isinstance(target_type, type) and issubclass(target_type, PyEnum) and isinstance(value, str):
|
||||
try:
|
||||
value = target_type(value)
|
||||
except Exception:
|
||||
pass
|
||||
# Restore Enum from string using annotations if available
|
||||
annotations = getattr(model_class, "__annotations__", {})
|
||||
ann = annotations.get(key)
|
||||
target_type = None
|
||||
if ann is not None:
|
||||
origin = get_origin(ann)
|
||||
if origin is None:
|
||||
target_type = ann
|
||||
else:
|
||||
args = get_args(ann)
|
||||
if args:
|
||||
target_type = args[0]
|
||||
if isinstance(target_type, type) and issubclass(target_type, PyEnum) and isinstance(value, str):
|
||||
try:
|
||||
value = target_type(value)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
setattr(instance, key, value)
|
||||
# Restore Numeric and DateTime types from JSON-friendly forms
|
||||
try:
|
||||
if isinstance(column.type, Numeric) and value is not None:
|
||||
if not isinstance(value, Decimal):
|
||||
value = Decimal(str(value))
|
||||
elif isinstance(column.type, DateTime) and isinstance(value, str):
|
||||
value = datetime.fromisoformat(value)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
setattr(instance, key, value)
|
||||
|
||||
return instance
|
||||
Reference in new issue
Block a user