Skip to content

Commit

Permalink
Merge branch 'bugs/fix-template-recursion'
Browse files Browse the repository at this point in the history
  • Loading branch information
cromedome committed Feb 5, 2023
2 parents 1f0e7f2 + 29d59f3 commit d1be188
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[ BUG FIXES ]
* PR #1247: Fix edge case in plugin compat (Sawyer X)
* GH #1621: Fix recursion error in TT after longjump (Andy Beverley)
* PR #1667: Remove failing module from GitHub Actions
config (Jason A. Crome)

Expand Down
13 changes: 13 additions & 0 deletions lib/Dancer2/Core/App.pm
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,19 @@ sub template {
and $self->setup_session;

# return content
if ($self->has_with_return) {
my $old_with_return = $self->with_return;
my $local_response;
$self->set_with_return( sub {
$local_response ||= shift;
});
my $content = $template->process( @_ );
$self->set_with_return($old_with_return);
if ($local_response) {
$self->with_return->($local_response);
}
return $content;
}
return $template->process( @_ );
}

Expand Down
52 changes: 52 additions & 0 deletions t/issues/gh-1621/gh-1621.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;
use Plack::Test;
use HTTP::Request::Common;
use Ref::Util qw<is_coderef>;

# Test to ensure that a longjump out of a template, caused by a call to redirect,
# does not cause any future problems rendering a template. Whilst this test is
# slightly obscure for reasons of simplicity, the exact use-case is in the use
# of Plugin::LogReport. If it catches a fatal error (which could occur during
# the render of a template) then it will redirect to another page.

{
package MyApp;

use Dancer2;

set template => 'template_toolkit';

# Set up 2 routes to the same template. The first route will redirect when
# the template renders, the second will not
get '/redirect' => sub {
my $redir = sub {
redirect "somewhere";
};
template 'redirect',
{ redirect_sub => $redir }
};

get '/noredirect' => sub {
template 'redirect';
};
}

my $app = Dancer2->psgi_app;
ok( is_coderef($app), 'Got app' );

test_psgi $app, sub {
my $cb = shift;
my $res = $cb->(GET '/redirect');
is $res->code, 302, 'Redirect template results in redirect';

$res = $cb->(GET '/noredirect');
is $res->code, 200, 'Successful subsequent request to normal template';
like $res->content, qr/foobar/, 'Correct content';
};

done_testing();
1 change: 1 addition & 0 deletions t/issues/gh-1621/views/redirect.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[% IF NOT redirect_sub %]foobar[% END %]

0 comments on commit d1be188

Please sign in to comment.