mirror of https://github.com/rails/rails
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:
commit
5034b08890
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue