Skip to content

Commit 90a1ddf

Browse files
lee-elenbaasdbanty
andauthored
Add response data to template data (#767)
Co-authored-by: Dylan Anthony <[email protected]> Co-authored-by: Dylan Anthony <[email protected]>
1 parent 8c919b3 commit 90a1ddf

File tree

4 files changed

+28
-57
lines changed

4 files changed

+28
-57
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
default: minor
3+
---
4+
5+
# Add original OpenAPI `data` attribute to `Response` object
6+
7+
PR #767
8+
9+
In custom templates, you can now access a `response.data` attribute that contains the original OpenAPI definition of the
10+
response (Response Object or Reference Object).

openapi_python_client/parser/responses.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Response:
3434
status_code: HTTPStatus
3535
prop: Property
3636
source: _ResponseSource
37+
data: Union[oai.Response, oai.Reference] # Original data which created this response, useful for custom templates
3738

3839

3940
def _source_by_content_type(content_type: str) -> Optional[_ResponseSource]:
@@ -60,17 +61,18 @@ def empty_response(
6061
status_code: HTTPStatus,
6162
response_name: str,
6263
config: Config,
63-
description: Optional[str],
64+
data: Union[oai.Response, oai.Reference],
6465
) -> Response:
6566
"""Return an untyped response, for when no response type is defined"""
6667
return Response(
68+
data=data,
6769
status_code=status_code,
6870
prop=AnyProperty(
6971
name=response_name,
7072
default=None,
7173
required=True,
7274
python_name=PythonIdentifier(value=response_name, prefix=config.field_prefix),
73-
description=description,
75+
description=data.description if isinstance(data, oai.Response) else None,
7476
example=None,
7577
),
7678
source=NONE_SOURCE,
@@ -94,7 +96,7 @@ def response_from_data(
9496
status_code=status_code,
9597
response_name=response_name,
9698
config=config,
97-
description=None,
99+
data=data,
98100
),
99101
schemas,
100102
)
@@ -106,7 +108,7 @@ def response_from_data(
106108
status_code=status_code,
107109
response_name=response_name,
108110
config=config,
109-
description=data.description,
111+
data=data,
110112
),
111113
schemas,
112114
)
@@ -128,7 +130,7 @@ def response_from_data(
128130
status_code=status_code,
129131
response_name=response_name,
130132
config=config,
131-
description=data.description,
133+
data=data,
132134
),
133135
schemas,
134136
)
@@ -145,4 +147,4 @@ def response_from_data(
145147
if isinstance(prop, PropertyError):
146148
return prop, schemas
147149

148-
return Response(status_code=status_code, prop=prop, source=source), schemas
150+
return Response(status_code=status_code, prop=prop, source=source, data=data), schemas

tests/test_parser/test_openapi.py

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -187,55 +187,6 @@ def test__add_responses_error(self, mocker):
187187
),
188188
]
189189

190-
def test__add_responses(self, mocker, date_time_property_factory, date_property_factory):
191-
from openapi_python_client.parser.openapi import Endpoint, Response
192-
193-
response_1_data = mocker.MagicMock()
194-
response_2_data = mocker.MagicMock()
195-
data = {
196-
"200": response_1_data,
197-
"404": response_2_data,
198-
}
199-
endpoint = self.make_endpoint()
200-
schemas = mocker.MagicMock()
201-
schemas_1 = mocker.MagicMock()
202-
schemas_2 = mocker.MagicMock()
203-
response_1 = Response(
204-
status_code=200,
205-
source="source",
206-
prop=date_time_property_factory(name="datetime"),
207-
)
208-
response_2 = Response(
209-
status_code=404,
210-
source="source",
211-
prop=date_property_factory(name="date"),
212-
)
213-
response_from_data = mocker.patch(
214-
f"{MODULE_NAME}.response_from_data", side_effect=[(response_1, schemas_1), (response_2, schemas_2)]
215-
)
216-
config = MagicMock()
217-
218-
endpoint, response_schemas = Endpoint._add_responses(
219-
endpoint=endpoint, data=data, schemas=schemas, config=config
220-
)
221-
222-
response_from_data.assert_has_calls(
223-
[
224-
mocker.call(status_code=200, data=response_1_data, schemas=schemas, parent_name="name", config=config),
225-
mocker.call(
226-
status_code=404, data=response_2_data, schemas=schemas_1, parent_name="name", config=config
227-
),
228-
]
229-
)
230-
assert endpoint.responses == [response_1, response_2]
231-
assert endpoint.relative_imports == {
232-
"from dateutil.parser import isoparse",
233-
"from typing import cast",
234-
"import datetime",
235-
"import_3",
236-
}
237-
assert response_schemas == schemas_2
238-
239190
def test_add_parameters_handles_no_params(self):
240191
from openapi_python_client.parser.openapi import Endpoint, Schemas
241192

tests/test_parser/test_responses.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
def test_response_from_data_no_content(any_property_factory):
1212
from openapi_python_client.parser.responses import Response, response_from_data
1313

14+
data = oai.Response.model_construct(description="")
15+
1416
response, schemas = response_from_data(
1517
status_code=200,
16-
data=oai.Response.model_construct(description=""),
18+
data=data,
1719
schemas=Schemas(),
1820
parent_name="parent",
1921
config=MagicMock(),
@@ -28,15 +30,18 @@ def test_response_from_data_no_content(any_property_factory):
2830
description="",
2931
),
3032
source=NONE_SOURCE,
33+
data=data,
3134
)
3235

3336

3437
def test_response_from_data_reference(any_property_factory):
3538
from openapi_python_client.parser.responses import Response, response_from_data
3639

40+
data = oai.Reference.model_construct()
41+
3742
response, schemas = response_from_data(
3843
status_code=200,
39-
data=oai.Reference.model_construct(),
44+
data=data,
4045
schemas=Schemas(),
4146
parent_name="parent",
4247
config=MagicMock(),
@@ -50,6 +55,7 @@ def test_response_from_data_reference(any_property_factory):
5055
required=True,
5156
),
5257
source=NONE_SOURCE,
58+
data=data,
5359
)
5460

5561

@@ -92,6 +98,7 @@ def test_response_from_data_no_content_schema(any_property_factory):
9298
description=data.description,
9399
),
94100
source=NONE_SOURCE,
101+
data=data,
95102
)
96103

97104

@@ -147,6 +154,7 @@ def test_response_from_data_property(mocker, any_property_factory):
147154
status_code=400,
148155
prop=prop,
149156
source=JSON_SOURCE,
157+
data=data,
150158
)
151159
property_from_data.assert_called_once_with(
152160
name="response_400",

0 commit comments

Comments
 (0)