这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@costela
Copy link
Contributor

@costela costela commented Apr 8, 2025

netip.Addr supports parsing both ipv4 and ipv6 out of the box. So it is a bit surprising to have a netip.Addr field in an API only support ipv4 and no easy way to add "dual-stack" support for APIs.

I propose to extend the support for netip.Addr added in #396 and support ipv6 out of the box as well.

⚠️ The change in the format detection for netip.Addr fields may be considered breaking, since existing APIs will start accepting IPv6 addresses and people may be counting on that limitation. I'd argue that it's worth it, but if not, we can remove that single change, while still adding support for the ip dual-stack format, to be used explicitly.

WDYT?

Copilot AI review requested due to automatic review settings April 8, 2025 15:50
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

@codecov
Copy link

codecov bot commented Apr 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.56%. Comparing base (4fd3cdb) to head (b8026a4).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #792   +/-   ##
=======================================
  Coverage   92.56%   92.56%           
=======================================
  Files          23       23           
  Lines        5511     5515    +4     
=======================================
+ Hits         5101     5105    +4     
  Misses        351      351           
  Partials       59       59           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@costela
Copy link
Contributor Author

costela commented Apr 13, 2025

Taking a second look at this, it appears it is possible to support dual-stack input by setting format:"ipv6", because IP.To16 also works for ipv4.

That doesn't seem very intuitive and could maybe warrant a doc fix?

Personally I'd try making this a bit more explicit: the ipv4/ipv6 formats should really be specific to their respective IP versions, and a new ip format should support both. WDYT?

Also, it may be a good idea to switch to netip for all validations regardless, just for the micro-optimization:

pkg: github.com/danielgtaylor/huma/v2
cpu: 12th Gen Intel(R) Core(TM) i7-1250U
BenchmarkNetParseIP-12               31314682                32.42 ns/op           16 B/op          1 allocs/op
BenchmarkNetipParseAddr-12           72398744                15.65 ns/op            0 B/op          0 allocs/op

Comment on lines +239 to +254
case "ip":
if _, err := netip.ParseAddr(str); err != nil {
res.Add(path, str, validation.MsgExpectedRFCIPAddr)
}
Copy link
Owner

@danielgtaylor danielgtaylor Apr 22, 2025

Choose a reason for hiding this comment

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

@costela I love this! One concern I have is that ipv4 and ipv6 are actually in the JSON Schema spec but ip is not. See https://json-schema.org/understanding-json-schema/reference/type#ip-addresses.

I wonder if we should support all three? The two standard ones, and then ip for either one since Go supports it well. What do you think?

edit: just saw your other comment which is similar to mine, sorry 😂

@costela costela force-pushed the add-support-for-dualstack-ip-parsing branch from 896acfb to b8026a4 Compare August 29, 2025 08:00
@thushjandan
Copy link

Would be awesome if this PR could be merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants