Skip to content

Commit 07f1255

Browse files
authored
Merge pull request #725 from cordada/bugfix/pydantic-rut-json-schema-generation-fails
extras: Fix generation of JSON Schema for Pydantic `Rut` type
2 parents 9fc64b3 + dee3fa7 commit 07f1255

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/cl_sii/extras/pydantic_types.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class _RutPydanticAnnotation:
4141
- Customizing the core schema and JSON schema:
4242
https://docs.pydantic.dev/2.9/architecture/#customizing-the-core-schema-and-json-schema
4343
(https://github.com/pydantic/pydantic/blob/v2.9.2/docs/architecture.md#customizing-the-core-schema-and-json-schema)
44+
- Implementing __get_pydantic_json_schema__:
45+
https://docs.pydantic.dev/2.9/concepts/json_schema/#implementing-__get_pydantic_json_schema__
46+
(https://github.com/pydantic/pydantic/blob/v2.9.2/docs/concepts/json_schema.md#implementing-__get_pydantic_json_schema__-)
4447
4548
Examples:
4649
@@ -73,6 +76,7 @@ class _RutPydanticAnnotation:
7376
'78773510-K'
7477
>>> example_type_adapter.dump_json(cl_sii.rut.Rut('78773510-K'))
7578
b'"78773510-K"'
79+
>>> example_json_schema = example_type_adapter.json_schema()
7680
"""
7781

7882
RUT_CANONICAL_STRICT_REGEX: ClassVar[Pattern] = re.compile(
@@ -99,7 +103,7 @@ def validate_from_str(value: str) -> cl_sii.rut.Rut:
99103

100104
from_str_schema = pydantic_core.core_schema.chain_schema(
101105
[
102-
pydantic_core.core_schema.str_schema(pattern=cls.RUT_CANONICAL_STRICT_REGEX),
106+
cls.str_schema(),
103107
pydantic_core.core_schema.no_info_plain_validator_function(validate_from_str),
104108
]
105109
)
@@ -117,6 +121,19 @@ def validate_from_str(value: str) -> cl_sii.rut.Rut:
117121
),
118122
)
119123

124+
@classmethod
125+
def __get_pydantic_json_schema__(
126+
cls,
127+
core_schema: pydantic_core.core_schema.CoreSchema,
128+
handler: pydantic.GetJsonSchemaHandler,
129+
) -> pydantic.json_schema.JsonSchemaValue:
130+
core_schema = cls.str_schema()
131+
return handler(core_schema)
132+
133+
@classmethod
134+
def str_schema(cls) -> pydantic_core.core_schema.CoreSchema:
135+
return pydantic_core.core_schema.str_schema(pattern=cls.RUT_CANONICAL_STRICT_REGEX)
136+
120137

121138
Rut = Annotated[cl_sii.rut.Rut, _RutPydanticAnnotation]
122139
"""

src/tests/test_extras_pydantic_types.py

+32
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,35 @@ def test_deserialize_invalid(self) -> None:
133133

134134
with self.assertRaises(pydantic.ValidationError):
135135
self.pydantic_type_adapter.validate_json(data)
136+
137+
def test_json_schema_for_validation(self) -> None:
138+
# -----Arrange-----
139+
140+
expected_json_schema = {
141+
'type': 'string',
142+
'pattern': '^(\\d{1,8})-([\\dK])$',
143+
}
144+
145+
# -----Act-----
146+
147+
actual_json_schema = self.pydantic_type_adapter.json_schema(mode='validation')
148+
149+
# -----Assert-----
150+
151+
self.assertEqual(expected_json_schema, actual_json_schema)
152+
153+
def test_json_schema_for_serialization(self) -> None:
154+
# -----Arrange-----
155+
156+
expected_json_schema = {
157+
'type': 'string',
158+
'pattern': '^(\\d{1,8})-([\\dK])$',
159+
}
160+
161+
# -----Act-----
162+
163+
actual_json_schema = self.pydantic_type_adapter.json_schema(mode='serialization')
164+
165+
# -----Assert-----
166+
167+
self.assertEqual(expected_json_schema, actual_json_schema)

0 commit comments

Comments
 (0)