Skip to content

Commit c90005f

Browse files
committed
correct tests breaking changes
1 parent e0c20e4 commit c90005f

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

openapi_python_client/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import shutil
44
import subprocess
55
import sys
6+
import urllib
67
from enum import Enum
78
from pathlib import Path
89
from typing import Any, Dict, Optional, Sequence, Union, cast
910

11+
import httpcore
12+
import httpx
1013
from jinja2 import BaseLoader, ChoiceLoader, Environment, FileSystemLoader, PackageLoader
1114

1215
from openapi_python_client import utils
@@ -297,6 +300,8 @@ def _get_document(*, url: Optional[str], path: Optional[Path]) -> Union[Dict[str
297300
result = resolver.resolve()
298301
if len(result.errors) > 0:
299302
return GeneratorError(header="; ".join(result.errors))
303+
except (httpx.HTTPError, httpcore.NetworkError, urllib.error.URLError):
304+
return GeneratorError(header="Could not get OpenAPI document from provided URL")
300305
except Exception:
301306
return GeneratorError(header="Invalid YAML from provided source")
302307

openapi_python_client/resolver/schema_resolver.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import urllib
33
from pathlib import Path
4-
from typing import Any, Dict, Generator, List, Union
4+
from typing import Any, Dict, Generator, List, Union, cast
55

66
import httpx
77

@@ -21,12 +21,20 @@ def __init__(self, url_or_path: Union[str, Path]):
2121
self._root_url: Union[str, None] = None
2222
self._root_url_scheme: Union[str, None] = None
2323

24-
if isinstance(url_or_path, Path):
24+
if self._isapath(url_or_path):
25+
url_or_path = cast(Path, url_or_path)
2526
self._root_path = url_or_path.absolute()
2627
self._root_path_dir = self._root_path.parent
2728
else:
29+
url_or_path = cast(str, url_or_path)
2830
self._root_url = url_or_path
29-
self._root_url_scheme = urllib.parse.urlparse(url_or_path).scheme
31+
try:
32+
self._root_url_scheme = urllib.parse.urlparse(url_or_path).scheme
33+
except Exception:
34+
raise urllib.error.URLError(f"Coult not parse URL > {url_or_path}")
35+
36+
def _isapath(self, url_or_path: Union[str, Path]) -> bool:
37+
return isinstance(url_or_path, Path)
3038

3139
def resolve(self, recursive: bool = True) -> ResolvedSchema:
3240
assert self._root_path or self._root_url
@@ -81,14 +89,14 @@ def _fetch_remote_reference(self, relative_path: str) -> SchemaData:
8189
raise RuntimeError("Bad object initilalization")
8290

8391
def _fetch_remote_file_path(self, path: Path) -> SchemaData:
84-
logging.info("Fetching remote ref file path > {0}".format(path))
92+
logging.info(f"Fetching remote ref file path > {path}")
8593
return DataLoader.load(str(path), path.read_bytes())
8694

8795
def _fetch_url_reference(self, url: str) -> SchemaData:
8896
if url.startswith("//", 0):
8997
url = "{0}:{1}".format(self._root_url_scheme, url)
9098

91-
logging.info("Fetching remote ref url > {0}".format(url))
99+
logging.info(f"Fetching remote ref url > {url}")
92100
return DataLoader.load(url, httpx.get(url).content)
93101

94102
def _lookup_schema_references(self, attr: Any) -> Generator[Reference, None, None]:

tests/test___init__.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pathlib
2+
from urllib.parse import ParseResult
23

34
import httpcore
45
import jinja2
@@ -167,7 +168,7 @@ def test__get_document_url_and_path(self, mocker):
167168
loads.assert_not_called()
168169

169170
def test__get_document_bad_url(self, mocker):
170-
get = mocker.patch("httpx.get", side_effect=httpcore.NetworkError)
171+
get = mocker.patch("httpx.get")
171172
Path = mocker.patch("openapi_python_client.Path")
172173
loads = mocker.patch("yaml.safe_load")
173174

@@ -177,7 +178,7 @@ def test__get_document_bad_url(self, mocker):
177178
result = _get_document(url=url, path=None)
178179

179180
assert result == GeneratorError(header="Could not get OpenAPI document from provided URL")
180-
get.assert_called_once_with(url)
181+
get.assert_not_called()
181182
Path.assert_not_called()
182183
loads.assert_not_called()
183184

@@ -188,7 +189,7 @@ def test__get_document_url_no_path(self, mocker):
188189

189190
from openapi_python_client import _get_document
190191

191-
url = mocker.MagicMock()
192+
url = "http://localhost/"
192193
_get_document(url=url, path=None)
193194

194195
get.assert_called_once_with(url)
@@ -198,28 +199,30 @@ def test__get_document_url_no_path(self, mocker):
198199
def test__get_document_path_no_url(self, mocker):
199200
get = mocker.patch("httpx.get")
200201
loads = mocker.patch("yaml.safe_load")
202+
mocker.patch("openapi_python_client.resolver.schema_resolver.SchemaResolver._isapath", return_value=True)
201203

202204
from openapi_python_client import _get_document
203205

204206
path = mocker.MagicMock()
205207
_get_document(url=None, path=path)
206208

207209
get.assert_not_called()
208-
path.read_bytes.assert_called_once()
209-
loads.assert_called_once_with(path.read_bytes())
210+
path.absolute().read_bytes.assert_called_once()
211+
loads.assert_called_once_with(path.absolute().read_bytes())
210212

211213
def test__get_document_bad_yaml(self, mocker):
212214
get = mocker.patch("httpx.get")
213215
loads = mocker.patch("yaml.safe_load", side_effect=yaml.YAMLError)
216+
mocker.patch("openapi_python_client.resolver.schema_resolver.SchemaResolver._isapath", return_value=True)
214217

215218
from openapi_python_client import _get_document
216219

217220
path = mocker.MagicMock()
218221
result = _get_document(url=None, path=path)
219222

220223
get.assert_not_called()
221-
path.read_bytes.assert_called_once()
222-
loads.assert_called_once_with(path.read_bytes())
224+
path.absolute().read_bytes.assert_called_once()
225+
loads.assert_called_once_with(path.absolute().read_bytes())
223226
assert result == GeneratorError(header="Invalid YAML from provided source")
224227

225228

0 commit comments

Comments
 (0)