Skip to content

Commit

Permalink
🐛 Fixed a bug with working with the config of a model that does not h…
Browse files Browse the repository at this point in the history
…ave extra.
  • Loading branch information
mom1 committed Mar 9, 2022
1 parent 7471090 commit 6198858
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
10 changes: 7 additions & 3 deletions apiclient_pydantic/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def __init__(
def __call__(self, func: Callable) -> Callable:
attrs, self.signature = {}, get_typed_signature(func)
new_signature_parameters: DictStrAny = {}
forbid_attrs = set()

for name, arg in self.signature.parameters.items():
if name == 'self':
Expand All @@ -103,18 +104,21 @@ def __call__(self, func: Callable) -> Callable:
new_signature_parameters.setdefault(arg.name, arg)

attrs[name] = (arg_type, ...)

if is_pydantic_model(arg_type) and getattr(arg_type.Config, 'extra', None) == Extra.forbid:
forbid_attrs.add(name)
if attrs:
self.model_param = create_model(f'{func.__name__}Params', __config__=BaseConfig, **attrs) # type: ignore
config_cls = type(f'{func.__name__}Config', (BaseConfig,), {'forbid_attrs': forbid_attrs})
self.model_param = create_model(f'{func.__name__}Params', __config__=config_cls, **attrs) # type: ignore

@wraps(func)
def wrap(*args, **kwargs):
object_params = {}
forbid_attrs = getattr(self.model_param.Config, 'forbid_attrs', {})
for name, fld in self.model_param.__fields__.items():
kw = kwargs if name not in kwargs else kwargs[name]

object_params[name] = kw
if is_pydantic_model(fld.type_) and fld.type_.Config.extra == Extra.forbid and isinstance(kw, dict):
if is_pydantic_model(fld.type_) and name in forbid_attrs and isinstance(kw, dict):
object_params[name] = {k: v for k, v in kw.items() if k in fld.type_.__fields__}

params_object = ParamsObject(**object_params)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_seializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ class SimpleModel(BaseModel):
test_attr: str = 'Param'


class SimpleConfigModel(BaseModel):
test_attr: str = Field(alias='TestAttr')

class Config:
allow_population_by_field_name = True


class SimpleTestModel(BaseModel):
test: str

Expand Down Expand Up @@ -79,6 +86,9 @@ def function_union(self, data: Union[SimpleTestModel, SimpleModel]):
def function_list_response(self, data: SimpleModel) -> List[SimpleModel]:
return [data]

def function_config_test(self, data: SimpleConfigModel):
return data


def test_function_without_all():
client = Client()
Expand Down Expand Up @@ -192,6 +202,11 @@ def test_function_list_response():
assert client.function_list_response(test_attr='bla') == [{'test_attr': 'bla'}] # type: ignore


def test_function_config_test():
client = Client()
assert client.function_config_test(test_attr='bla') == {'TestAttr': 'bla'} # type: ignore


def test_param_for_model():
class MyModel(BaseModel):
test: str = Field(alias='TesT')
Expand Down

0 comments on commit 6198858

Please sign in to comment.