@@ -858,4 +858,67 @@ public function testErrorHandlerChaining(): void
858858 $ this ->assertEquals ('First error handler failed ' , $ e ->getPrevious ()->getMessage ());
859859 }
860860 }
861+
862+ public function testOptionalParamsWithNullUseDefault (): void
863+ {
864+ // Test when optional param receives null, it should use default value
865+ // But empty string should be preserved as-is
866+ $ route = new Route ('GET ' , '/path ' );
867+ $ route
868+ ->param ('x ' , 'x-default ' , new Text (200 ), 'x param ' , true )
869+ ->param ('y ' , 'y-default ' , new Text (200 ), 'y param ' , true )
870+ ->action (function ($ x , $ y ) {
871+ echo $ x . '- ' . $ y ;
872+ });
873+
874+ // Test with null value - should use default
875+ \ob_start ();
876+ $ request = new UtopiaRequestTest ();
877+ $ request ::_setParams (['x ' => null , 'y ' => 'y-value ' ]);
878+ $ this ->app ->execute ($ route , $ request , new Response ());
879+ $ result = \ob_get_contents ();
880+ \ob_end_clean ();
881+
882+ $ this ->assertEquals ('x-default-y-value ' , $ result );
883+
884+ // Test with empty string value - should keep empty string
885+ \ob_start ();
886+ $ request = new UtopiaRequestTest ();
887+ $ request ::_setParams (['x ' => '' , 'y ' => 'y-value ' ]);
888+ $ this ->app ->execute ($ route , $ request , new Response ());
889+ $ result = \ob_get_contents ();
890+ \ob_end_clean ();
891+
892+ $ this ->assertEquals ('-y-value ' , $ result );
893+
894+ // Test with null in one param and empty string in another
895+ \ob_start ();
896+ $ request = new UtopiaRequestTest ();
897+ $ request ::_setParams (['x ' => null , 'y ' => '' ]);
898+ $ this ->app ->execute ($ route , $ request , new Response ());
899+ $ result = \ob_get_contents ();
900+ \ob_end_clean ();
901+
902+ $ this ->assertEquals ('x-default- ' , $ result );
903+
904+ // Test that valid values are still used
905+ \ob_start ();
906+ $ request = new UtopiaRequestTest ();
907+ $ request ::_setParams (['x ' => 'x-value ' , 'y ' => 'y-value ' ]);
908+ $ this ->app ->execute ($ route , $ request , new Response ());
909+ $ result = \ob_get_contents ();
910+ \ob_end_clean ();
911+
912+ $ this ->assertEquals ('x-value-y-value ' , $ result );
913+
914+ // Test that numeric zero is still treated as a valid value (not null)
915+ \ob_start ();
916+ $ request = new UtopiaRequestTest ();
917+ $ request ::_setParams (['x ' => '0 ' , 'y ' => 'y-value ' ]);
918+ $ this ->app ->execute ($ route , $ request , new Response ());
919+ $ result = \ob_get_contents ();
920+ \ob_end_clean ();
921+
922+ $ this ->assertEquals ('0-y-value ' , $ result );
923+ }
861924}
0 commit comments