Skip to content

Commit

Permalink
add a helper
Browse files Browse the repository at this point in the history
  • Loading branch information
eli-bl committed Oct 28, 2024
1 parent 9df3bb4 commit 78bd860
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion openapi_python_client/parser/bodies.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Schemas,
property_from_data,
)
from openapi_python_client.parser.properties.schemas import get_reference_simple_name

from .. import schema as oai
from ..config import Config
Expand Down Expand Up @@ -138,7 +139,7 @@ def _resolve_reference(
references_seen = []
while isinstance(body, oai.Reference) and body.ref not in references_seen:
references_seen.append(body.ref)
body = request_bodies.get(body.ref.split("/")[-1])
body = request_bodies.get(get_reference_simple_name(body.ref))
if isinstance(body, oai.Reference):
return ParseError(detail="Circular $ref in request body", data=body)
if body is None and references_seen:
Expand Down
9 changes: 8 additions & 1 deletion openapi_python_client/parser/properties/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ def parse_reference_path(ref_path_raw: str) -> Union[ReferencePath, ParseError]:
return cast(ReferencePath, parsed.fragment)


def get_reference_simple_name(ref_path: str) -> str:
"""
Takes a path like `/components/schemas/NameOfThing` and returns a string like `NameOfThing`.
"""
return ref_path.split("/", 3)[-1]


@define
class Class:
"""Represents Python class which will be generated from an OpenAPI schema"""
Expand All @@ -56,7 +63,7 @@ class Class:
@staticmethod
def from_string(*, string: str, config: Config) -> "Class":
"""Get a Class from an arbitrary string"""
class_name = string.split("/")[-1] # Get rid of ref path stuff
class_name = get_reference_simple_name(string) # Get rid of ref path stuff
class_name = ClassName(class_name, config.field_prefix)
override = config.class_overrides.get(class_name)

Expand Down
4 changes: 2 additions & 2 deletions openapi_python_client/parser/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from attrs import define

from openapi_python_client import utils
from openapi_python_client.parser.properties.schemas import parse_reference_path
from openapi_python_client.parser.properties.schemas import get_reference_simple_name, parse_reference_path

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

0 comments on commit 78bd860

Please sign in to comment.