Skip to content

Commit 78bd860

Browse files
committed
add a helper
1 parent 9df3bb4 commit 78bd860

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

openapi_python_client/parser/bodies.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Schemas,
1010
property_from_data,
1111
)
12+
from openapi_python_client.parser.properties.schemas import get_reference_simple_name
1213

1314
from .. import schema as oai
1415
from ..config import Config
@@ -138,7 +139,7 @@ def _resolve_reference(
138139
references_seen = []
139140
while isinstance(body, oai.Reference) and body.ref not in references_seen:
140141
references_seen.append(body.ref)
141-
body = request_bodies.get(body.ref.split("/")[-1])
142+
body = request_bodies.get(get_reference_simple_name(body.ref))
142143
if isinstance(body, oai.Reference):
143144
return ParseError(detail="Circular $ref in request body", data=body)
144145
if body is None and references_seen:

openapi_python_client/parser/properties/schemas.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ def parse_reference_path(ref_path_raw: str) -> Union[ReferencePath, ParseError]:
4646
return cast(ReferencePath, parsed.fragment)
4747

4848

49+
def get_reference_simple_name(ref_path: str) -> str:
50+
"""
51+
Takes a path like `/components/schemas/NameOfThing` and returns a string like `NameOfThing`.
52+
"""
53+
return ref_path.split("/", 3)[-1]
54+
55+
4956
@define
5057
class Class:
5158
"""Represents Python class which will be generated from an OpenAPI schema"""
@@ -56,7 +63,7 @@ class Class:
5663
@staticmethod
5764
def from_string(*, string: str, config: Config) -> "Class":
5865
"""Get a Class from an arbitrary string"""
59-
class_name = string.split("/")[-1] # Get rid of ref path stuff
66+
class_name = get_reference_simple_name(string) # Get rid of ref path stuff
6067
class_name = ClassName(class_name, config.field_prefix)
6168
override = config.class_overrides.get(class_name)
6269

openapi_python_client/parser/responses.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from attrs import define
77

88
from openapi_python_client import utils
9-
from openapi_python_client.parser.properties.schemas import parse_reference_path
9+
from openapi_python_client.parser.properties.schemas import get_reference_simple_name, parse_reference_path
1010

1111
from .. import Config
1212
from .. import schema as oai
@@ -98,7 +98,7 @@ def response_from_data( # noqa: PLR0911
9898
return ref_path, schemas
9999
if not ref_path.startswith("/components/responses/"):
100100
return ParseError(data=data, detail=f"$ref to {data.ref} not allowed in responses"), schemas
101-
resp_data = responses.get(ref_path.split("/")[-1], None)
101+
resp_data = responses.get(get_reference_simple_name(ref_path), None)
102102
if not resp_data:
103103
return ParseError(data=data, detail=f"Could not find reference: {data.ref}"), schemas
104104
if not isinstance(resp_data, oai.Response):

0 commit comments

Comments
 (0)