@@ -69,6 +69,26 @@ fn networking_to_proto(n: &Networking) -> pb::NetworkingConfig {
6969 pb:: NetworkingConfig { mode : mode. into ( ) }
7070}
7171
72+ fn sanitize_optional_string ( value : Option < String > ) -> Option < String > {
73+ value. and_then ( |value| {
74+ if value. trim ( ) . is_empty ( ) {
75+ None
76+ } else {
77+ Some ( value)
78+ }
79+ } )
80+ }
81+
82+ fn sanitize_optional_str < ' a > ( value : Option < & ' a str > ) -> Option < & ' a str > {
83+ value. and_then ( |value| {
84+ if value. trim ( ) . is_empty ( ) {
85+ None
86+ } else {
87+ Some ( value)
88+ }
89+ } )
90+ }
91+
7292#[ derive( Debug , Deserialize ) ]
7393pub struct InstanceInfo {
7494 #[ serde( default ) ]
@@ -271,8 +291,9 @@ impl VmInfo {
271291 } ,
272292 app_url : self
273293 . gateway_enabled
274- . then_some ( self . instance_id . as_ref ( ) )
294+ . then_some ( self . instance_id . as_deref ( ) )
275295 . flatten ( )
296+ . and_then ( |id| sanitize_optional_str ( Some ( id) ) )
276297 . map ( |id| {
277298 // Use custom gateway URL if available, otherwise fall back to global config
278299 if let Some ( custom_gw_url) = custom_gateway_urls. first ( ) {
@@ -297,7 +318,7 @@ impl VmInfo {
297318 }
298319 } ) ,
299320 app_id : self . manifest . app_id . clone ( ) ,
300- instance_id : self . instance_id . as_deref ( ) . map ( Into :: into ) ,
321+ instance_id : sanitize_optional_string ( self . instance_id . clone ( ) ) ,
301322 exited_at : self . exited_at . clone ( ) ,
302323 events : self . events . clone ( ) ,
303324 }
@@ -336,7 +357,8 @@ impl VmState {
336357 }
337358 let uptime = display_ts ( proc_state. and_then ( |info| info. state . started_at . as_ref ( ) ) ) ;
338359 let exited_at = display_ts ( proc_state. and_then ( |info| info. state . stopped_at . as_ref ( ) ) ) ;
339- let instance_id = workdir. instance_info ( ) . ok ( ) . map ( |info| info. instance_id ) ;
360+ let instance_id =
361+ sanitize_optional_string ( workdir. instance_info ( ) . ok ( ) . map ( |info| info. instance_id ) ) ;
340362 VmInfo {
341363 manifest : self . config . manifest . clone ( ) ,
342364 workdir : workdir. path ( ) . to_path_buf ( ) ,
@@ -354,6 +376,31 @@ impl VmState {
354376 }
355377}
356378
379+ #[ cfg( test) ]
380+ mod tests {
381+ use super :: { sanitize_optional_str, sanitize_optional_string} ;
382+
383+ #[ test]
384+ fn sanitize_optional_string_filters_empty_values ( ) {
385+ assert_eq ! ( sanitize_optional_string( Some ( String :: new( ) ) ) , None ) ;
386+ assert_eq ! ( sanitize_optional_string( Some ( " " . to_string( ) ) ) , None ) ;
387+ assert_eq ! (
388+ sanitize_optional_string( Some ( "instance-123" . to_string( ) ) ) ,
389+ Some ( "instance-123" . to_string( ) )
390+ ) ;
391+ }
392+
393+ #[ test]
394+ fn sanitize_optional_str_filters_empty_values ( ) {
395+ assert_eq ! ( sanitize_optional_str( Some ( "" ) ) , None ) ;
396+ assert_eq ! ( sanitize_optional_str( Some ( " " ) ) , None ) ;
397+ assert_eq ! (
398+ sanitize_optional_str( Some ( "instance-123" ) ) ,
399+ Some ( "instance-123" )
400+ ) ;
401+ }
402+ }
403+
357404impl VmConfig {
358405 pub fn config_qemu (
359406 & self ,
0 commit comments