@@ -69,6 +69,10 @@ fn networking_to_proto(n: &Networking) -> pb::NetworkingConfig {
6969 pb:: NetworkingConfig { mode : mode. into ( ) }
7070}
7171
72+ fn sanitize_optional < T : AsRef < str > > ( value : Option < T > ) -> Option < T > {
73+ value. filter ( |value| !value. as_ref ( ) . trim ( ) . is_empty ( ) )
74+ }
75+
7276#[ derive( Debug , Deserialize ) ]
7377pub struct InstanceInfo {
7478 #[ serde( default ) ]
@@ -271,8 +275,9 @@ impl VmInfo {
271275 } ,
272276 app_url : self
273277 . gateway_enabled
274- . then_some ( self . instance_id . as_ref ( ) )
278+ . then_some ( self . instance_id . as_deref ( ) )
275279 . flatten ( )
280+ . and_then ( |id| sanitize_optional ( Some ( id) ) )
276281 . map ( |id| {
277282 // Use custom gateway URL if available, otherwise fall back to global config
278283 if let Some ( custom_gw_url) = custom_gateway_urls. first ( ) {
@@ -297,7 +302,7 @@ impl VmInfo {
297302 }
298303 } ) ,
299304 app_id : self . manifest . app_id . clone ( ) ,
300- instance_id : self . instance_id . as_deref ( ) . map ( Into :: into ) ,
305+ instance_id : sanitize_optional ( self . instance_id . clone ( ) ) ,
301306 exited_at : self . exited_at . clone ( ) ,
302307 events : self . events . clone ( ) ,
303308 }
@@ -336,7 +341,8 @@ impl VmState {
336341 }
337342 let uptime = display_ts ( proc_state. and_then ( |info| info. state . started_at . as_ref ( ) ) ) ;
338343 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 ) ;
344+ let instance_id =
345+ sanitize_optional ( workdir. instance_info ( ) . ok ( ) . map ( |info| info. instance_id ) ) ;
340346 VmInfo {
341347 manifest : self . config . manifest . clone ( ) ,
342348 workdir : workdir. path ( ) . to_path_buf ( ) ,
@@ -354,6 +360,31 @@ impl VmState {
354360 }
355361}
356362
363+ #[ cfg( test) ]
364+ mod tests {
365+ use super :: sanitize_optional;
366+
367+ #[ test]
368+ fn sanitize_optional_filters_empty_owned_values ( ) {
369+ assert_eq ! ( sanitize_optional( Some ( String :: new( ) ) ) , None ) ;
370+ assert_eq ! ( sanitize_optional( Some ( " " . to_string( ) ) ) , None ) ;
371+ assert_eq ! (
372+ sanitize_optional( Some ( "instance-123" . to_string( ) ) ) ,
373+ Some ( "instance-123" . to_string( ) )
374+ ) ;
375+ }
376+
377+ #[ test]
378+ fn sanitize_optional_filters_empty_borrowed_values ( ) {
379+ assert_eq ! ( sanitize_optional( Some ( "" ) ) , None ) ;
380+ assert_eq ! ( sanitize_optional( Some ( " " ) ) , None ) ;
381+ assert_eq ! (
382+ sanitize_optional( Some ( "instance-123" ) ) ,
383+ Some ( "instance-123" )
384+ ) ;
385+ }
386+ }
387+
357388impl VmConfig {
358389 pub fn config_qemu (
359390 & self ,
0 commit comments