Skip to content

Commit

Permalink
Fix IPAddr prefix information missing when write to cache in msgpack …
Browse files Browse the repository at this point in the history
…serializer

* Save cache size by omit the prefix if unnecessary

* rename to straightforward naming.

* check the prefix directly instead of inspect

* Remove unused helper method

* add to changelog

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
  • Loading branch information
r-plus and jonathanhefner committed Jan 15, 2024
1 parent 0ecfbe2 commit 3815171
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
7 changes: 7 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,3 +1,10 @@
* Include `IPAddr#prefix` when serializing an `IPAddr` using the
`ActiveSupport::MessagePack` serializer. This change is backward and forward
compatible — old payloads can still be read, and new payloads will be
readable by older versions of Rails.

*Taiki Komaba*

* Add `default:` support for `ActiveSupport::CurrentAttributes.attribute`

```ruby
Expand Down
17 changes: 15 additions & 2 deletions activesupport/lib/active_support/message_pack/extensions.rb
Expand Up @@ -86,8 +86,9 @@ def install(registry)
unpacker: URI.method(:parse)

registry.register_type 14, IPAddr,
packer: :to_s,
unpacker: :new
packer: method(:write_ipaddr),
unpacker: method(:read_ipaddr),
recursive: true

registry.register_type 15, Pathname,
packer: :to_s,
Expand Down Expand Up @@ -221,6 +222,18 @@ def read_set(unpacker)
Set.new(unpacker.read)
end

def write_ipaddr(ipaddr, packer)
if ipaddr.prefix < 32 || (ipaddr.ipv6? && ipaddr.prefix < 128)
packer.write("#{ipaddr}/#{ipaddr.prefix}")
else
packer.write(ipaddr.to_s)
end
end

def read_ipaddr(unpacker)
IPAddr.new(unpacker.read)
end

def write_hash_with_indifferent_access(hwia, packer)
packer.write(hwia.to_h)
end
Expand Down
6 changes: 6 additions & 0 deletions activesupport/test/message_pack/shared_serializer_tests.rb
Expand Up @@ -127,6 +127,12 @@ module MessagePackSharedSerializerTests

test "roundtrips IPAddr" do
assert_roundtrip IPAddr.new("127.0.0.1")
assert_roundtrip IPAddr.new("1.1.1.1/16")
assert_equal 16, load(dump(IPAddr.new("1.1.1.1/16"))).prefix

assert_roundtrip IPAddr.new("::1")
assert_roundtrip IPAddr.new("1:1:1:1:1:1:1:1/64")
assert_equal 64, load(dump(IPAddr.new("1:1:1:1:1:1:1:1/64"))).prefix
end

test "roundtrips Pathname" do
Expand Down

0 comments on commit 3815171

Please sign in to comment.