Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix YAML serialization #508

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/addressable/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2396,6 +2396,25 @@ def defer_validation
@validation_deferred = false
end

def encode_with(coder)
instance_variables.each do |ivar|
value = instance_variable_get(ivar)
if value != NONE
key = ivar.to_s.slice(1..-1)
coder[key] = value
end
end
nil
end

def init_with(coder)
reset_ivs
coder.map.each do |key, value|
instance_variable_set("@#{key}", value)
end
nil
end

protected
SELF_REF = '.'
PARENT = '..'
Expand Down
8 changes: 8 additions & 0 deletions spec/addressable/uri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
require "addressable/uri"
require "uri"
require "ipaddr"
require "yaml"

if !"".respond_to?("force_encoding")
class String
Expand Down Expand Up @@ -6799,3 +6800,10 @@ def to_str
expect(res).to be nil
end
end

describe Addressable::URI, "YAML safe loading" do
it "doesn't serialize anonymous objects" do
url = Addressable::URI.parse("http://example.com/")
expect(YAML.dump(url)).to_not include("!ruby/object {}")
end
end