Merge pull request #50742 from r-plus/fix/msgpack-cache-ipaddr

Fix IPAddr prefix information missing when write to cache in msgpack serializer
This commit is contained in:
Jonathan Hefner 2024-01-15 00:54:08 -06:00 committed by GitHub
commit 5034b08890
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 2 deletions

View File

@ -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

View File

@ -86,8 +86,9 @@ module ActiveSupport
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,
@ -221,6 +222,18 @@ module ActiveSupport
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

View File

@ -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