33import logging
44import json
55import copy
6+ from datetime import datetime
67
78from typing import NoReturn , Tuple
89
@@ -95,11 +96,10 @@ def rocketpy_encoder(obj, config: DiscretizeConfig = DiscretizeConfig()):
9596 include_outputs = True ,
9697 include_function_data = True ,
9798 )
98- encoder = RocketPyEncoder (
99- include_outputs = True ,
100- include_function_data = True ,
101- )
102- return encoder .default (obj_copy )
99+ encoded_result = json .loads (json_str )
100+
101+ # Post-process to fix datetime fields that got converted to lists
102+ return _fix_datetime_fields (encoded_result )
103103 except Exception as e :
104104 logger .warning (f"Failed to encode with RocketPyEncoder: { e } " )
105105 attributes = {}
@@ -114,6 +114,40 @@ def rocketpy_encoder(obj, config: DiscretizeConfig = DiscretizeConfig()):
114114 return attributes
115115
116116
117+ def _fix_datetime_fields (data ):
118+ """
119+ Fix datetime fields that RocketPyEncoder converted to lists.
120+ """
121+ if isinstance (data , dict ):
122+ fixed = {}
123+ for key , value in data .items ():
124+ if (
125+ key in ['date' , 'local_date' , 'datetime_date' ]
126+ and isinstance (value , list )
127+ and len (value ) >= 3
128+ ):
129+ # Convert [year, month, day, hour, ...] back to datetime
130+ try :
131+ year , month , day = value [0 :3 ]
132+ hour = value [3 ] if len (value ) > 3 else 0
133+ minute = value [4 ] if len (value ) > 4 else 0
134+ second = value [5 ] if len (value ) > 5 else 0
135+ microsecond = value [6 ] if len (value ) > 6 else 0
136+
137+ fixed [key ] = datetime (
138+ year , month , day , hour , minute , second , microsecond
139+ )
140+ except (ValueError , TypeError , IndexError ):
141+ # If conversion fails, keep the original value
142+ fixed [key ] = value
143+ else :
144+ fixed [key ] = _fix_datetime_fields (value )
145+ return fixed
146+ if isinstance (data , (list , tuple )):
147+ return [_fix_datetime_fields (item ) for item in data ]
148+ return data
149+
150+
117151class RocketPyGZipMiddleware :
118152 def __init__ (
119153 self , app : ASGIApp , minimum_size : int = 500 , compresslevel : int = 9
@@ -231,6 +265,13 @@ async def send_with_gzip(self, message: Message) -> None:
231265
232266 await self .send (message )
233267
268+ else :
269+ # Pass through other message types unmodified.
270+ if not self .started :
271+ self .started = True
272+ await self .send (self .initial_message )
273+ await self .send (message )
274+
234275
235276async def unattached_send (message : Message ) -> NoReturn :
236277 raise RuntimeError ("send awaitable not set" ) # pragma: no cover
0 commit comments