-
-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1261 from scorpp/master
Pass serialisation context to model_dump (fixes #1233)
- Loading branch information
Showing
2 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
from pydantic import model_serializer | ||
|
||
from ninja import Router, Schema | ||
from ninja.schema import pydantic_version | ||
from ninja.testing import TestClient | ||
|
||
|
||
def api_endpoint_test(request): | ||
return { | ||
"test1": "foo", | ||
"test2": "bar", | ||
} | ||
|
||
|
||
@pytest.mark.skipif( | ||
pydantic_version < [2, 7], | ||
reason="Serialization context was introduced in Pydantic 2.7", | ||
) | ||
def test_request_is_passed_in_context_when_supported(): | ||
class SchemaWithCustomSerializer(Schema): | ||
test1: str | ||
test2: str | ||
|
||
@model_serializer(mode="wrap") | ||
def ser_model(self, handler, info): | ||
assert "request" in info.context | ||
assert info.context["request"].path == "/test" # check it is HttRequest | ||
assert "response_status" in info.context | ||
|
||
return handler(self) | ||
|
||
router = Router() | ||
router.add_api_operation( | ||
"/test", ["GET"], api_endpoint_test, response=SchemaWithCustomSerializer | ||
) | ||
|
||
TestClient(router).get("/test") | ||
|
||
|
||
@pytest.mark.parametrize( | ||
["pydantic_version"], | ||
[ | ||
[[2, 0]], | ||
[[2, 4]], | ||
[[2, 6]], | ||
], | ||
) | ||
def test_no_serialisation_context_used_when_no_supported(pydantic_version): | ||
class SchemaWithCustomSerializer(Schema): | ||
test1: str | ||
test2: str | ||
|
||
@model_serializer(mode="wrap") | ||
def ser_model(self, handler, info): | ||
if hasattr(info, "context"): | ||
# an actually newer Pydantic, but pydantic_version is still mocked, so no context is expected | ||
assert info.context is None | ||
|
||
return handler(self) | ||
|
||
with mock.patch("ninja.operation.pydantic_version", pydantic_version): | ||
router = Router() | ||
router.add_api_operation( | ||
"/test", ["GET"], api_endpoint_test, response=SchemaWithCustomSerializer | ||
) | ||
|
||
resp_json = TestClient(router).get("/test").json() | ||
|
||
assert resp_json == { | ||
"test1": "foo", | ||
"test2": "bar", | ||
} |