Skip to content

Commit

Permalink
small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-rp committed May 6, 2024
1 parent d8a194a commit 6f63396
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
8 changes: 7 additions & 1 deletion openapi_python_client/detectors/base_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from openapi_python_client.parser.endpoints import Response
from openapi_python_client.parser.context import OpenapiContext
from openapi_python_client.parser.parameters import Parameter
from openapi_python_client.parser.models import SchemaWrapper, DataPropertyPath


class BaseDetector(ABC):
Expand All @@ -23,7 +24,8 @@ def detect_response_and_pagination(
...

@abstractmethod
def detect_payload_path(self) -> None:
def detect_payload(self, content_schema: "SchemaWrapper", expect_list: bool) -> Optional["DataPropertyPath"]:
"""Detect json path of the actual payload within a content schema"""
...

@abstractmethod
Expand All @@ -37,3 +39,7 @@ def detect_primary_key(self) -> None:
@abstractmethod
def detect_parent_endpoint(self) -> None:
...

@abstractmethod
def detect_transformer_mapping(self) -> None:
...
14 changes: 6 additions & 8 deletions openapi_python_client/detectors/default/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,13 @@ def detect_response_and_pagination(
# detect pagination
pagination = self._detect_pagination(content_schema, parameters)

# detect response payload path
# detect response payload path, we can give a hint wether we expect a list or not
path_suggests_list = not is_var_part(get_path_parts(path)[-1])
expect_list = (pagination is not None) or path_suggests_list
response.payload = self._detect_payload(content_schema=content_schema, expect_list=expect_list)
response.payload = self.detect_payload(content_schema=content_schema, expect_list=expect_list)

return pagination, response

def detect_payload_path(self) -> None:
...

def detect_authentication(self) -> None:
...

Expand All @@ -65,7 +62,10 @@ def detect_primary_key(self) -> None:
def detect_parent_endpoint(self) -> None:
...

def _detect_payload(self, content_schema: SchemaWrapper, expect_list: bool) -> Optional[DataPropertyPath]:
def detect_transformer_mapping(self) -> None:
...

def detect_payload(self, content_schema: SchemaWrapper, expect_list: bool) -> Optional[DataPropertyPath]:
"""Detect payload path in given schema"""
payload: Optional[DataPropertyPath] = None
payload_schema: Optional[SchemaWrapper] = content_schema
Expand All @@ -75,7 +75,6 @@ def _detect_payload(self, content_schema: SchemaWrapper, expect_list: bool) -> O
payload_path: List[str] = []

if expect_list:
# TODO: improve heuristics and move into some utility function for testing
if payload_schema.is_list:
payload = DataPropertyPath(tuple(payload_path), payload_schema)
else:
Expand All @@ -88,7 +87,6 @@ def _detect_payload(self, content_schema: SchemaWrapper, expect_list: bool) -> O
prop = payload_schema.properties[0]
payload_path.append(prop.name)
payload_schema = prop.schema

payload = DataPropertyPath(tuple(payload_path), payload_schema)

return payload
Expand Down
4 changes: 1 addition & 3 deletions openapi_python_client/parser/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ def payload(self) -> Optional[DataPropertyPath]:
@property
def is_list(self) -> bool:
"""if we know the payload, we can discover from there, if not assume list if last path part is not arg"""
if self.payload:
return self.payload.is_list
return not is_var_part(self.path_parts[-1])
return self.payload.is_list if self.payload else (not is_var_part(self.path_parts[-1]))

@property
def parent(self) -> Optional["Endpoint"]:
Expand Down

0 comments on commit 6f63396

Please sign in to comment.