mojolicious / mojo

:sparkles: Mojolicious - Perl real-time web framework
https://mojolicious.org
Artistic License 2.0
2.66k stars 576 forks source link

Bug(s) in url_for? #1781

Open reneeb opened 3 years ago

reneeb commented 3 years ago

Steps to reproduce the behavior

#!/usr/bin/perl

use Mojolicious::Lite -strict;

under '/admin/:area' => [ area => [qw/test hallo/] ] => { area => 'hallo' };

get( '/:tag' )->name('single_tag');

say app->url_for( 'single_tag', tag => 'test', area => 'hallo' );
say app->url_for( 'single_tag', tag => 'test', area => 'bug' );
say app->url_for( 'single_tag', tag => 'test', area => 'world' );
say app->url_for( 'single_tag', tag => 'test' );
say app->url_for( 'single_tag', tag => 'test', format => 'json' );

Prints

❯ perl url_for.pl 
/admin/test
/admin/bug/test
/admin/world/test
/admin/test
/admin/hallo/test.json

Expected behavior

I would expect that the area placeholder is replaced with hallo in the first and fourth call.

And as content negotiation via file suffix was disabled by default in 9.11 and 9.17 added ?_format=*, I would expect url_for to return /admin/hallo/test?_format=json in the last call.

This is all done in https://github.com/mojolicious/mojo/blob/main/lib/Mojolicious/Routes/Pattern.pm (sub render)

kraih commented 3 years ago

You made the placeholder optional, and usually url_for will try to make the path as short as possible.

reneeb commented 3 years ago

Is it really optional when there are other parts after the placeholder? url_for generates URLs that result in 404s.