Skip to content
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

[BUG] Custom types like bson.Binary require __get_pydantic_core_schema__ #651

Closed
Luc1412 opened this issue Aug 10, 2023 · 7 comments
Closed
Labels

Comments

@Luc1412
Copy link
Contributor

Luc1412 commented Aug 10, 2023

Describe the bug
With pydantic 2.0 custom types like in my case bson.Binary cause issues. You either need to create a config with arbitrary_types_allowed = True or implement __get_pydantic_core_schema__

This is the error:

pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'bson.binary.Binary'>. Set \`arbitrary_types_allowed=True\` in the model_config to ignore this error or implement \`__get_pydantic_core_schema__\` on your type to fully support it.

If you got this error by calling handler(<some type>) within `__get_pydantic_core_schema__` then you likely need to call `handler.generate_schema(<some type>)` since we do not call `__get_pydantic_core_schema__` on `<some type>` otherwise to avoid infinite recursion.

For further information visit https://errors.pydantic.dev/2.1.1/u/schema-for-unknown-type

To Reproduce

class ExampleEntry(BaseModel):
    example: bson.Binary

Expected behavior
It works without any issue

Additional context
Discord: https://ptb.discord.com/channels/822196934973456394/822196935435747332/1138966773044101190

@roman-right roman-right added the bug Something isn't working label Aug 10, 2023
@roman-right
Copy link
Owner

Hi! Thank you for the catch. I'll check it out in the next bug-fixing session this or next week

@roman-right
Copy link
Owner

The thing is Pydantic v2 needs some additional effort to support custom types.
I've added a type inherited from bson.Binary to Beanie. You can use it this way:

from beanie import BsonBinary

class Sample(Document):
    binary_field: BsonBinary

Please let me know if you have any issues with it.
It is implemented in the PR: #669

@roman-right roman-right removed the bug Something isn't working label Sep 9, 2023
@Luc1412
Copy link
Contributor Author

Luc1412 commented Sep 19, 2023

There is an issue with the subtype. When using BsonBinary the subtype seems to be always 0. Before it was properly set, in my case 6.

@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity.

@github-actions github-actions bot added the Stale label Oct 20, 2023
@Luc1412
Copy link
Contributor Author

Luc1412 commented Oct 20, 2023

@roman-right Any news regarding this?

@gsakkis
Copy link
Contributor

gsakkis commented Oct 20, 2023

Should have been fixed by #739

@Luc1412
Copy link
Contributor Author

Luc1412 commented Oct 20, 2023

Thanks, your PR solved this issue.

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

No branches or pull requests

3 participants