Skip to content
Open
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
33 changes: 20 additions & 13 deletions ninja/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,26 +275,33 @@ def _result_to_response(
# Empty response.
return temporal_response

resp_object = ResponseObject(result)
# ^ we need object because getter_dict seems work only with model_validate
validated_object = response_model.model_validate(
resp_object, context={"request": request, "response_status": status}
model_dump_kwargs: Dict[str, Any] = dict(
by_alias=self.by_alias,
exclude_unset=self.exclude_unset,
exclude_defaults=self.exclude_defaults,
exclude_none=self.exclude_none,
)

model_dump_kwargs: Dict[str, Any] = {}
if pydantic_version >= [2, 7]:
# pydantic added support for serialization context at 2.7
model_dump_kwargs.update(
context={"request": request, "response_status": status}
)

result = validated_object.model_dump(
by_alias=self.by_alias,
exclude_unset=self.exclude_unset,
exclude_defaults=self.exclude_defaults,
exclude_none=self.exclude_none,
**model_dump_kwargs,
)["response"]
if isinstance(result, Schema):
# if the result is already a Schema, just return it
return self.api.create_response(
request,
result.model_dump(**model_dump_kwargs),
temporal_response=temporal_response,
)

resp_object = ResponseObject(result)
# ^ we need object because getter_dict seems work only with model_validate
validated_object = response_model.model_validate(
resp_object, context={"request": request, "response_status": status}
)

result = validated_object.model_dump(**model_dump_kwargs)["response"]
return self.api.create_response(
request, result, temporal_response=temporal_response
)
Expand Down