Skip to content

fix path merge with path object #2245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/Mojo/Path.pm
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,18 @@ sub merge {
my ($self, $path) = @_;

# Replace
return $self->parse($path) if $path =~ m!^/!;
if (ref $path) {
Copy link
Preview

Copilot AI Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the object-handling branch, the final return uses the setter leading_slash, which may not explicitly return $self. To ensure merge remains chainable and returns the modified object, explicitly return $self; after updating parts and slash attributes.

Copilot uses AI. Check for mistakes.

if ($path->leading_slash) {
@{$self->parts} = @{$path->parts};
$self->trailing_slash($path->trailing_slash);
return $self->leading_slash($path->leading_slash);
}
}
elsif ($path =~ m!^/!) { return $self->parse($path) }

# Merge
pop @{$self->parts} unless $self->trailing_slash;
$path = $self->new($path);
pop @{$self->parts} unless $self->trailing_slash;
$path = $self->new($path) unless ref $path;
push @{$self->parts}, @{$path->parts};
return $self->trailing_slash($path->trailing_slash);
}
Expand Down
26 changes: 26 additions & 0 deletions t/mojo/path.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use Mojo::Base -strict;

use Test::More;
use Mojo::Path;
use Mojo::Util qw(encode url_escape);

subtest 'Basic functionality' => sub {
my $path = Mojo::Path->new;
Expand Down Expand Up @@ -170,6 +171,31 @@ subtest 'Merge' => sub {
is $path->to_route, '/foo/baz/yada', 'right route';
};

subtest 'Merge path object' => sub {
my $charset = 'ISO-8859-15';
my $part = 'b€r';
my $part_enc = url_escape(encode($charset, $part));
my $parse_path = sub { Mojo::Path->new->charset($charset)->parse(@_) };

for my $has_trailing_slash (!!0, !!1) {
my $trailing_slash = $has_trailing_slash ? '/' : '';
my $trailing_slash_diag = 'has'.($has_trailing_slash ? '' : ' no').' trailing slash';
my $path = $parse_path->("/$part_enc/");
$path->merge($parse_path->($part_enc.$trailing_slash));
is_deeply $path->parts, [($part) x 2], 'right structure';
is "$path", "/$part_enc/$part_enc".$trailing_slash, 'right path';
ok $path->leading_slash, 'has leading slash';
is $path->trailing_slash, $has_trailing_slash, $trailing_slash_diag;
is $path->to_route, "/$part/$part".$trailing_slash, 'right route';
$path = $parse_path->("/foo/")->merge($parse_path->("/$part_enc".$trailing_slash));
is_deeply $path->parts, [$part], 'right structure';
is "$path", "/$part_enc".$trailing_slash, 'right path';
ok $path->leading_slash, 'has leading slash';
is $path->trailing_slash, $has_trailing_slash, $trailing_slash_diag;
is $path->to_route, "/$part".$trailing_slash, 'right route';
}
};

subtest 'Empty path elements' => sub {
my $path = Mojo::Path->new('//');
is "$path", '//', 'right path';
Expand Down
Loading