Skip to content

Commit 8c44367

Browse files
committed
Add support for YAML documents (#111)
1 parent 14bf2cc commit 8c44367

File tree

4 files changed

+26
-26
lines changed

4 files changed

+26
-26
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
with the pieces it can.
1616
- Support for Enums declared in "components/schemas" and references to them (#102).
1717
- Generated clients can now be installed via pip (#120).
18+
- Support for YAML OpenAPI documents (#111)
1819

1920
### Internal Changes
2021
- Switched OpenAPI document parsing to use Pydantic based on a vendored version of

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# openapi-python-client
1010
Generate modern Python clients from OpenAPI
1111

12-
**This project is still in early development and does not support all OpenAPI features**
12+
**This project is still in development and does not support all OpenAPI features**
1313

1414
## Why This?
1515
The Python clients generated by openapi-generator support Python 2 and therefore come with a lot of baggage. This tool

openapi_python_client/__init__.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
""" Generate modern Python clients from OpenAPI """
22
from __future__ import annotations
33

4-
import json
54
import shutil
65
import subprocess
76
import sys
@@ -27,7 +26,7 @@
2726

2827

2928
def _get_project_for_url_or_path(url: Optional[str], path: Optional[Path]) -> _Project:
30-
data_dict = _get_json(url=url, path=path)
29+
data_dict = _get_document(url=url, path=path)
3130
openapi = GeneratorData.from_dict(data_dict)
3231
return _Project(openapi=openapi)
3332

@@ -65,18 +64,18 @@ def update_existing_client(*, url: Optional[str], path: Optional[Path]) -> Seque
6564
return project.update()
6665

6766

68-
def _get_json(*, url: Optional[str], path: Optional[Path]) -> Dict[str, Any]:
69-
json_bytes: bytes
67+
def _get_document(*, url: Optional[str], path: Optional[Path]) -> Dict[str, Any]:
68+
yaml_bytes: bytes
7069
if url is not None and path is not None:
7170
raise ValueError("Provide URL or Path, not both.")
7271
if url is not None:
7372
response = httpx.get(url)
74-
json_bytes = response.content
73+
yaml_bytes = response.content
7574
elif path is not None:
76-
json_bytes = path.read_bytes()
75+
yaml_bytes = path.read_bytes()
7776
else:
7877
raise ValueError("No URL or Path provided")
79-
return json.loads(json_bytes)
78+
return yaml.safe_load(yaml_bytes)
8079

8180

8281
class _Project:

tests/test___init__.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
def test__get_project_for_url_or_path(mocker):
1010
data_dict = mocker.MagicMock()
11-
_get_json = mocker.patch("openapi_python_client._get_json", return_value=data_dict)
11+
_get_document = mocker.patch("openapi_python_client._get_document", return_value=data_dict)
1212
openapi = mocker.MagicMock()
1313
from_dict = mocker.patch("openapi_python_client.parser.GeneratorData.from_dict", return_value=openapi)
1414
_Project = mocker.patch("openapi_python_client._Project")
@@ -19,7 +19,7 @@ def test__get_project_for_url_or_path(mocker):
1919

2020
project = _get_project_for_url_or_path(url=url, path=path)
2121

22-
_get_json.assert_called_once_with(url=url, path=path)
22+
_get_document.assert_called_once_with(url=url, path=path)
2323
from_dict.assert_called_once_with(data_dict)
2424
_Project.assert_called_once_with(openapi=openapi)
2525
assert project == _Project()
@@ -58,56 +58,56 @@ def test_update_existing_client(mocker):
5858

5959

6060
class TestGetJson:
61-
def test__get_json_no_url_or_path(self, mocker):
61+
def test__get_document_no_url_or_path(self, mocker):
6262
get = mocker.patch("httpx.get")
6363
Path = mocker.patch("openapi_python_client.Path")
64-
loads = mocker.patch("json.loads")
64+
loads = mocker.patch("yaml.safe_load")
6565

66-
from openapi_python_client import _get_json
66+
from openapi_python_client import _get_document
6767

6868
with pytest.raises(ValueError):
69-
_get_json(url=None, path=None)
69+
_get_document(url=None, path=None)
7070

7171
get.assert_not_called()
7272
Path.assert_not_called()
7373
loads.assert_not_called()
7474

75-
def test__get_json_url_and_path(self, mocker):
75+
def test__get_document_url_and_path(self, mocker):
7676
get = mocker.patch("httpx.get")
7777
Path = mocker.patch("openapi_python_client.Path")
78-
loads = mocker.patch("json.loads")
78+
loads = mocker.patch("yaml.safe_load")
7979

80-
from openapi_python_client import _get_json
80+
from openapi_python_client import _get_document
8181

8282
with pytest.raises(ValueError):
83-
_get_json(url=mocker.MagicMock(), path=mocker.MagicMock())
83+
_get_document(url=mocker.MagicMock(), path=mocker.MagicMock())
8484

8585
get.assert_not_called()
8686
Path.assert_not_called()
8787
loads.assert_not_called()
8888

89-
def test__get_json_url_no_path(self, mocker):
89+
def test__get_document_url_no_path(self, mocker):
9090
get = mocker.patch("httpx.get")
9191
Path = mocker.patch("openapi_python_client.Path")
92-
loads = mocker.patch("json.loads")
92+
loads = mocker.patch("yaml.safe_load")
9393

94-
from openapi_python_client import _get_json
94+
from openapi_python_client import _get_document
9595

9696
url = mocker.MagicMock()
97-
_get_json(url=url, path=None)
97+
_get_document(url=url, path=None)
9898

9999
get.assert_called_once_with(url)
100100
Path.assert_not_called()
101101
loads.assert_called_once_with(get().content)
102102

103-
def test__get_json_path_no_url(self, mocker):
103+
def test__get_document_path_no_url(self, mocker):
104104
get = mocker.patch("httpx.get")
105-
loads = mocker.patch("json.loads")
105+
loads = mocker.patch("yaml.safe_load")
106106

107-
from openapi_python_client import _get_json
107+
from openapi_python_client import _get_document
108108

109109
path = mocker.MagicMock()
110-
_get_json(url=None, path=path)
110+
_get_document(url=None, path=path)
111111

112112
get.assert_not_called()
113113
path.read_bytes.assert_called_once()

0 commit comments

Comments
 (0)