Skip to content

fix(parser): Prevent crash when providing a non-string default to a string attribute. Fixes #414 #415

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 1 commit into from
May 12, 2021
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
4 changes: 2 additions & 2 deletions openapi_python_client/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .cli import cli
from .cli import app

cli()
app()
9 changes: 8 additions & 1 deletion openapi_python_client/parser/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,14 @@ def _add_body(
body=data.requestBody, schemas=schemas, parent_name=endpoint.name, config=config
)
if isinstance(json_body, ParseError):
return ParseError(detail=f"cannot parse body of endpoint {endpoint.name}", data=json_body.data), schemas
return (
ParseError(
header=f"Cannot parse body of endpoint {endpoint.name}",
detail=json_body.detail,
data=json_body.data,
),
schemas,
)

endpoint.multipart_body_class = Endpoint.parse_multipart_body(body=data.requestBody, config=config)

Expand Down
8 changes: 5 additions & 3 deletions openapi_python_client/parser/properties/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def convert(type_string: str, value: Any) -> Optional[Any]:
raise ValidationError()
try:
return _CONVERTERS[type_string](value)
except (KeyError, ValueError) as e:
except (KeyError, ValueError, AttributeError) as e:
raise ValidationError from e


Expand All @@ -58,8 +58,10 @@ def convert_chain(type_strings: Iterable[str], value: Any) -> Optional[Any]:
raise ValidationError()


def _convert_string(value: str) -> Optional[str]:
return f"{utils.remove_string_escapes(value)!r}"
def _convert_string(value: Any) -> Optional[str]:
if isinstance(value, str):
value = utils.remove_string_escapes(value)
return repr(value)


def _convert_datetime(value: str) -> Optional[str]:
Expand Down
4 changes: 2 additions & 2 deletions tests/test___main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def test_main(mocker):
cli = mocker.patch("openapi_python_client.cli.cli")
app = mocker.patch("openapi_python_client.cli.app")

# noinspection PyUnresolvedReferences
from openapi_python_client import __main__

cli.assert_called_once()
app.assert_called_once()
8 changes: 6 additions & 2 deletions tests/test_parser/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def test_add_body_bad_data(self, mocker):
from openapi_python_client.parser.openapi import Endpoint, Schemas

mocker.patch.object(Endpoint, "parse_request_form_body")
parse_error = ParseError(data=mocker.MagicMock())
parse_error = ParseError(data=mocker.MagicMock(), detail=mocker.MagicMock())
other_schemas = mocker.MagicMock()
mocker.patch.object(Endpoint, "parse_request_json_body", return_value=(parse_error, other_schemas))
endpoint = self.make_endpoint()
Expand All @@ -249,7 +249,11 @@ def test_add_body_bad_data(self, mocker):
)

assert result == (
ParseError(detail=f"cannot parse body of endpoint {endpoint.name}", data=parse_error.data),
ParseError(
header=f"Cannot parse body of endpoint {endpoint.name}",
detail=parse_error.detail,
data=parse_error.data,
),
other_schemas,
)

Expand Down