Skip to content

Commit d63e579

Browse files
fix: Modify the parameter name encoding to file_encoding, and set the default is UTF-8
1 parent d162058 commit d63e579

File tree

4 files changed

+61
-33
lines changed

4 files changed

+61
-33
lines changed

openapi_python_client/__init__.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ class Project:
4444
project_name_override: Optional[str] = None
4545
package_name_override: Optional[str] = None
4646
package_version_override: Optional[str] = None
47-
encoding: Optional[str] = None
47+
file_encoding: str
4848

4949
def __init__(
5050
self,
5151
*,
5252
openapi: GeneratorData,
5353
meta: MetaType,
5454
custom_template_path: Optional[Path] = None,
55-
encoding: Optional[str] = None,
55+
file_encoding: str = "utf-8",
5656
) -> None:
5757
self.openapi: GeneratorData = openapi
5858
self.meta: MetaType = meta
59-
self.encoding = encoding
59+
self.encoding = file_encoding
6060

6161
package_loader = PackageLoader(__package__)
6262
loader: BaseLoader
@@ -264,15 +264,15 @@ def _get_project_for_url_or_path(
264264
path: Optional[Path],
265265
meta: MetaType,
266266
custom_template_path: Optional[Path] = None,
267-
encoding: Optional[str] = None,
267+
file_encoding: str = "utf-8",
268268
) -> Union[Project, GeneratorError]:
269269
data_dict = _get_document(url=url, path=path)
270270
if isinstance(data_dict, GeneratorError):
271271
return data_dict
272272
openapi = GeneratorData.from_dict(data_dict)
273273
if isinstance(openapi, GeneratorError):
274274
return openapi
275-
return Project(openapi=openapi, custom_template_path=custom_template_path, meta=meta, encoding=encoding)
275+
return Project(openapi=openapi, custom_template_path=custom_template_path, meta=meta, file_encoding=file_encoding)
276276

277277

278278
def create_new_client(
@@ -281,7 +281,7 @@ def create_new_client(
281281
path: Optional[Path],
282282
meta: MetaType,
283283
custom_template_path: Optional[Path] = None,
284-
encoding: Optional[str] = None,
284+
file_encoding: str = "utf-8",
285285
) -> Sequence[GeneratorError]:
286286
"""
287287
Generate the client library
@@ -290,23 +290,30 @@ def create_new_client(
290290
A list containing any errors encountered when generating.
291291
"""
292292
project = _get_project_for_url_or_path(
293-
url=url, path=path, custom_template_path=custom_template_path, meta=meta, encoding=encoding
293+
url=url, path=path, custom_template_path=custom_template_path, meta=meta, file_encoding=file_encoding
294294
)
295295
if isinstance(project, GeneratorError):
296296
return [project]
297297
return project.build()
298298

299299

300300
def update_existing_client(
301-
*, url: Optional[str], path: Optional[Path], meta: MetaType, custom_template_path: Optional[Path] = None
301+
*,
302+
url: Optional[str],
303+
path: Optional[Path],
304+
meta: MetaType,
305+
custom_template_path: Optional[Path] = None,
306+
file_encoding: str = "utf-8",
302307
) -> Sequence[GeneratorError]:
303308
"""
304309
Update an existing client library
305310
306311
Returns:
307312
A list containing any errors encountered when generating.
308313
"""
309-
project = _get_project_for_url_or_path(url=url, path=path, custom_template_path=custom_template_path, meta=meta)
314+
project = _get_project_for_url_or_path(
315+
url=url, path=path, custom_template_path=custom_template_path, meta=meta, file_encoding=file_encoding
316+
)
310317
if isinstance(project, GeneratorError):
311318
return [project]
312319
return project.update()

openapi_python_client/cli.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import codecs
12
import pathlib
23
from pprint import pformat
34
from typing import Optional, Sequence
@@ -116,7 +117,7 @@ def generate(
116117
url: Optional[str] = typer.Option(None, help="A URL to read the JSON from"),
117118
path: Optional[pathlib.Path] = typer.Option(None, help="A path to the JSON file"),
118119
custom_template_path: Optional[pathlib.Path] = typer.Option(None, **custom_template_path_options), # type: ignore
119-
encoding: Optional[str] = typer.Option(None, help="Set file encoding for client files"), # type: ignore
120+
file_encoding: str = typer.Option("utf-8", help="Encoding used when writing generated"),
120121
meta: MetaType = _meta_option,
121122
) -> None:
122123
""" Generate a new OpenAPI Client library """
@@ -128,8 +129,15 @@ def generate(
128129
if url and path:
129130
typer.secho("Provide either --url or --path, not both", fg=typer.colors.RED)
130131
raise typer.Exit(code=1)
132+
133+
try:
134+
codecs.getencoder(file_encoding)
135+
except LookupError:
136+
typer.secho("Unknown encoding : {}".format(file_encoding), fg=typer.colors.RED)
137+
raise typer.Exit(code=1)
138+
131139
errors = create_new_client(
132-
url=url, path=path, meta=meta, custom_template_path=custom_template_path, encoding=encoding
140+
url=url, path=path, meta=meta, custom_template_path=custom_template_path, file_encoding=file_encoding
133141
)
134142
handle_errors(errors)
135143

@@ -140,6 +148,7 @@ def update(
140148
path: Optional[pathlib.Path] = typer.Option(None, help="A path to the JSON file"),
141149
custom_template_path: Optional[pathlib.Path] = typer.Option(None, **custom_template_path_options), # type: ignore
142150
meta: MetaType = _meta_option,
151+
file_encoding: str = typer.Option("utf-8", help="Encoding used when writing generated"),
143152
) -> None:
144153
""" Update an existing OpenAPI Client library """
145154
from . import update_existing_client
@@ -151,5 +160,13 @@ def update(
151160
typer.secho("Provide either --url or --path, not both", fg=typer.colors.RED)
152161
raise typer.Exit(code=1)
153162

154-
errors = update_existing_client(url=url, path=path, meta=meta, custom_template_path=custom_template_path)
163+
try:
164+
codecs.getencoder(file_encoding)
165+
except LookupError:
166+
typer.secho("Unknown encoding : {}".format(file_encoding), fg=typer.colors.RED)
167+
raise typer.Exit(code=1)
168+
169+
errors = update_existing_client(
170+
url=url, path=path, meta=meta, custom_template_path=custom_template_path, file_encoding=file_encoding
171+
)
155172
handle_errors(errors)

tests/test___init__.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ def test__get_project_for_url_or_path(mocker):
2323

2424
_get_document.assert_called_once_with(url=url, path=path)
2525
from_dict.assert_called_once_with(data_dict)
26-
_Project.assert_called_once_with(openapi=openapi, custom_template_path=None, meta=MetaType.POETRY, encoding=None)
26+
_Project.assert_called_once_with(
27+
openapi=openapi, custom_template_path=None, meta=MetaType.POETRY, file_encoding="utf-8"
28+
)
2729
assert project == _Project.return_value
2830

2931

@@ -76,7 +78,7 @@ def test_create_new_client(mocker):
7678
result = create_new_client(url=url, path=path, meta=MetaType.POETRY)
7779

7880
_get_project_for_url_or_path.assert_called_once_with(
79-
url=url, path=path, custom_template_path=None, meta=MetaType.POETRY, encoding=None
81+
url=url, path=path, custom_template_path=None, meta=MetaType.POETRY, file_encoding="utf-8"
8082
)
8183
project.build.assert_called_once()
8284
assert result == project.build.return_value
@@ -95,7 +97,7 @@ def test_create_new_client_project_error(mocker):
9597
result = create_new_client(url=url, path=path, meta=MetaType.POETRY)
9698

9799
_get_project_for_url_or_path.assert_called_once_with(
98-
url=url, path=path, custom_template_path=None, meta=MetaType.POETRY, encoding=None
100+
url=url, path=path, custom_template_path=None, meta=MetaType.POETRY, file_encoding="utf-8"
99101
)
100102
assert result == [error]
101103

@@ -113,7 +115,7 @@ def test_update_existing_client(mocker):
113115
result = update_existing_client(url=url, path=path, meta=MetaType.POETRY)
114116

115117
_get_project_for_url_or_path.assert_called_once_with(
116-
url=url, path=path, custom_template_path=None, meta=MetaType.POETRY
118+
url=url, path=path, custom_template_path=None, meta=MetaType.POETRY, file_encoding="utf-8"
117119
)
118120
project.update.assert_called_once()
119121
assert result == project.update.return_value
@@ -132,7 +134,7 @@ def test_update_existing_client_project_error(mocker):
132134
result = update_existing_client(url=url, path=path, meta=MetaType.POETRY)
133135

134136
_get_project_for_url_or_path.assert_called_once_with(
135-
url=url, path=path, custom_template_path=None, meta=MetaType.POETRY
137+
url=url, path=path, custom_template_path=None, meta=MetaType.POETRY, file_encoding="utf-8"
136138
)
137139
assert result == [error]
138140

@@ -392,9 +394,9 @@ def test__build_metadata_poetry(self, mocker):
392394
project_name=project.project_name,
393395
package_name=project.package_name,
394396
)
395-
readme_path.write_text.assert_called_once_with(readme_template.render(), encoding=None)
397+
readme_path.write_text.assert_called_once_with(readme_template.render(), encoding="utf-8")
396398
git_ignore_template.render.assert_called_once()
397-
git_ignore_path.write_text.assert_called_once_with(git_ignore_template.render(), encoding=None)
399+
git_ignore_path.write_text.assert_called_once_with(git_ignore_template.render(), encoding="utf-8")
398400
project._build_pyproject_toml.assert_called_once_with(use_poetry=True)
399401

400402
def test__build_metadata_setup(self, mocker):
@@ -429,9 +431,9 @@ def test__build_metadata_setup(self, mocker):
429431
project_name=project.project_name,
430432
package_name=project.package_name,
431433
)
432-
readme_path.write_text.assert_called_once_with(readme_template.render(), encoding=None)
434+
readme_path.write_text.assert_called_once_with(readme_template.render(), encoding="utf-8")
433435
git_ignore_template.render.assert_called_once()
434-
git_ignore_path.write_text.assert_called_once_with(git_ignore_template.render(), encoding=None)
436+
git_ignore_path.write_text.assert_called_once_with(git_ignore_template.render(), encoding="utf-8")
435437
project._build_pyproject_toml.assert_called_once_with(use_poetry=False)
436438
project._build_setup_py.assert_called_once()
437439

@@ -475,7 +477,7 @@ def test__build_pyproject_toml(self, mocker, use_poetry):
475477
version=project.version,
476478
description=project.package_description,
477479
)
478-
pyproject_path.write_text.assert_called_once_with(pyproject_template.render(), encoding=None)
480+
pyproject_path.write_text.assert_called_once_with(pyproject_template.render(), encoding="utf-8")
479481

480482
def test__build_setup_py(self, mocker):
481483
from openapi_python_client import MetaType, Project
@@ -505,7 +507,7 @@ def test__build_setup_py(self, mocker):
505507
version=project.version,
506508
description=project.package_description,
507509
)
508-
setup_path.write_text.assert_called_once_with(setup_template.render(), encoding=None)
510+
setup_path.write_text.assert_called_once_with(setup_template.render(), encoding="utf-8")
509511

510512

511513
def test__reformat(mocker):

tests/test_cli.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,18 @@ def test_config_arg(mocker, _create_new_client):
3131

3232
config_path = "config/path"
3333
path = "cool/path"
34-
encoding = "utf-8"
34+
file_encoding = "utf-8"
3535

3636
result = runner.invoke(
37-
app, [f"--config={config_path}", "generate", f"--path={path}", f"--encoding={encoding}"], catch_exceptions=False
37+
app,
38+
[f"--config={config_path}", "generate", f"--path={path}", f"--file-encoding={file_encoding}"],
39+
catch_exceptions=False,
3840
)
3941

4042
assert result.exit_code == 0
4143
load_config.assert_called_once_with(path=Path(config_path))
4244
_create_new_client.assert_called_once_with(
43-
url=None, path=Path(path), custom_template_path=None, meta=MetaType.POETRY, encoding="utf-8"
45+
url=None, path=Path(path), custom_template_path=None, meta=MetaType.POETRY, file_encoding="utf-8"
4446
)
4547

4648

@@ -86,7 +88,7 @@ def test_generate_url(self, _create_new_client):
8688

8789
assert result.exit_code == 0
8890
_create_new_client.assert_called_once_with(
89-
url=url, path=None, custom_template_path=None, meta=MetaType.POETRY, encoding=None
91+
url=url, path=None, custom_template_path=None, meta=MetaType.POETRY, file_encoding="utf-8"
9092
)
9193

9294
def test_generate_path(self, _create_new_client):
@@ -97,7 +99,7 @@ def test_generate_path(self, _create_new_client):
9799

98100
assert result.exit_code == 0
99101
_create_new_client.assert_called_once_with(
100-
url=None, path=Path(path), custom_template_path=None, meta=MetaType.POETRY, encoding=None
102+
url=None, path=Path(path), custom_template_path=None, meta=MetaType.POETRY, file_encoding="utf-8"
101103
)
102104

103105
def test_generate_meta(self, _create_new_client):
@@ -108,19 +110,19 @@ def test_generate_meta(self, _create_new_client):
108110

109111
assert result.exit_code == 0
110112
_create_new_client.assert_called_once_with(
111-
url=None, path=Path(path), custom_template_path=None, meta=MetaType.NONE, encoding=None
113+
url=None, path=Path(path), custom_template_path=None, meta=MetaType.NONE, file_encoding="utf-8"
112114
)
113115

114116
def test_generate_encoding(self, _create_new_client):
115117
path = "cool/path"
116118
encoding = "utf-8"
117119
from openapi_python_client.cli import MetaType, app
118120

119-
result = runner.invoke(app, ["generate", f"--path={path}", f"--encoding={encoding}"])
121+
result = runner.invoke(app, ["generate", f"--path={path}", f"--file-encoding={encoding}"])
120122

121123
assert result.exit_code == 0
122124
_create_new_client.assert_called_once_with(
123-
url=None, path=Path(path), custom_template_path=None, meta=MetaType.POETRY, encoding="utf-8"
125+
url=None, path=Path(path), custom_template_path=None, meta=MetaType.POETRY, file_encoding="utf-8"
124126
)
125127

126128
def test_generate_handle_errors(self, _create_new_client):
@@ -192,7 +194,7 @@ def test_update_url(self, _update_existing_client):
192194

193195
assert result.exit_code == 0
194196
_update_existing_client.assert_called_once_with(
195-
url=url, path=None, custom_template_path=None, meta=MetaType.POETRY
197+
url=url, path=None, custom_template_path=None, meta=MetaType.POETRY, file_encoding="utf-8"
196198
)
197199

198200
def test_update_path(self, _update_existing_client):
@@ -203,5 +205,5 @@ def test_update_path(self, _update_existing_client):
203205

204206
assert result.exit_code == 0
205207
_update_existing_client.assert_called_once_with(
206-
url=None, path=Path(path), custom_template_path=None, meta=MetaType.POETRY
208+
url=None, path=Path(path), custom_template_path=None, meta=MetaType.POETRY, file_encoding="utf-8"
207209
)

0 commit comments

Comments
 (0)