Skip to content

Commit 7fdbabe

Browse files
committed
preserve url.Parse behaviour before go1.19
Closes: #157 Signed-off-by: Shengjing Zhu <zhsj@debian.org>
1 parent 1005cfb commit 7fdbabe

6 files changed

Lines changed: 32 additions & 11 deletions

File tree

normalizer.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const fileScheme = "file"
4040
//
4141
// The base path argument is assumed to be canonicalized (e.g. using normalizeBase()).
4242
func normalizeURI(refPath, base string) string {
43-
refURL, err := url.Parse(refPath)
43+
refURL, err := parseURL(refPath)
4444
if err != nil {
4545
specLogger.Printf("warning: invalid URI in $ref %q: %v", refPath, err)
4646
refURL, refPath = repairURI(refPath)
@@ -58,7 +58,7 @@ func normalizeURI(refPath, base string) string {
5858
return refURL.String()
5959
}
6060

61-
baseURL, _ := url.Parse(base)
61+
baseURL, _ := parseURL(base)
6262
if path.IsAbs(refURL.Path) {
6363
baseURL.Path = refURL.Path
6464
} else if refURL.Path != "" {
@@ -84,7 +84,6 @@ func normalizeURI(refPath, base string) string {
8484
// There is a special case for schemas that are anchored with an "id":
8585
// in that case, the rebasing is performed // against the id only if this is an anchor for the initial root document.
8686
// All other intermediate "id"'s found along the way are ignored for the purpose of rebasing.
87-
//
8887
func denormalizeRef(ref *Ref, originalRelativeBase, id string) Ref {
8988
debugLog("denormalizeRef called:\n$ref: %q\noriginal: %s\nroot ID:%s", ref.String(), originalRelativeBase, id)
9089

@@ -94,7 +93,7 @@ func denormalizeRef(ref *Ref, originalRelativeBase, id string) Ref {
9493
}
9594

9695
if id != "" {
97-
idBaseURL, err := url.Parse(id)
96+
idBaseURL, err := parseURL(id)
9897
if err == nil { // if the schema id is not usable as a URI, ignore it
9998
if ref, ok := rebase(ref, idBaseURL, true); ok { // rebase, but keep references to root unchaged (do not want $ref: "")
10099
// $ref relative to the ID of the schema in the root document
@@ -103,7 +102,7 @@ func denormalizeRef(ref *Ref, originalRelativeBase, id string) Ref {
103102
}
104103
}
105104

106-
originalRelativeBaseURL, _ := url.Parse(originalRelativeBase)
105+
originalRelativeBaseURL, _ := parseURL(originalRelativeBase)
107106

108107
r, _ := rebase(ref, originalRelativeBaseURL, false)
109108

@@ -168,7 +167,7 @@ func normalizeRef(ref *Ref, relativeBase string) *Ref {
168167
//
169168
// See also: https://en.wikipedia.org/wiki/File_URI_scheme
170169
func normalizeBase(in string) string {
171-
u, err := url.Parse(in)
170+
u, err := parseURL(in)
172171
if err != nil {
173172
specLogger.Printf("warning: invalid URI in RelativeBase %q: %v", in, err)
174173
u, in = repairURI(in)

normalizer_nonwindows.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !windows
12
// +build !windows
23

34
// Copyright 2015 go-swagger maintainers
@@ -34,7 +35,7 @@ func absPath(in string) string {
3435
}
3536

3637
func repairURI(in string) (*url.URL, string) {
37-
u, _ := url.Parse("")
38+
u, _ := parseURL("")
3839
debugLog("repaired URI: original: %q, repaired: %q", in, "")
3940
return u, ""
4041
}

normalizer_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ func repairURI(in string) (*url.URL, string) {
6060
const prefix = fileScheme + "://"
6161
if !strings.HasPrefix(in, prefix) {
6262
// giving up: resolve to empty path
63-
u, _ := url.Parse("")
63+
u, _ := parseURL("")
6464

6565
return u, ""
6666
}
6767

6868
// attempt the repair, stripping the scheme should be sufficient
69-
u, _ := url.Parse(strings.TrimPrefix(in, prefix))
69+
u, _ := parseURL(strings.TrimPrefix(in, prefix))
7070
debugLog("repaired URI: original: %q, repaired: %q", in, u.String())
7171

7272
return u, u.String()

schema.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package spec
1717
import (
1818
"encoding/json"
1919
"fmt"
20-
"net/url"
2120
"strings"
2221

2322
"github.com/go-openapi/jsonpointer"
@@ -145,7 +144,7 @@ func (r *SchemaURL) fromMap(v map[string]interface{}) error {
145144
}
146145
if vv, ok := v["$schema"]; ok {
147146
if str, ok := vv.(string); ok {
148-
u, err := url.Parse(str)
147+
u, err := parseURL(str)
149148
if err != nil {
150149
return err
151150
}

url_go18.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build !go1.19
2+
// +build !go1.19
3+
4+
package spec
5+
6+
import "net/url"
7+
8+
var parseURL = url.Parse

url_go19.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//go:build go1.19
2+
// +build go1.19
3+
4+
package spec
5+
6+
import "net/url"
7+
8+
func parseURL(s string) (*url.URL, error) {
9+
u, err := url.Parse(s)
10+
if err == nil {
11+
u.OmitHost = false
12+
}
13+
return u, err
14+
}

0 commit comments

Comments
 (0)