Skip to content

Commit a1ec608

Browse files
forest-benchlingdbanty
authored andcommitted
feat: Add option to fail on warning [#427]. Thanks @forest-benchling!
1 parent 54ee592 commit a1ec608

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

Diff for: openapi_python_client/cli.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def _print_parser_error(e: GeneratorError, color: str) -> None:
5454
typer.echo()
5555

5656

57-
def handle_errors(errors: Sequence[GeneratorError]) -> None:
57+
def handle_errors(errors: Sequence[GeneratorError], fail_on_warning: bool = False) -> None:
5858
"""Turn custom errors into formatted error messages"""
5959
if len(errors) == 0:
6060
return
@@ -91,7 +91,7 @@ def handle_errors(errors: Sequence[GeneratorError]) -> None:
9191
err=True,
9292
)
9393

94-
if error_level == ErrorLevel.ERROR:
94+
if error_level == ErrorLevel.ERROR or fail_on_warning:
9595
raise typer.Exit(code=1)
9696

9797

@@ -119,6 +119,7 @@ def generate(
119119
meta: MetaType = _meta_option,
120120
file_encoding: str = typer.Option("utf-8", help="Encoding used when writing generated"),
121121
config_path: Optional[pathlib.Path] = CONFIG_OPTION,
122+
fail_on_warning: bool = False,
122123
) -> None:
123124
"""Generate a new OpenAPI Client library"""
124125
from . import create_new_client
@@ -145,7 +146,7 @@ def generate(
145146
file_encoding=file_encoding,
146147
config=config,
147148
)
148-
handle_errors(errors)
149+
handle_errors(errors, fail_on_warning)
149150

150151

151152
@app.command()
@@ -156,6 +157,7 @@ def update(
156157
meta: MetaType = _meta_option,
157158
file_encoding: str = typer.Option("utf-8", help="Encoding used when writing generated"),
158159
config_path: Optional[pathlib.Path] = CONFIG_OPTION,
160+
fail_on_warning: bool = False,
159161
) -> None:
160162
"""Update an existing OpenAPI Client library"""
161163
from . import update_existing_client
@@ -182,4 +184,4 @@ def update(
182184
file_encoding=file_encoding,
183185
config=config,
184186
)
185-
handle_errors(errors)
187+
handle_errors(errors, fail_on_warning)

Diff for: tests/test_cli.py

+22
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,28 @@ def test_generate_handle_multiple_warnings(self, _create_new_client):
194194
"https://github.com/triaxtec/openapi-python-client/issues/new/choose\n"
195195
)
196196

197+
def test_generate_fail_on_warning(self, _create_new_client):
198+
error_1 = ParseError(data={"test": "data"}, detail="this is a message")
199+
error_2 = ParseError(data={"other": "data"}, detail="this is another message", header="Custom Header")
200+
_create_new_client.return_value = [error_1, error_2]
201+
path = "cool/path"
202+
from openapi_python_client.cli import app
203+
204+
result = runner.invoke(app, ["generate", f"--path={path}", "--fail-on-warning"])
205+
206+
assert result.exit_code == 1
207+
assert result.output == (
208+
"Warning(s) encountered while generating. Client was generated, but some pieces may be missing\n\n"
209+
"Unable to parse this part of your OpenAPI document: \n\n"
210+
"this is a message\n\n"
211+
"{'test': 'data'}\n\n"
212+
"Custom Header\n\n"
213+
"this is another message\n\n"
214+
"{'other': 'data'}\n\n"
215+
"If you believe this was a mistake or this tool is missing a feature you need, please open an issue at "
216+
"https://github.com/triaxtec/openapi-python-client/issues/new/choose\n"
217+
)
218+
197219

198220
@pytest.fixture
199221
def _update_existing_client(mocker):

0 commit comments

Comments
 (0)