Skip to content

Commit fb88eba

Browse files
authored
Merge pull request #383 from yyamano/issue-334
Fixed broken test (Added option for inputting encoding of file #334)
2 parents 9730a6d + 3ad6e66 commit fb88eba

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

fastapi_code_generator/__main__.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def dynamic_load_module(module_path: Path) -> Any:
4343

4444
@app.command()
4545
def main(
46-
input_file: typer.FileText = typer.Option(..., "--input", "-i"),
46+
encoding: str = typer.Option("utf-8", "--encoding", "-e"),
47+
input_file: str = typer.Option(..., "--input", "-i"),
4748
output_dir: Path = typer.Option(..., "--output", "-o"),
4849
model_file: str = typer.Option(None, "--model-file", "-m"),
4950
template_dir: Optional[Path] = typer.Option(None, "--template-dir", "-t"),
@@ -57,8 +58,12 @@ def main(
5758
),
5859
disable_timestamp: bool = typer.Option(False, "--disable-timestamp"),
5960
) -> None:
60-
input_name: str = input_file.name
61-
input_text: str = input_file.read()
61+
input_name: str = input_file
62+
input_text: str
63+
64+
with open(input_file, encoding=encoding) as f:
65+
input_text = f.read()
66+
6267
if model_file:
6368
model_path = Path(model_file).with_suffix('.py')
6469
else:
@@ -68,6 +73,7 @@ def main(
6873
return generate_code(
6974
input_name,
7075
input_text,
76+
encoding,
7177
output_dir,
7278
template_dir,
7379
model_path,
@@ -80,6 +86,7 @@ def main(
8086
return generate_code(
8187
input_name,
8288
input_text,
89+
encoding,
8390
output_dir,
8491
template_dir,
8592
model_path,
@@ -103,6 +110,7 @@ def _get_most_of_reference(data_type: DataType) -> Optional[Reference]:
103110
def generate_code(
104111
input_name: str,
105112
input_text: str,
113+
encoding: str,
106114
output_dir: Path,
107115
template_dir: Optional[Path],
108116
model_path: Optional[Path] = None,
@@ -219,7 +227,9 @@ def generate_code(
219227
header += f"\n# timestamp: {timestamp}"
220228

221229
for path, code in results.items():
222-
with output_dir.joinpath(path.with_suffix(".py")).open("wt") as file:
230+
with output_dir.joinpath(path.with_suffix(".py")).open(
231+
"wt", encoding=encoding
232+
) as file:
223233
print(header, file=file)
224234
print("", file=file)
225235
print(code.rstrip(), file=file)

tests/test_generate.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
SPECIFIC_TAGS = 'Wild Boars, Fat Cats'
2424

25+
ENCODING = 'utf-8'
26+
2527

2628
@pytest.mark.parametrize(
2729
"oas_file", (DATA_DIR / OPEN_API_DEFAULT_TEMPLATE_DIR_NAME).glob("*.yaml")
@@ -33,6 +35,7 @@ def test_generate_default_template(oas_file):
3335
generate_code(
3436
input_name=oas_file.name,
3537
input_text=oas_file.read_text(),
38+
encoding=ENCODING,
3639
output_dir=output_dir,
3740
template_dir=None,
3841
)
@@ -54,6 +57,7 @@ def test_generate_custom_security_template(oas_file):
5457
generate_code(
5558
input_name=oas_file.name,
5659
input_text=oas_file.read_text(),
60+
encoding=ENCODING,
5761
output_dir=output_dir,
5862
template_dir=DATA_DIR / 'custom_template' / 'security',
5963
)
@@ -79,6 +83,7 @@ def test_generate_remote_ref(mocker):
7983
generate_code(
8084
input_name=oas_file.name,
8185
input_text=oas_file.read_text(),
86+
encoding=ENCODING,
8287
output_dir=output_dir,
8388
template_dir=None,
8489
)
@@ -105,6 +110,7 @@ def test_disable_timestamp(oas_file):
105110
generate_code(
106111
input_name=oas_file.name,
107112
input_text=oas_file.read_text(),
113+
encoding=ENCODING,
108114
output_dir=output_dir,
109115
template_dir=None,
110116
disable_timestamp=True,
@@ -130,6 +136,7 @@ def test_generate_using_routers(oas_file):
130136
generate_code(
131137
input_name=oas_file.name,
132138
input_text=oas_file.read_text(),
139+
encoding=ENCODING,
133140
output_dir=output_dir,
134141
template_dir=BUILTIN_MODULAR_TEMPLATE_DIR,
135142
generate_routers=True,
@@ -166,6 +173,7 @@ def test_generate_modify_specific_routers(oas_file):
166173
generate_code(
167174
input_name=oas_file.name,
168175
input_text=oas_file.read_text(),
176+
encoding=ENCODING,
169177
output_dir=output_dir,
170178
template_dir=BUILTIN_MODULAR_TEMPLATE_DIR,
171179
generate_routers=True,

0 commit comments

Comments
 (0)