From 125d3e0bb0b2e60792ecc62c3bcaa84989c5c351 Mon Sep 17 00:00:00 2001 From: Dylan Anthony Date: Thu, 6 May 2021 08:41:35 -0600 Subject: [PATCH] fix(parser): Prevent crash when providing a non-string default to a string attribute. Fixes #414 --- openapi_python_client/__main__.py | 4 ++-- openapi_python_client/parser/openapi.py | 9 ++++++++- openapi_python_client/parser/properties/converter.py | 8 +++++--- tests/test___main__.py | 4 ++-- tests/test_parser/test_openapi.py | 8 ++++++-- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/openapi_python_client/__main__.py b/openapi_python_client/__main__.py index 4cafccbaf..f8cd76f35 100644 --- a/openapi_python_client/__main__.py +++ b/openapi_python_client/__main__.py @@ -1,3 +1,3 @@ -from .cli import cli +from .cli import app -cli() +app() diff --git a/openapi_python_client/parser/openapi.py b/openapi_python_client/parser/openapi.py index ca8793d09..f0b9774ce 100644 --- a/openapi_python_client/parser/openapi.py +++ b/openapi_python_client/parser/openapi.py @@ -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) diff --git a/openapi_python_client/parser/properties/converter.py b/openapi_python_client/parser/properties/converter.py index b493755fc..d8556e118 100644 --- a/openapi_python_client/parser/properties/converter.py +++ b/openapi_python_client/parser/properties/converter.py @@ -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 @@ -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]: diff --git a/tests/test___main__.py b/tests/test___main__.py index 4e2a51546..79ef06e0a 100644 --- a/tests/test___main__.py +++ b/tests/test___main__.py @@ -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() diff --git a/tests/test_parser/test_openapi.py b/tests/test_parser/test_openapi.py index c496b7dc2..a220d243c 100644 --- a/tests/test_parser/test_openapi.py +++ b/tests/test_parser/test_openapi.py @@ -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() @@ -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, )