Skip to content

Commit

Permalink
Define URI::NONE as a module to avoid serialization issues (#509)
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 different anonymous object as ivar values.

The simplest fix for this is to use a named module.

Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
  • Loading branch information
casperisfine and byroot committed May 19, 2023
1 parent 736c42a commit b56cef3
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
Expand Up @@ -2588,7 +2588,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
Expand Up @@ -6769,6 +6769,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 b56cef3

Please sign in to comment.