diff --git a/jsonLoader.go b/jsonLoader.go index 4f57ff7..5d88af2 100644 --- a/jsonLoader.go +++ b/jsonLoader.go @@ -33,6 +33,7 @@ import ( "io" "io/ioutil" "net/http" + "net/url" "os" "path/filepath" "runtime" @@ -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 diff --git a/schema_test.go b/schema_test.go index 102e887..c05ddf0 100644 --- a/schema_test.go +++ b/schema_test.go @@ -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#", diff --git a/testdata/extra/file with space.json b/testdata/extra/file with space.json new file mode 100644 index 0000000..de33b4d --- /dev/null +++ b/testdata/extra/file with space.json @@ -0,0 +1 @@ +{"foo": true}