Skip to content

Commit fc52138

Browse files
committed
Traverse function-before schemas during schema gathering
Backport of: #11801
1 parent 25af789 commit fc52138

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

pydantic/_internal/_schema_gather.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ def traverse_schema(schema: AllSchemas, context: GatherContext) -> None:
161161
traverse_schema(schema['return_schema'], context)
162162
elif schema_type == 'computed-field':
163163
traverse_schema(schema['return_schema'], context)
164+
elif schema_type == 'function-before':
165+
if 'schema' in schema:
166+
traverse_schema(schema['schema'], context)
167+
if 'json_schema_input_schema' in schema:
168+
traverse_schema(schema['json_schema_input_schema'], context)
164169
elif schema_type == 'function-plain':
165170
# TODO duplicate schema types for serializers and validators, needs to be deduplicated.
166171
if 'return_schema' in schema:

tests/test_json_schema.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6589,6 +6589,19 @@ class Model(BaseModel):
65896589
}
65906590

65916591

6592+
def test_json_schema_input_type_inlined() -> None:
6593+
class Sub(BaseModel):
6594+
pass
6595+
6596+
class Model(BaseModel):
6597+
sub: Annotated[object, BeforeValidator(lambda v: v, json_schema_input_type=Sub)]
6598+
6599+
json_schema = Model.model_json_schema()
6600+
6601+
assert 'Sub' in json_schema['$defs']
6602+
assert json_schema['properties']['sub'] == {'$ref': '#/$defs/Sub'}
6603+
6604+
65926605
@pytest.mark.parametrize(
65936606
'validator',
65946607
[

0 commit comments

Comments
 (0)