Skip to content

feat: Add option to fail generating client if there are any warnings #427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions openapi_python_client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def _print_parser_error(e: GeneratorError, color: str) -> None:
typer.echo()


def handle_errors(errors: Sequence[GeneratorError]) -> None:
def handle_errors(errors: Sequence[GeneratorError], fail_on_warning: bool = False) -> None:
"""Turn custom errors into formatted error messages"""
if len(errors) == 0:
return
Expand Down Expand Up @@ -91,7 +91,7 @@ def handle_errors(errors: Sequence[GeneratorError]) -> None:
err=True,
)

if error_level == ErrorLevel.ERROR:
if error_level == ErrorLevel.ERROR or fail_on_warning:
raise typer.Exit(code=1)


Expand Down Expand Up @@ -119,6 +119,7 @@ def generate(
meta: MetaType = _meta_option,
file_encoding: str = typer.Option("utf-8", help="Encoding used when writing generated"),
config_path: Optional[pathlib.Path] = CONFIG_OPTION,
fail_on_warning: bool = False,
) -> None:
"""Generate a new OpenAPI Client library"""
from . import create_new_client
Expand All @@ -145,7 +146,7 @@ def generate(
file_encoding=file_encoding,
config=config,
)
handle_errors(errors)
handle_errors(errors, fail_on_warning)


@app.command()
Expand All @@ -156,6 +157,7 @@ def update(
meta: MetaType = _meta_option,
file_encoding: str = typer.Option("utf-8", help="Encoding used when writing generated"),
config_path: Optional[pathlib.Path] = CONFIG_OPTION,
fail_on_warning: bool = False,
) -> None:
"""Update an existing OpenAPI Client library"""
from . import update_existing_client
Expand All @@ -182,4 +184,4 @@ def update(
file_encoding=file_encoding,
config=config,
)
handle_errors(errors)
handle_errors(errors, fail_on_warning)
22 changes: 22 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,28 @@ def test_generate_handle_multiple_warnings(self, _create_new_client):
"https://github.com/triaxtec/openapi-python-client/issues/new/choose\n"
)

def test_generate_fail_on_warning(self, _create_new_client):
error_1 = ParseError(data={"test": "data"}, detail="this is a message")
error_2 = ParseError(data={"other": "data"}, detail="this is another message", header="Custom Header")
_create_new_client.return_value = [error_1, error_2]
path = "cool/path"
from openapi_python_client.cli import app

result = runner.invoke(app, ["generate", f"--path={path}", "--fail-on-warning"])

assert result.exit_code == 1
assert result.output == (
"Warning(s) encountered while generating. Client was generated, but some pieces may be missing\n\n"
"Unable to parse this part of your OpenAPI document: \n\n"
"this is a message\n\n"
"{'test': 'data'}\n\n"
"Custom Header\n\n"
"this is another message\n\n"
"{'other': 'data'}\n\n"
"If you believe this was a mistake or this tool is missing a feature you need, please open an issue at "
"https://github.com/triaxtec/openapi-python-client/issues/new/choose\n"
)


@pytest.fixture
def _update_existing_client(mocker):
Expand Down