|
1 | 1 | import pytest
|
2 | 2 |
|
3 |
| -from pydantic_core import SchemaSerializer, core_schema |
| 3 | +from pydantic_core import SchemaSerializer, SchemaValidator, core_schema |
4 | 4 |
|
5 | 5 | from ..conftest import plain_repr
|
6 | 6 |
|
@@ -60,3 +60,39 @@ def test_lax_or_strict_custom_ser():
|
60 | 60 | assert s.to_python('abc') == ' abc '
|
61 | 61 | assert s.to_python('abc', mode='json') == ' abc '
|
62 | 62 | assert s.to_json('abc') == b'" abc "'
|
| 63 | + |
| 64 | + |
| 65 | +def test_serialize_with_extra_on_superclass() -> None: |
| 66 | + class Parent: |
| 67 | + x: int |
| 68 | + |
| 69 | + class Other(Parent): |
| 70 | + y: str |
| 71 | + |
| 72 | + Parent.__pydantic_core_schema__ = core_schema.model_schema( |
| 73 | + Parent, |
| 74 | + core_schema.model_fields_schema( |
| 75 | + { |
| 76 | + 'x': core_schema.model_field(core_schema.int_schema()), |
| 77 | + } |
| 78 | + ), |
| 79 | + config=core_schema.CoreConfig(extra_fields_behavior='allow'), |
| 80 | + ) |
| 81 | + Parent.__pydantic_validator__ = SchemaValidator(Parent.__pydantic_core_schema__) |
| 82 | + Parent.__pydantic_serializer__ = SchemaSerializer(Parent.__pydantic_core_schema__) |
| 83 | + |
| 84 | + Other.__pydantic_core_schema__ = core_schema.model_schema( |
| 85 | + Other, |
| 86 | + core_schema.model_fields_schema( |
| 87 | + { |
| 88 | + 'x': core_schema.model_field(core_schema.int_schema()), |
| 89 | + 'y': core_schema.model_field(core_schema.str_schema()), |
| 90 | + } |
| 91 | + ), |
| 92 | + config=core_schema.CoreConfig(extra_fields_behavior='forbid'), |
| 93 | + ) |
| 94 | + Other.__pydantic_validator__ = SchemaValidator(Other.__pydantic_core_schema__) |
| 95 | + Other.__pydantic_serializer__ = SchemaSerializer(Other.__pydantic_core_schema__) |
| 96 | + |
| 97 | + other = Other.__pydantic_validator__.validate_python({'x': 1, 'y': 'some string'}) |
| 98 | + assert Parent.__pydantic_serializer__.to_python(other) == {'x': 1} |
0 commit comments