Skip to content

Commit 82fcdeb

Browse files
committed
Fix filepath with spaces not working by unescaping the file URI before
loading a file from disk
1 parent 66521ed commit 82fcdeb

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

jsonLoader.go

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"io"
3434
"io/ioutil"
3535
"net/http"
36+
"net/url"
3637
"os"
3738
"path/filepath"
3839
"runtime"
@@ -145,6 +146,12 @@ func (l *jsonReferenceLoader) LoadJSON() (interface{}, error) {
145146
if reference.HasFileScheme {
146147

147148
filename := strings.TrimPrefix(refToURL.String(), "file://")
149+
filename, err = url.QueryUnescape(filename)
150+
151+
if err != nil {
152+
return nil, err
153+
}
154+
148155
if runtime.GOOS == "windows" {
149156
// on Windows, a file URL may have an extra leading slash, use slashes
150157
// instead of backslashes, and have spaces escaped

schema_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,22 @@ func TestFragmentLoader(t *testing.T) {
272272
}
273273
}
274274

275+
func TestFileWithSpace(t *testing.T) {
276+
wd, err := os.Getwd()
277+
278+
if err != nil {
279+
panic(err.Error())
280+
}
281+
282+
fileName := filepath.Join(wd, "testdata", "extra", "file with space.json")
283+
loader := NewReferenceLoader("file://" + filepath.ToSlash(fileName))
284+
285+
json, err := loader.LoadJSON()
286+
287+
assert.Nil(t, err, "Unexpected error when trying to load a filepath containing a space")
288+
assert.Equal(t, map[string]interface{}{"foo": true}, json, "Contents of the file do not match")
289+
}
290+
275291
func TestAdditionalPropertiesErrorMessage(t *testing.T) {
276292
schema := `{
277293
"$schema": "http://json-schema.org/draft-07/schema#",

testdata/extra/file with space.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"foo": true}

0 commit comments

Comments
 (0)