fix path merge with path object#2245
Open
sparrow2009 wants to merge 1 commit intomojolicious:mainfrom
Open
Conversation
Mojo::Path's support of merging objects of class Mojo::Path (vs. strings) until now is implemented by object stringification followed by a reparsing using a default Mojo::Path object. This approach works fine for merged objects with a character set of UTF-8 as this is also the default character set for Mojo::Path objects. It can fail however for other character sets. Instead copy the path parts and the slash attributes from the source to the target object. The missing tests for merging objects are included using the ISO-8859-15 character set. That way those tests can also function as regression tests. The ISO-8859-15 character set deviates from ISO-8859-1 and in turn from Unicode in that it reassigns code points of less common symbols (code point A4 which is the currency sign in ISO-8859-1/Unicode is replaced by the Euro currency sign e.g.). Background: Mojo::URL::to_abs() e.g. passes path objects to Mojo::Path::merge() in order to merge the path onto the base path today. And can fail in the way outlined above if the path object is not configured to be UTF-8.
There was a problem hiding this comment.
Pull Request Overview
This PR updates Mojo::Path::merge to properly merge another Mojo::Path object by copying its parts and slash attributes instead of stringifying and reparsing, and adds tests (using ISO-8859-15) to cover this behavior.
- Adjusted
mergelogic inlib/Mojo/Path.pmto detect and mergeMojo::Pathobjects directly - Added
Merge path objectsubtest int/mojo/path.tto validate correct structure and encoding handling
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| t/mojo/path.t | Added use Mojo::Util qw(encode url_escape) and new subtest for merging paths with non-UTF-8 charset |
| lib/Mojo/Path.pm | Enhanced merge to handle Mojo::Path objects without reparsing |
Comments suppressed due to low confidence (1)
lib/Mojo/Path.pm:44
- [nitpick] The indentation and brace placement in the new
mergeblock differ from the surrounding code style. Align the new block with existing indentation patterns for better readability and consistency.
if (ref $path) {
|
|
||
| # Replace | ||
| return $self->parse($path) if $path =~ m!^/!; | ||
| if (ref $path) { |
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Mojo::Path's support of merging objects of classMojo::Path(vs. strings) until now is implemented by object stringification followed by a reparsing using a defaultMojo::Pathobject.This approach works fine for merged objects with a character set of UTF-8 as this is also the default character set for
Mojo::Pathobjects. It can fail however for other character sets.Instead copy the path parts and the slash attributes from the source to the target object.
The missing tests for merging objects are included using the ISO-8859-15 character set. That way those tests can also function as regression tests. The ISO-8859-15 character set deviates from ISO-8859-1 and in turn from Unicode in that it reassigns code points of less common symbols (code point A4 which is the currency sign in ISO-8859-1/Unicode is replaced by the Euro currency sign e.g.).
Background:
Mojo::URL::to_abs()e.g. passes path objects toMojo::Path::merge()in order to merge the path onto the base path today. And can fail in the way outlined above if the path object is not configured to be UTF-8.