Skip to content

Commit 98ce9b1

Browse files
committed
Added improved naming scheme using parent elements
1 parent 5575eea commit 98ce9b1

File tree

12 files changed

+131
-66
lines changed

12 files changed

+131
-66
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- Response schema handling was unified with input schema handling, meaning that responses will behave differently than before.
1717
Specifically, instead of the content-type deciding what the generated Python type is, the schema itself will.
1818
- Instead of skipping input properties with no type, enum, anyOf, or oneOf declared, the property will be declared as `None`.
19+
- Class (models and Enums) names will now contain the name of their parent element (if any). For example, a property
20+
declared in an endpoint will be named like {endpoint_name}_{previous_class_name}. Classes will no longer be
21+
deduplicated by appending a number to the end of the generated name, so if two names conflict with this new naming
22+
scheme, there will be an error instead.
1923

2024
### Additions
2125

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/test_inline_objects.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77
Client = httpx.Client
88

9-
from ...models.json_body import JsonBody
10-
from ...models.response_200 import Response_200
9+
from ...models.test_inline_objects_json_body import TestInlineObjectsJsonBody
10+
from ...models.test_inline_objects_response_200 import TestInlineObjectsResponse_200
1111

1212

13-
def _parse_response(*, response: httpx.Response) -> Optional[Response_200]:
13+
def _parse_response(*, response: httpx.Response) -> Optional[TestInlineObjectsResponse_200]:
1414
if response.status_code == 200:
15-
response_200 = Response_200.from_dict(response.json())
15+
response_200 = TestInlineObjectsResponse_200.from_dict(response.json())
1616

1717
return response_200
1818
return None
1919

2020

21-
def _build_response(*, response: httpx.Response) -> Response[Response_200]:
21+
def _build_response(*, response: httpx.Response) -> Response[TestInlineObjectsResponse_200]:
2222
return Response(
2323
status_code=response.status_code,
2424
content=response.content,
@@ -30,8 +30,8 @@ def _build_response(*, response: httpx.Response) -> Response[Response_200]:
3030
def httpx_request(
3131
*,
3232
client: Client,
33-
json_body: JsonBody,
34-
) -> Response[Response_200]:
33+
json_body: TestInlineObjectsJsonBody,
34+
) -> Response[TestInlineObjectsResponse_200]:
3535

3636
json_json_body = json_body.to_dict()
3737

end_to_end_tests/golden-record-custom/custom_e2e/models/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
from .body_upload_file_tests_upload_post import BodyUploadFileTestsUploadPost
77
from .different_enum import DifferentEnum
88
from .http_validation_error import HTTPValidationError
9-
from .json_body import JsonBody
10-
from .response_200 import Response_200
9+
from .test_inline_objects_json_body import TestInlineObjectsJsonBody
10+
from .test_inline_objects_response_200 import TestInlineObjectsResponse_200
1111
from .validation_error import ValidationError

end_to_end_tests/golden-record-custom/custom_e2e/models/json_body.py renamed to end_to_end_tests/golden-record-custom/custom_e2e/models/test_inline_objects_json_body.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
@attr.s(auto_attribs=True)
7-
class JsonBody:
7+
class TestInlineObjectsJsonBody:
88
""" """
99

1010
a_property: str
@@ -19,9 +19,9 @@ def to_dict(self) -> Dict[str, Any]:
1919
return field_dict
2020

2121
@staticmethod
22-
def from_dict(d: Dict[str, Any]) -> "JsonBody":
22+
def from_dict(d: Dict[str, Any]) -> "TestInlineObjectsJsonBody":
2323
a_property = d["a_property"]
2424

25-
return JsonBody(
25+
return TestInlineObjectsJsonBody(
2626
a_property=a_property,
2727
)

end_to_end_tests/golden-record/my_test_api_client/models/json_body.py renamed to end_to_end_tests/golden-record-custom/custom_e2e/models/test_inline_objects_response_200.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
@attr.s(auto_attribs=True)
7-
class JsonBody:
7+
class TestInlineObjectsResponse_200:
88
""" """
99

1010
a_property: str
@@ -19,9 +19,9 @@ def to_dict(self) -> Dict[str, Any]:
1919
return field_dict
2020

2121
@staticmethod
22-
def from_dict(d: Dict[str, Any]) -> "JsonBody":
22+
def from_dict(d: Dict[str, Any]) -> "TestInlineObjectsResponse_200":
2323
a_property = d["a_property"]
2424

25-
return JsonBody(
25+
return TestInlineObjectsResponse_200(
2626
a_property=a_property,
2727
)

end_to_end_tests/golden-record/my_test_api_client/api/tests/test_inline_objects.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
import httpx
44

55
from ...client import Client
6-
from ...models.json_body import JsonBody
7-
from ...models.response_200 import Response_200
6+
from ...models.test_inline_objects_json_body import TestInlineObjectsJsonBody
7+
from ...models.test_inline_objects_response_200 import TestInlineObjectsResponse_200
88
from ...types import Response
99

1010

1111
def _get_kwargs(
1212
*,
1313
client: Client,
14-
json_body: JsonBody,
14+
json_body: TestInlineObjectsJsonBody,
1515
) -> Dict[str, Any]:
1616
url = "{}/tests/inline_objects".format(client.base_url)
1717

@@ -28,15 +28,15 @@ def _get_kwargs(
2828
}
2929

3030

31-
def _parse_response(*, response: httpx.Response) -> Optional[Response_200]:
31+
def _parse_response(*, response: httpx.Response) -> Optional[TestInlineObjectsResponse_200]:
3232
if response.status_code == 200:
33-
response_200 = Response_200.from_dict(response.json())
33+
response_200 = TestInlineObjectsResponse_200.from_dict(response.json())
3434

3535
return response_200
3636
return None
3737

3838

39-
def _build_response(*, response: httpx.Response) -> Response[Response_200]:
39+
def _build_response(*, response: httpx.Response) -> Response[TestInlineObjectsResponse_200]:
4040
return Response(
4141
status_code=response.status_code,
4242
content=response.content,
@@ -48,8 +48,8 @@ def _build_response(*, response: httpx.Response) -> Response[Response_200]:
4848
def sync_detailed(
4949
*,
5050
client: Client,
51-
json_body: JsonBody,
52-
) -> Response[Response_200]:
51+
json_body: TestInlineObjectsJsonBody,
52+
) -> Response[TestInlineObjectsResponse_200]:
5353
kwargs = _get_kwargs(
5454
client=client,
5555
json_body=json_body,
@@ -65,8 +65,8 @@ def sync_detailed(
6565
def sync(
6666
*,
6767
client: Client,
68-
json_body: JsonBody,
69-
) -> Optional[Response_200]:
68+
json_body: TestInlineObjectsJsonBody,
69+
) -> Optional[TestInlineObjectsResponse_200]:
7070
""" """
7171

7272
return sync_detailed(
@@ -78,8 +78,8 @@ def sync(
7878
async def asyncio_detailed(
7979
*,
8080
client: Client,
81-
json_body: JsonBody,
82-
) -> Response[Response_200]:
81+
json_body: TestInlineObjectsJsonBody,
82+
) -> Response[TestInlineObjectsResponse_200]:
8383
kwargs = _get_kwargs(
8484
client=client,
8585
json_body=json_body,
@@ -94,8 +94,8 @@ async def asyncio_detailed(
9494
async def asyncio(
9595
*,
9696
client: Client,
97-
json_body: JsonBody,
98-
) -> Optional[Response_200]:
97+
json_body: TestInlineObjectsJsonBody,
98+
) -> Optional[TestInlineObjectsResponse_200]:
9999
""" """
100100

101101
return (

end_to_end_tests/golden-record/my_test_api_client/models/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
from .body_upload_file_tests_upload_post import BodyUploadFileTestsUploadPost
77
from .different_enum import DifferentEnum
88
from .http_validation_error import HTTPValidationError
9-
from .json_body import JsonBody
10-
from .response_200 import Response_200
9+
from .test_inline_objects_json_body import TestInlineObjectsJsonBody
10+
from .test_inline_objects_response_200 import TestInlineObjectsResponse_200
1111
from .validation_error import ValidationError

end_to_end_tests/golden-record-custom/custom_e2e/models/response_200.py renamed to end_to_end_tests/golden-record/my_test_api_client/models/test_inline_objects_json_body.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
@attr.s(auto_attribs=True)
7-
class Response_200:
7+
class TestInlineObjectsJsonBody:
88
""" """
99

1010
a_property: str
@@ -19,9 +19,9 @@ def to_dict(self) -> Dict[str, Any]:
1919
return field_dict
2020

2121
@staticmethod
22-
def from_dict(d: Dict[str, Any]) -> "Response_200":
22+
def from_dict(d: Dict[str, Any]) -> "TestInlineObjectsJsonBody":
2323
a_property = d["a_property"]
2424

25-
return Response_200(
25+
return TestInlineObjectsJsonBody(
2626
a_property=a_property,
2727
)

end_to_end_tests/golden-record/my_test_api_client/models/response_200.py renamed to end_to_end_tests/golden-record/my_test_api_client/models/test_inline_objects_response_200.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
@attr.s(auto_attribs=True)
7-
class Response_200:
7+
class TestInlineObjectsResponse_200:
88
""" """
99

1010
a_property: str
@@ -19,9 +19,9 @@ def to_dict(self) -> Dict[str, Any]:
1919
return field_dict
2020

2121
@staticmethod
22-
def from_dict(d: Dict[str, Any]) -> "Response_200":
22+
def from_dict(d: Dict[str, Any]) -> "TestInlineObjectsResponse_200":
2323
a_property = d["a_property"]
2424

25-
return Response_200(
25+
return TestInlineObjectsResponse_200(
2626
a_property=a_property,
2727
)

openapi_python_client/parser/openapi.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,19 @@ def parse_multipart_body(body: oai.RequestBody) -> Optional[Reference]:
119119

120120
@staticmethod
121121
def parse_request_json_body(
122-
*, body: oai.RequestBody, schemas: Schemas
122+
*, body: oai.RequestBody, schemas: Schemas, parent_name: str
123123
) -> Tuple[Union[Property, PropertyError, None], Schemas]:
124124
""" Return json_body """
125125
body_content = body.content
126126
json_body = body_content.get("application/json")
127127
if json_body is not None and json_body.media_type_schema is not None:
128-
return property_from_data("json_body", required=True, data=json_body.media_type_schema, schemas=schemas)
128+
return property_from_data(
129+
name="json_body",
130+
required=True,
131+
data=json_body.media_type_schema,
132+
schemas=schemas,
133+
parent_name=parent_name,
134+
)
129135
return None, schemas
130136

131137
@staticmethod
@@ -138,7 +144,9 @@ def _add_body(
138144
return endpoint, schemas
139145

140146
endpoint.form_body_reference = Endpoint.parse_request_form_body(data.requestBody)
141-
json_body, schemas = Endpoint.parse_request_json_body(body=data.requestBody, schemas=schemas)
147+
json_body, schemas = Endpoint.parse_request_json_body(
148+
body=data.requestBody, schemas=schemas, parent_name=endpoint.name
149+
)
142150
if isinstance(json_body, ParseError):
143151
return ParseError(detail=f"cannot parse body of endpoint {endpoint.name}", data=json_body.data), schemas
144152

@@ -161,7 +169,9 @@ def _add_body(
161169
def _add_responses(*, endpoint: "Endpoint", data: oai.Responses, schemas: Schemas) -> Tuple["Endpoint", Schemas]:
162170
endpoint = deepcopy(endpoint)
163171
for code, response_data in data.items():
164-
response, schemas = response_from_data(status_code=int(code), data=response_data, schemas=schemas)
172+
response, schemas = response_from_data(
173+
status_code=int(code), data=response_data, schemas=schemas, parent_name=endpoint.name
174+
)
165175
if isinstance(response, ParseError):
166176
endpoint.errors.append(
167177
ParseError(
@@ -188,7 +198,11 @@ def _add_parameters(
188198
if isinstance(param, oai.Reference) or param.param_schema is None:
189199
continue
190200
prop, schemas = property_from_data(
191-
name=param.name, required=param.required, data=param.param_schema, schemas=schemas
201+
name=param.name,
202+
required=param.required,
203+
data=param.param_schema,
204+
schemas=schemas,
205+
parent_name=endpoint.name,
192206
)
193207
if isinstance(prop, ParseError):
194208
return ParseError(detail=f"cannot parse parameter of endpoint {endpoint.name}", data=prop.data), schemas

0 commit comments

Comments
 (0)