Skip to content

Commit

Permalink
Define URI::NONE as a module to avoid serialization issues
Browse files Browse the repository at this point in the history
If it's an anonymous objects, when serialized and restored by Marshal
or YAML, the restored instance has a diferent anonymous object as ivar values.

The simplest fix for this is to use a named module.
  • Loading branch information
byroot committed May 2, 2023
1 parent e91b64e commit 8d9350e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/addressable/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2569,7 +2569,7 @@ def reset_ivs
@query = nil
end

NONE = Object.new.freeze
NONE = Module.new.freeze

private_constant :NONE
end
Expand Down
31 changes: 31 additions & 0 deletions spec/addressable/uri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6768,6 +6768,37 @@ def to_str
end
end

describe Addressable::URI, "support serialization roundtrip" do
before do
@uri = Addressable::URI.new(
:scheme => "http",
:user => "user",
:password => "password",
:host => "example.com",
:port => 80,
:path => "/path",
:query => "query=value",
:fragment => "fragment"
)
end

it "is in a working state after being serialized with Marshal" do
@uri = Addressable::URI.parse("http://example.com")
cloned_uri = Marshal.load(Marshal.dump(@uri))
expect(cloned_uri.normalized_scheme).to be == @uri.normalized_scheme
end

it "is in a working state after being serialized with YAML" do
@uri = Addressable::URI.parse("http://example.com")
cloned_uri = if YAML.respond_to?(:unsafe_load)
YAML.unsafe_load(YAML.dump(@uri))
else
YAML.load(YAML.dump(@uri))
end
expect(cloned_uri.normalized_scheme).to be == @uri.normalized_scheme
end
end

describe Addressable::URI, "when initialized in a non-main `Ractor`" do
it "should have the same value as if used in the main `Ractor`" do
pending("Ruby 3.0+ for `Ractor` support") unless defined?(Ractor)
Expand Down

0 comments on commit 8d9350e

Please sign in to comment.