Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

KimigaiiWuyi
Copy link

@KimigaiiWuyi KimigaiiWuyi commented Jul 26, 2024

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).

Field no longer supports arbitrary keyword arguments to be added to the JSON schema. Instead, any extra data you want to add to the JSON schema should be passed as a dictionary to the json_schema_extra keyword argument.

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 the schema_extra field, which is logically compatible with both the schema_extra and json_schema_extra passes, and schema_extra should be deprecated at some point in the future. field at some point in the future, but for now schema_extra should be retained.

This Pull Reqeust is easy and simple, and easy to verify.

@Kaplas85
Copy link

Any news about this PR?

@ndeybach
Copy link

ndeybach commented Dec 26, 2024

Any possibility of a merge @tiangolo ? I would also be interested in this support being added / long term.

@paulomtts
Copy link

Please merge this.

@svlandeg svlandeg changed the title 🎨 Field() supports json_schema_extra for Pydantic v2 Field() supports json_schema_extra for Pydantic v2 Feb 24, 2025
@svlandeg svlandeg added the feature New feature or request label Feb 24, 2025
@arnoldasjan
Copy link

can you merge this?

Copy link

@YuriiMotov YuriiMotov left a 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>
@KimigaiiWuyi
Copy link
Author

@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)

Indeed, you are correct. Although this situation is rare, it must be taken into consideration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request waiting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants