Skip to content

Fix/softly-ignore-invalid-response-status-code #327

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
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixes

- Parser will softly ignore value error during schema responses' status code convertion from string to integer (not a number). Errors will be reported to the end user and parsing will continue to proceed (#327).
- The generated `from_dict` and `to_dict` methods of models will now properly handle `nullable` and `not required` properties that are themselves generated models (#315). Thanks @forest-benchling!

## 0.7.3 - 2020-12-21
Expand Down
19 changes: 17 additions & 2 deletions openapi_python_client/parser/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,29 @@ def _add_body(
def _add_responses(*, endpoint: "Endpoint", data: oai.Responses, schemas: Schemas) -> Tuple["Endpoint", Schemas]:
endpoint = deepcopy(endpoint)
for code, response_data in data.items():

status_code: int
try:
status_code = int(code)
except ValueError:
endpoint.errors.append(
ParseError(
detail=(
f"Invalid response status code {code} (not a number), "
f"response will be ommitted from generated client"
)
)
)
continue

response, schemas = response_from_data(
status_code=int(code), data=response_data, schemas=schemas, parent_name=endpoint.name
status_code=status_code, data=response_data, schemas=schemas, parent_name=endpoint.name
)
if isinstance(response, ParseError):
endpoint.errors.append(
ParseError(
detail=(
f"Cannot parse response for status code {code}, "
f"Cannot parse response for status code {status_code}, "
f"response will be ommitted from generated client"
),
data=response.data,
Expand Down
29 changes: 29 additions & 0 deletions tests/test_parser/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,35 @@ def test_add_body_happy(self, mocker):
assert endpoint.form_body_reference == form_body_reference
assert endpoint.multipart_body_reference == multipart_body_reference

def test__add_responses_status_code_error(self, mocker):
from openapi_python_client.parser.openapi import Endpoint, Schemas

schemas = Schemas()
response_1_data = mocker.MagicMock()
response_2_data = mocker.MagicMock()
data = {
"not_a_number": response_1_data,
}
endpoint = Endpoint(
path="path",
method="method",
description=None,
name="name",
requires_security=False,
tag="tag",
relative_imports={"import_3"},
)
parse_error = ParseError(data=mocker.MagicMock())
response_from_data = mocker.patch(f"{MODULE_NAME}.response_from_data", return_value=(parse_error, schemas))

response, schemas = Endpoint._add_responses(endpoint=endpoint, data=data, schemas=schemas)

assert response.errors == [
ParseError(
detail=f"Invalid response status code not_a_number (not a number), response will be ommitted from generated client"
)
]

def test__add_responses_error(self, mocker):
from openapi_python_client.parser.openapi import Endpoint, Schemas

Expand Down