Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 202 #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions apiclient_pydantic_generator/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@
from datamodel_code_generator.types import DataType, DataTypeManager, StrictTypes
from pydantic import BaseModel, validator


RE_APPLICATION_JSON_PATTERN: Pattern[str] = re.compile(r'^application/.*json$')


class CachedPropertyModel(BaseModel):
class Config:
arbitrary_types_allowed = True
keep_untouched = (cached_property, )
keep_untouched = (cached_property,)


class Response(BaseModel):
Expand Down Expand Up @@ -359,8 +358,8 @@ def parse_request_body(
super().parse_request_body(name, request_body, path)
arguments: List[Argument] = []
for (
media_type,
media_obj,
media_type,
media_obj,
) in request_body.content.items():
if isinstance(media_obj.schema_, (JsonSchemaObject, ReferenceObject)): # pragma: no cover
if RE_APPLICATION_JSON_PATTERN.match(media_type):
Expand All @@ -384,18 +383,23 @@ def parse_responses(
path: List[str],
) -> Dict[str, Dict[str, DataType]]:
data_types = super().parse_responses(name, responses, path)
status_code_200 = data_types.get('200')
if status_code_200:
data_type = list(status_code_200.values())[0]
if data_type:
self.data_types.append(data_type)
type_hint = data_type.type_hint # TODO: change to lazy loading
else:
type_hint = 'None'
type_hint = 'None'
for code in [200, 201, 202]:
response_model = self._get_response(code, data_types)
if response_model:
type_hint = response_model.type_hint # TODO: change to lazy loading
break
self._temporary_operation['response'] = type_hint

return data_types

def _get_response(self, status_code: int, data_types: dict[str, dict[str, DataType]]) -> DataType:
response_model = data_types.get(f"{status_code}")
if response_model:
data_type = list(response_model.values())[0]
if data_type:
self.data_types.append(data_type)
return data_type

def parse_operation(
self,
raw_operation: Dict[str, Any],
Expand Down
1 change: 1 addition & 0 deletions apiclient_pydantic_generator/templates/client.jinja2
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import annotations
from typing import Any

from {% if base_cls.from_ %}{{base_cls.from_}}{% else %}.{% endif %} import {{base_cls.import_}}{% if base_cls.alias %} as {{base_cls.alias}}{% endif %}
from apiclient_pydantic import serialize_all_methods
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pathlib import Path

import pytest


@pytest.fixture
def resources_folder():
return Path(__file__).parent / "resources"
Loading