Skip to content

Commit

Permalink
Fix filepath with spaces not working by unescaping the file URI before
Browse files Browse the repository at this point in the history
loading a file from disk
  • Loading branch information
johandorland committed Oct 15, 2019
1 parent 66521ed commit 82fcdeb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions jsonLoader.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -145,6 +146,12 @@ func (l *jsonReferenceLoader) LoadJSON() (interface{}, error) {
if reference.HasFileScheme {

filename := strings.TrimPrefix(refToURL.String(), "file://")
filename, err = url.QueryUnescape(filename)

if err != nil {
return nil, err
}

if runtime.GOOS == "windows" {
// on Windows, a file URL may have an extra leading slash, use slashes
// instead of backslashes, and have spaces escaped
Expand Down
16 changes: 16 additions & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,22 @@ func TestFragmentLoader(t *testing.T) {
}
}

func TestFileWithSpace(t *testing.T) {
wd, err := os.Getwd()

if err != nil {
panic(err.Error())
}

fileName := filepath.Join(wd, "testdata", "extra", "file with space.json")
loader := NewReferenceLoader("file://" + filepath.ToSlash(fileName))

json, err := loader.LoadJSON()

assert.Nil(t, err, "Unexpected error when trying to load a filepath containing a space")
assert.Equal(t, map[string]interface{}{"foo": true}, json, "Contents of the file do not match")
}

func TestAdditionalPropertiesErrorMessage(t *testing.T) {
schema := `{
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down
1 change: 1 addition & 0 deletions testdata/extra/file with space.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"foo": true}

0 comments on commit 82fcdeb

Please sign in to comment.