From 7fdbabec811e05073163ae13a0ea962c9ac149a3 Mon Sep 17 00:00:00 2001 From: Shengjing Zhu Date: Fri, 12 Aug 2022 20:18:32 +0800 Subject: [PATCH] preserve url.Parse behaviour before go1.19 Closes: #157 Signed-off-by: Shengjing Zhu --- normalizer.go | 11 +++++------ normalizer_nonwindows.go | 3 ++- normalizer_windows.go | 4 ++-- schema.go | 3 +-- url_go18.go | 8 ++++++++ url_go19.go | 14 ++++++++++++++ 6 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 url_go18.go create mode 100644 url_go19.go diff --git a/normalizer.go b/normalizer.go index d6c4839..e8b6009 100644 --- a/normalizer.go +++ b/normalizer.go @@ -40,7 +40,7 @@ const fileScheme = "file" // // The base path argument is assumed to be canonicalized (e.g. using normalizeBase()). func normalizeURI(refPath, base string) string { - refURL, err := url.Parse(refPath) + refURL, err := parseURL(refPath) if err != nil { specLogger.Printf("warning: invalid URI in $ref %q: %v", refPath, err) refURL, refPath = repairURI(refPath) @@ -58,7 +58,7 @@ func normalizeURI(refPath, base string) string { return refURL.String() } - baseURL, _ := url.Parse(base) + baseURL, _ := parseURL(base) if path.IsAbs(refURL.Path) { baseURL.Path = refURL.Path } else if refURL.Path != "" { @@ -84,7 +84,6 @@ func normalizeURI(refPath, base string) string { // There is a special case for schemas that are anchored with an "id": // in that case, the rebasing is performed // against the id only if this is an anchor for the initial root document. // All other intermediate "id"'s found along the way are ignored for the purpose of rebasing. -// func denormalizeRef(ref *Ref, originalRelativeBase, id string) Ref { debugLog("denormalizeRef called:\n$ref: %q\noriginal: %s\nroot ID:%s", ref.String(), originalRelativeBase, id) @@ -94,7 +93,7 @@ func denormalizeRef(ref *Ref, originalRelativeBase, id string) Ref { } if id != "" { - idBaseURL, err := url.Parse(id) + idBaseURL, err := parseURL(id) if err == nil { // if the schema id is not usable as a URI, ignore it if ref, ok := rebase(ref, idBaseURL, true); ok { // rebase, but keep references to root unchaged (do not want $ref: "") // $ref relative to the ID of the schema in the root document @@ -103,7 +102,7 @@ func denormalizeRef(ref *Ref, originalRelativeBase, id string) Ref { } } - originalRelativeBaseURL, _ := url.Parse(originalRelativeBase) + originalRelativeBaseURL, _ := parseURL(originalRelativeBase) r, _ := rebase(ref, originalRelativeBaseURL, false) @@ -168,7 +167,7 @@ func normalizeRef(ref *Ref, relativeBase string) *Ref { // // See also: https://en.wikipedia.org/wiki/File_URI_scheme func normalizeBase(in string) string { - u, err := url.Parse(in) + u, err := parseURL(in) if err != nil { specLogger.Printf("warning: invalid URI in RelativeBase %q: %v", in, err) u, in = repairURI(in) diff --git a/normalizer_nonwindows.go b/normalizer_nonwindows.go index c8a0645..2df0723 100644 --- a/normalizer_nonwindows.go +++ b/normalizer_nonwindows.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows // Copyright 2015 go-swagger maintainers @@ -34,7 +35,7 @@ func absPath(in string) string { } func repairURI(in string) (*url.URL, string) { - u, _ := url.Parse("") + u, _ := parseURL("") debugLog("repaired URI: original: %q, repaired: %q", in, "") return u, "" } diff --git a/normalizer_windows.go b/normalizer_windows.go index fe2d1ec..a66c532 100644 --- a/normalizer_windows.go +++ b/normalizer_windows.go @@ -60,13 +60,13 @@ func repairURI(in string) (*url.URL, string) { const prefix = fileScheme + "://" if !strings.HasPrefix(in, prefix) { // giving up: resolve to empty path - u, _ := url.Parse("") + u, _ := parseURL("") return u, "" } // attempt the repair, stripping the scheme should be sufficient - u, _ := url.Parse(strings.TrimPrefix(in, prefix)) + u, _ := parseURL(strings.TrimPrefix(in, prefix)) debugLog("repaired URI: original: %q, repaired: %q", in, u.String()) return u, u.String() diff --git a/schema.go b/schema.go index a8d0f73..4e9be85 100644 --- a/schema.go +++ b/schema.go @@ -17,7 +17,6 @@ package spec import ( "encoding/json" "fmt" - "net/url" "strings" "github.com/go-openapi/jsonpointer" @@ -145,7 +144,7 @@ func (r *SchemaURL) fromMap(v map[string]interface{}) error { } if vv, ok := v["$schema"]; ok { if str, ok := vv.(string); ok { - u, err := url.Parse(str) + u, err := parseURL(str) if err != nil { return err } diff --git a/url_go18.go b/url_go18.go new file mode 100644 index 0000000..60b7851 --- /dev/null +++ b/url_go18.go @@ -0,0 +1,8 @@ +//go:build !go1.19 +// +build !go1.19 + +package spec + +import "net/url" + +var parseURL = url.Parse diff --git a/url_go19.go b/url_go19.go new file mode 100644 index 0000000..392e3e6 --- /dev/null +++ b/url_go19.go @@ -0,0 +1,14 @@ +//go:build go1.19 +// +build go1.19 + +package spec + +import "net/url" + +func parseURL(s string) (*url.URL, error) { + u, err := url.Parse(s) + if err == nil { + u.OmitHost = false + } + return u, err +}