-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Fix JSON Schema generation of fields with plain validators in serialization mode #10167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Deploying pydantic-docs with
|
| Latest commit: |
7ae6890
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://f68dc72e.pydantic-docs.pages.dev |
| Branch Preview URL: | https://plain-val-ser.pydantic-docs.pages.dev |
CodSpeed Performance ReportMerging #10167 will not alter performanceComparing Summary
|
|
This changes the behavior of the added test in #10155. Consider the following example: from typing import Annotated
from pydantic import BaseModel, PlainValidator
class Model1(BaseModel):
pass
class Model2(BaseModel):
model1: Annotated[Model1, PlainValidator(lambda _: Model1())]
pydantic/pydantic/functional_validators.py Lines 163 to 178 in 752cdc9
With the addition of the {
'type': 'function-wrap',
'function': <function PlainValidator.__get_pydantic_core_schema__.<locals>.<lambda> at 0x712713441300>,
'schema': {
'type': 'definition-ref',
'schema_ref': '[...].Model1:101540811979568'
},
'return_schema': {
'type': 'definition-ref',
'schema_ref': '[...].Model1:101540811979568'
}
}Now when counting the refs of a schema, we (wrongly?) assume that having a reference count pydantic/pydantic/_internal/_core_utils.py Lines 446 to 454 in 823d51f
And it thus prevents the refs to be inlined. This changes the resulting core schema, to be a pydantic/pydantic/_internal/_core_utils.py Lines 509 to 512 in 823d51f
And the original issue disappears. I adapted the test to reproduce the original behavior (i.e. do not add the |
Thanks to the newly added `return_schema`, the core schema of `Model2` becomes a `'definitions'` schema, which no longer reproduces the issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great - thanks for breaking this change out into a smaller PR!
As per #10094 (comment).
Change Summary
Related issue number
Checklist