Skip to content

Commit ece0530

Browse files
committed
Fix regression on URI.parse/1, closes #11363
1 parent 48b838a commit ece0530

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

lib/elixir/lib/uri.ex

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,15 @@ defmodule URI do
668668
{:error, _} -> %URI{path: path}
669669
end
670670
671-
Also note this function sets the authority field, but the field has been
672-
deprecated and it is not set by `URI.new!/1` and `URI.new/1`.
671+
There are two differencws in the behaviour of this function compared to
672+
`URI.new/1`:
673+
674+
* This function sets the deprecated authority field
675+
676+
* This function sets the path to `nil` when it is empty,
677+
while `new/1` consider the path always exists and sets it
678+
to an empty string
679+
673680
"""
674681
# TODO: Deprecate me at least on v1.17
675682
@doc deprecated: "Use URI.new/1 or URI.new!/1 instead"
@@ -706,6 +713,7 @@ defmodule URI do
706713
],
707714
parts
708715

716+
path = nillify(path)
709717
scheme = nillify(scheme)
710718
query = nillify_query(query_with_question_mark)
711719
{authority, userinfo, host, port} = split_authority(authority_with_slashes)
@@ -817,6 +825,8 @@ defmodule URI do
817825
merge(parse(base), parse(rel))
818826
end
819827

828+
# TODO: Deprecate me on Elixir v1.19
829+
defp merge_paths(nil, rel_path), do: merge_paths("/", rel_path)
820830
defp merge_paths("", rel_path), do: merge_paths("/", rel_path)
821831
defp merge_paths(_, "/" <> _ = rel_path), do: remove_dot_segments_from_path(rel_path)
822832

lib/elixir/test/elixir/uri_test.exs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ defmodule URITest do
449449
query: nil,
450450
fragment: nil,
451451
port: 443,
452-
path: "",
452+
path: nil,
453453
userinfo: nil
454454
}
455455

@@ -551,7 +551,7 @@ defmodule URITest do
551551
expected_uri = %URI{
552552
scheme: "http",
553553
host: "foo.com",
554-
path: "",
554+
path: nil,
555555
query: nil,
556556
fragment: nil,
557557
port: 4444,
@@ -564,7 +564,7 @@ defmodule URITest do
564564
expected_uri = %URI{
565565
scheme: "https",
566566
host: "foo.com",
567-
path: "",
567+
path: nil,
568568
query: nil,
569569
fragment: nil,
570570
port: 443,
@@ -577,7 +577,7 @@ defmodule URITest do
577577
expected_uri = %URI{
578578
scheme: "http",
579579
host: "foo.com",
580-
path: "",
580+
path: nil,
581581
query: nil,
582582
fragment: nil,
583583
port: 4444,
@@ -655,5 +655,11 @@ defmodule URITest do
655655
test "preserves an empty query" do
656656
assert URI.parse("http://foo.com/?").query == ""
657657
end
658+
659+
test "merges empty path" do
660+
base = URI.parse("http://example.com")
661+
assert URI.merge(base, "/foo") |> to_string == "http://example.com/foo"
662+
assert URI.merge(base, "foo") |> to_string == "http://example.com/foo"
663+
end
658664
end
659665
end

0 commit comments

Comments
 (0)