From b29ec780540ff5f5c4eb1355ba34d70a15e05d57 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 7 Sep 2026 15:09:00 +1200 Subject: [PATCH] Always return query parameters Assisted-By: devx/7ef83274-928f-499d-b668-48449b1ff60e --- lib/protocol/url/reference.rb | 4 +++- test/protocol/url/reference.rb | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/protocol/url/reference.rb b/lib/protocol/url/reference.rb index b8f94b4..b0dbee6 100644 --- a/lib/protocol/url/reference.rb +++ b/lib/protocol/url/reference.rb @@ -132,6 +132,8 @@ def parameters? # # Afterwards, the `query` attribute will be cleared. # + # If there is no query string or existing parameters, an empty mutable hash is created. + # # @returns [Hash] The merged parameters. def parse_query!(encoding = Encoding) if @query and !@query.empty? @@ -146,7 +148,7 @@ def parse_query!(encoding = Encoding) @query = nil end - return @parameters + return @parameters ||= {} end # @returns [Boolean] Whether the reference has a query string. diff --git a/test/protocol/url/reference.rb b/test/protocol/url/reference.rb index 7ec71af..9370b41 100644 --- a/test/protocol/url/reference.rb +++ b/test/protocol/url/reference.rb @@ -598,6 +598,16 @@ expect(reference.parameters).to be == {"foo" => "bar"} end + it "creates mutable parameters when there is no query string" do + reference = subject.parse("/path") + + parameters = reference.parse_query! + parameters["foo"] = "bar" + + expect(reference.parameters).to be == {"foo" => "bar"} + expect(reference.to_s).to be == "/path?foo=bar" + end + it "updates to_s output after parsing" do reference = subject.parse("/path?foo=bar&baz=qux") expect(reference.to_s).to be == "/path?foo=bar&baz=qux"