Fix/normalize url parseable host - #116
Merged
fredbi merged 2 commits intoAug 25, 2026
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change type
Please select: 🆕 New feature or enhancement|🔧 Bug fix'|📃 Documentation update
Short description
Fixes
Full description
Checklist