Skip to content
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
67 changes: 48 additions & 19 deletions ninja/signature/details.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,20 @@ def _args_flatten_map(self, args: List[FuncParam]) -> Dict[str, Tuple[str, ...]]

def _model_flatten_map(self, model: TModel, prefix: str) -> Generator:
field: FieldInfo
for attr, field in model.model_fields.items():
field_name = field.alias or attr
name = f"{prefix}{self.FLATTEN_PATH_SEP}{field_name}"
if is_pydantic_model(field.annotation):
yield from self._model_flatten_map(field.annotation, name) # type: ignore
else:
yield field_name, name
if get_origin(model) in UNION_TYPES:
# If the model is a union type, process each type in the union
for arg in get_args(model):
if arg is type(None):
continue # Skip NoneType
yield from self._model_flatten_map(arg, prefix)
else:
for attr, field in model.model_fields.items():
field_name = field.alias or attr
name = f"{prefix}{self.FLATTEN_PATH_SEP}{field_name}"
if is_pydantic_model(field.annotation):
yield from self._model_flatten_map(field.annotation, name) # type: ignore
else:
yield field_name, name

def _get_param_type(self, name: str, arg: inspect.Parameter) -> FuncParam:
# _EMPTY = self.signature.empty
Expand Down Expand Up @@ -260,9 +267,9 @@ def _get_param_type(self, name: str, arg: inspect.Parameter) -> FuncParam:

# 2) if param name is a part of the path parameter
elif name in self.path_params_names:
assert (
default == self.signature.empty
), f"'{name}' is a path param, default not allowed"
assert default == self.signature.empty, (
f"'{name}' is a path param, default not allowed"
)
param_source = Path(...)

# 3) if param is a collection, or annotation is part of pydantic model:
Expand Down Expand Up @@ -295,7 +302,11 @@ def is_pydantic_model(cls: Any) -> bool:

# Handle Union types
if origin in UNION_TYPES:
return any(issubclass(arg, pydantic.BaseModel) for arg in get_args(cls))
return any(
issubclass(arg, pydantic.BaseModel)
for arg in get_args(cls)
if arg is not type(None)
)
return issubclass(cls, pydantic.BaseModel)
except TypeError: # pragma: no cover
return False
Expand Down Expand Up @@ -338,14 +349,32 @@ def detect_collection_fields(
for attr in path[1:]:
if hasattr(annotation_or_field, "annotation"):
annotation_or_field = annotation_or_field.annotation
annotation_or_field = next(
(
a
for a in annotation_or_field.model_fields.values()
if a.alias == attr
),
annotation_or_field.model_fields.get(attr),
) # pragma: no cover

# check union types
if get_origin(annotation_or_field) in UNION_TYPES:
for arg in get_args(annotation_or_field):
if arg is type(None):
continue # Skip NoneType
if hasattr(arg, "model_fields"):
annotation_or_field = next(
(
a
for a in arg.model_fields.values()
if a.alias == attr
),
arg.model_fields.get(attr),
) # pragma: no cover
else:
continue
else:
annotation_or_field = next(
(
a
for a in annotation_or_field.model_fields.values()
if a.alias == attr
),
annotation_or_field.model_fields.get(attr),
) # pragma: no cover

annotation_or_field = getattr(
annotation_or_field, "outer_type_", annotation_or_field
Expand Down
4 changes: 4 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ def test_is_pydantic_model():
class Model(BaseModel):
x: int

class ModelNone(BaseModel):
x: int | None

assert is_pydantic_model(Model)
assert is_pydantic_model("instance") is False
assert is_pydantic_model(ModelNone)


def test_client():
Expand Down
15 changes: 12 additions & 3 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class SomeModel(BaseModel):
i: int
s: str
f: float
n: int | None = None


class OtherModel(BaseModel):
Expand Down Expand Up @@ -86,7 +87,12 @@ def view7(request, obj: OtherModel = OtherModel(x=1, y=1)):
(
"/test1",
dict(json={"i": "1", "s": "foo", "f": "1.1"}),
{"i": 1, "s": "foo", "f": 1.1},
{"i": 1, "s": "foo", "f": 1.1, "n": None},
),
(
"/test1",
dict(json={"i": "1", "s": "foo", "f": "1.1", "n": 42}),
{"i": 1, "s": "foo", "f": 1.1, "n": 42},
),
(
"/test2",
Expand All @@ -96,12 +102,15 @@ def view7(request, obj: OtherModel = OtherModel(x=1, y=1)):
"other": {"x": 1, "y": 2},
}
),
{"some": {"i": 1, "s": "foo", "f": 1.1}, "other": {"x": 1, "y": 2}},
{
"some": {"i": 1, "s": "foo", "f": 1.1, "n": None},
"other": {"x": 1, "y": 2},
},
),
(
"/test3",
dict(json={"i": "1", "s": "foo", "f": "1.1"}),
{"i": 1, "s": "foo", "f": 1.1},
{"i": 1, "s": "foo", "f": 1.1, "n": None},
),
(
"/test_form",
Expand Down