-
-
Notifications
You must be signed in to change notification settings - Fork 753
✨ Field()
supports json_schema_extra
for Pydantic v2
#1035
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
base: main
Are you sure you want to change the base?
Conversation
Any news about this PR? |
Any possibility of a merge @tiangolo ? I would also be interested in this support being added / long term. |
Please merge this. |
Field()
supports json_schema_extra
for Pydantic v2
Field()
supports json_schema_extra
for Pydantic v2
can you merge this? |
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.
@KimigaiiWuyi, thanks for your interest and efforts!
Currently people use schema_extra
as a workaround to pass parameters to Field
that are accepted by Pydantic's Field
, but not accepted by SQLModel's Field
.
As an example:
class A(SQLModel):
id_: int = Field(schema_extra={"validation_alias": "id"})
With pure Pydantic you would just pass it directly:
class A(BaseModel):
id_: int = Field(validation_alias="id")
But SQLModel's Field
doesn't have such parameter and we need to use that workaround.
Your implementation treats schema_extra
as a duplicate of json_schema_extra
and overrides it if both are present.
But it may lead to issues in this case (stupid example just to demonstrate the idea):
class A(SQLModel):
id_: int = Field(schema_extra={"validation_alias": "id"}, json_schema_extra={"type": "number"})
In the example above, the validation_alias
will be ignored with your implementation.
In this PR we can just modify it to add json_schema_extra
to json_extra
:
current_schema_extra = schema_extra or {}
if json_schema_extra:
current_schema_extra["json_schema_extra"] = json_schema_extra
In long perspective we need to deprecate schema_extra
parameter and introduce something like pd_extra_params
(Pydantic extra parameters) parameter so that people would be able to pass parameters not accepted by SQLModel's Field
Also, we need to add test to verify that new json_schema_extra
field works (fields are added to json schema)
Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com>
Indeed, you are correct. Although this situation is rare, it must be taken into consideration. |
According to what this link mentions,
PydanticV1's Field class supported passing arbitrary keyword arguments to the JSON schema. In SQLModel counterpart, it was supported by passing schema_extra (see this).
PydanticV2 still supports passing extra things to the JSON schema but in a slightly different way. One has to pass a dictionary in the json_schema_extra argument (see this).
So I modified the Field's parameters to be compatible and consistent with the
Pydantic v2
parameters, and for backward compatibility, I didn't remove theschema_extra
field, which is logically compatible with both theschema_extra
andjson_schema_extra
passes, andschema_extra
should be deprecated at some point in the future. field at some point in the future, but for nowschema_extra
should be retained.This Pull Reqeust is easy and simple, and easy to verify.