Skip to content

Fix/normalize url parseable host - #116

Merged
fredbi merged 2 commits into
go-openapi:masterfrom
fredbi:fix/normalize-url-parseable-host
Aug 25, 2026
Merged

Fix/normalize url parseable host#116
fredbi merged 2 commits into
go-openapi:masterfrom
fredbi:fix/normalize-url-parseable-host

Conversation

@fredbi

@fredbi fredbi commented Aug 25, 2026

Copy link
Copy Markdown
Member

Change type

Please select: 🆕 New feature or enhancement|🔧 Bug fix'|📃 Documentation update

Short description

Fixes

Full description

Checklist

  • I have signed all my commits with my name and email (see DCO. This does not require a PGP-signed commit
  • I have rebased and squashed my work, so only one commit remains
  • I have added tests to cover my changes.
  • I have properly enriched go doc comments in code.
  • I have properly documented any breaking change.

fredbi and others added 2 commits August 25, 2026 11:12
NormalizeURL trimmed :80 or :443 off u.Host with the regexp `(:\d+)/?$`.
url.Parse accepts "https://:a:443" as the host ":a" on port 443, so the trim
left "https://:a", which url.Parse then rejects - ":a" is not a valid port.
go-openapi/spec hands Ref.String() straight back to MustCreateRef, which panics
on a URI that no longer parses.

removeDefaultPort now reads the port through u.Port(), parses the shortened
authority before keeping it, and repeats until nothing more comes off:
url.Parse reads "http://:80:80" as the host ":80" on port 80, so one pass left
a URL that normalized further on the next call. The regexp is gone with it -
u.Port() already reports the port url.Parse would use, and the trailing `/?`
never matched, u.Host holding no path.

FuzzNormalizeURL pins both properties, that the output parses and that
normalizing it again changes nothing. It found the "http://:80:80" case and an
earlier bracket-counting attempt at the first fix; both minimized inputs are
checked in under internal/testdata/fuzz.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
removeDuplicateSlashes ran regexp.ReplaceAllString with `/{2,}` over every
path. It now looks for the first "//" and returns the path untouched when there
is none, which is the common case and costs one scan and no allocation, then
copies the rest collapsing each run of slashes to one. Runs of any length
collapse, as they did before: "/a//b///c////d" becomes "/a/b/c/d".

BenchmarkNormalizeURL, checked in with this, over five URLs covering the
branches: 2222ns -> 751ns, 411 -> 208 B/op, 19 -> 5 allocs/op.

That was the last regexp in the package, so internal no longer imports regexp.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
@codecov

codecov Bot commented Aug 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.98%. Comparing base (edb350b) to head (6449126).
⚠️ Report is 9 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #116      +/-   ##
==========================================
+ Coverage   97.43%   98.98%   +1.55%     
==========================================
  Files           2        2              
  Lines          78       99      +21     
==========================================
+ Hits           76       98      +22     
  Misses          1        1              
+ Partials        1        0       -1     

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

@fredbi
fredbi merged commit 404eada into go-openapi:master Aug 25, 2026
23 checks passed
@fredbi
fredbi deleted the fix/normalize-url-parseable-host branch August 25, 2026 11:59
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.

1 participant