Skip to content

Commit fdeea99

Browse files
p1-raNementon
andauthored
fix(parser): Softly ignore invalid response status code (#327)
Co-authored-by: Nementon <[email protected]>
1 parent af09640 commit fdeea99

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2626

2727
### Fixes
2828

29+
- 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).
2930
- 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!
3031

3132
## 0.7.3 - 2020-12-21

openapi_python_client/parser/openapi.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,29 @@ def _add_body(
169169
def _add_responses(*, endpoint: "Endpoint", data: oai.Responses, schemas: Schemas) -> Tuple["Endpoint", Schemas]:
170170
endpoint = deepcopy(endpoint)
171171
for code, response_data in data.items():
172+
173+
status_code: int
174+
try:
175+
status_code = int(code)
176+
except ValueError:
177+
endpoint.errors.append(
178+
ParseError(
179+
detail=(
180+
f"Invalid response status code {code} (not a number), "
181+
f"response will be ommitted from generated client"
182+
)
183+
)
184+
)
185+
continue
186+
172187
response, schemas = response_from_data(
173-
status_code=int(code), data=response_data, schemas=schemas, parent_name=endpoint.name
188+
status_code=status_code, data=response_data, schemas=schemas, parent_name=endpoint.name
174189
)
175190
if isinstance(response, ParseError):
176191
endpoint.errors.append(
177192
ParseError(
178193
detail=(
179-
f"Cannot parse response for status code {code}, "
194+
f"Cannot parse response for status code {status_code}, "
180195
f"response will be ommitted from generated client"
181196
),
182197
data=response.data,

tests/test_parser/test_openapi.py

+29
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,35 @@ def test_add_body_happy(self, mocker):
294294
assert endpoint.form_body_reference == form_body_reference
295295
assert endpoint.multipart_body_reference == multipart_body_reference
296296

297+
def test__add_responses_status_code_error(self, mocker):
298+
from openapi_python_client.parser.openapi import Endpoint, Schemas
299+
300+
schemas = Schemas()
301+
response_1_data = mocker.MagicMock()
302+
response_2_data = mocker.MagicMock()
303+
data = {
304+
"not_a_number": response_1_data,
305+
}
306+
endpoint = Endpoint(
307+
path="path",
308+
method="method",
309+
description=None,
310+
name="name",
311+
requires_security=False,
312+
tag="tag",
313+
relative_imports={"import_3"},
314+
)
315+
parse_error = ParseError(data=mocker.MagicMock())
316+
response_from_data = mocker.patch(f"{MODULE_NAME}.response_from_data", return_value=(parse_error, schemas))
317+
318+
response, schemas = Endpoint._add_responses(endpoint=endpoint, data=data, schemas=schemas)
319+
320+
assert response.errors == [
321+
ParseError(
322+
detail=f"Invalid response status code not_a_number (not a number), response will be ommitted from generated client"
323+
)
324+
]
325+
297326
def test__add_responses_error(self, mocker):
298327
from openapi_python_client.parser.openapi import Endpoint, Schemas
299328

0 commit comments

Comments
 (0)