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 IPAddr prefix information missing when write to cache in msgpack serializer #50742

Merged
merged 1 commit into from
Jan 15, 2024

Conversation

r-plus
Copy link
Contributor

@r-plus r-plus commented Jan 13, 2024

Motivation / Background

This Pull Request has been created because we notice the msgpack serializer missing the prefix(netmask) data of IPAddr object when dump.

Detail

This Pull Request fixed the missing prefix(netmask) data of IPAddr object when dumping by msgpack serializer.

Additional information

to_s is not contains prefix(netmask) data.

 IPAddr.new('192.168.0.1/24').to_s
=> "192.168.0.0"

So to_s missing the prefix(netmask) data when dump/load by msgpack serializer.

irb(main):032> serializer = ActiveSupport::MessagePack::CacheSerializer
irb(main):033> serializer.load(serializer.dump(IPAddr.new('192.168.0.1/24')))
=> #<IPAddr: IPv4:192.168.0.0/255.255.255.255>

IPAddr object assertion

IPAddr instance equal assertion is not accurate to assert same data. This is reason why I use inspect for test.

IPAddr.new('192.168.0.1/24') == IPAddr.new('192.168.0.0/32')
=> true
irb(main):027> IPAddr.new('192.168.0.1/24').inspect == IPAddr.new('192.168.0.0').inspect
=> false

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

Comment on lines +89 to +91
packer: method(:write_ipaddr),
unpacker: method(:read_ipaddr),
recursive: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that we can't just change the serialization of a type now that the first version was released, as previous payload wouldn't be readable, and old code wouldn't be able to read new payload (cc @jonathanhefner that's the kind of stuff I was worried about :/)

So now we need to define a new IPAddr type, and conditionally enable it, like we do in Paquito.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you are right. We should consider backward compatibility.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write the single string "#{ipaddr.to_s}/#{ipaddr.prefix}" as single packer write, and unpacker back to :new.
I think this way would keep the compatibility, but what do you think?

Copy link
Contributor Author

@r-plus r-plus Jan 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okey, did it without deprecation system.
Fortunately, IPAddr.new can be compatible with or without prefixes(netmask).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@byroot As @r-plus mentioned, a new type isn't necessary in this case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see it's not needed for backward compatibility, but what about forward compatibility? We unfortunately need both to allow for smooth upgrades.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, I see the unpack is unchanged, so we're good.

@r-plus r-plus force-pushed the fix/msgpack-cache-ipaddr branch 2 times, most recently from d0f1676 to f47f92b Compare January 14, 2024 10:45
Copy link
Member

@jonathanhefner jonathanhefner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the pull request!

The current behavior was derived from IPAddr#as_json, which also does not account for prefix:

class IPAddr # :nodoc:
def as_json(options = nil)
to_s
end
end

However, MessagePack can roundtrip IPAddr whereas JSON can't, so I think it's reasonable to support prefix. 👍

Also, would you mind adding a CHANGELOG entry?

@r-plus
Copy link
Contributor Author

r-plus commented Jan 15, 2024

@jonathanhefner
Thanks for your review, I taken the suggestions and add the CHANGELOG.

…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>
@jonathanhefner jonathanhefner merged commit 5034b08 into rails:main Jan 15, 2024
4 checks passed
@jonathanhefner
Copy link
Member

I pushed a commit to tweak the CHANGELOG entry a bit and to squash the commits.

Thank you, @r-plus! 📦

@r-plus r-plus deleted the fix/msgpack-cache-ipaddr branch January 15, 2024 06:58
r-plus added a commit to appbrew/cloudfront-rails that referenced this pull request May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants