Skip to content

Commit 2adca8a

Browse files
authored
Merge pull request #122 from p1c2u/fix/file-handler-windows-compatibility-fix
File handler Windows compatibility fix
2 parents 05e6862 + 44cf7ef commit 2adca8a

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

.github/workflows/python-test.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ on:
1010

1111
jobs:
1212
test:
13-
runs-on: ubuntu-latest
13+
runs-on: ${{ matrix.os }}
1414
strategy:
1515
matrix:
1616
python-version: [2.7, 3.5, 3.6, 3.7, 3.8, 3.9]
17+
os: [windows-latest, ubuntu-latest]
1718
fail-fast: false
1819
steps:
1920
- uses: actions/checkout@v2
@@ -27,7 +28,9 @@ jobs:
2728
pip install -r requirements.txt
2829
pip install -r requirements_dev.txt
2930
pip install -e .
31+
shell: bash
3032
- name: Test
3133
run: python setup.py test
34+
shell: bash
3235
- name: Upload coverage
3336
uses: codecov/codecov-action@v1

openapi_spec_validator/handlers/file.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from yaml import load
44

55
from openapi_spec_validator.handlers.base import BaseHandler
6+
from openapi_spec_validator.handlers.utils import uri_to_path
67
from openapi_spec_validator.loaders import ExtendedSafeLoader
78

89

@@ -19,12 +20,12 @@ def __call__(self, f):
1920
class FileHandler(FileObjectHandler):
2021
"""OpenAPI spec validator file path handler."""
2122

22-
def __call__(self, f):
23-
if isinstance(f, StringIO):
24-
return super(FileHandler, self).__call__(f)
23+
def __call__(self, uri):
24+
if isinstance(uri, StringIO):
25+
return super(FileHandler, self).__call__(uri)
2526

26-
assert f.startswith("file")
27+
assert uri.startswith("file")
2728

28-
filename = f[7:]
29-
with open(filename) as fh:
29+
filepath = uri_to_path(uri)
30+
with open(filepath) as fh:
3031
return super(FileHandler, self).__call__(fh)
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import os.path
2+
3+
from six.moves.urllib.parse import urlparse, unquote
4+
from six.moves.urllib.request import url2pathname
5+
6+
7+
def uri_to_path(uri):
8+
parsed = urlparse(uri)
9+
host = "{0}{0}{mnt}{0}".format(os.path.sep, mnt=parsed.netloc)
10+
return os.path.normpath(
11+
os.path.join(host, url2pathname(unquote(parsed.path)))
12+
)

0 commit comments

Comments
 (0)