Skip to content

reflect: accept ipv4 and ipv6 in net.IP schema - #201

Closed
AdamMagued wants to merge 1 commit into
invopop:mainfrom
AdamMagued:fix-net-ip-schema
Closed

AdamMagued wants to merge 1 commit into
invopop:mainfrom
AdamMagued:fix-net-ip-schema

Conversation

@AdamMagued

Copy link
Copy Markdown

Fixes #198

Root Cause Analysis

Go's standard library net.IP.UnmarshalText unmarshals text representations of IP addresses by calling net.ParseIP, which accepts both IPv4 (dotted-decimal) and IPv6 addresses. However, invopop/jsonschema reflected net.IP exclusively into an IPv4 string schema:

if t == ipType {
    st.Type = "string"
    st.Format = "ipv4"
    return st
}

This caused valid IPv6 addresses accepted by net.IP in Go to fail validation against schemas generated by jsonschema.

Solution

Updated reflect.go to generate an anyOf schema for net.IP containing both ipv4 and ipv6 string format schemas:

{
  "anyOf": [
    {"type": "string", "format": "ipv4"},
    {"type": "string", "format": "ipv6"}
  ]
}

This accurately models the formats accepted by net.IP.UnmarshalText. Updated existing test fixtures and added regression unit tests in reflect_test.go verifying schema generation for struct fields of type net.IP and direct reflection of net.IP.

Verification

  • Ran go test -v -race ./...: 100% tests passed.
  • Ran go vet ./...: 0 warnings.
  • Ran git diff --check: 0 whitespace issues.

@AdamMagued

Copy link
Copy Markdown
Author

Closing in favor of earlier community PR #199 to keep the review queue clean.

@AdamMagued AdamMagued closed this Oct 2, 2026
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.

net.IP schema should accept ipv6 and ipv4

1 participant