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

__bson__ serialization does not work when Optional / Union used with Custom Types #477

Open
nikhildigde opened this issue May 16, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@nikhildigde
Copy link

Bug

A clear and concise description of what the bug is.
I have a requirement to create a custom type and implement the bson ser method. It works well when used normally, but if used with an Optional / Union , it dosent work. In this case, the internal logic considers the field type as a Union.

Current Behavior

... Steps to reproduce the bug ...
`class EncryptedString(str):

@classmethod
def __get_validators__(cls):
    yield cls.validate

@classmethod
def validate(cls, v):
    if isinstance(v, bytes):
        return v.decode("utf-8")
    if not isinstance(v, str): 
        raise TypeError("Only string type is allowed")
     
    return v

@classmethod
def __bson__(cls, v) -> str:
    encrypted_data = v.encode("utf-8")
    return encrypted_data

class Model1(EmbeddedModel):
key1: Optional[EncryptedString] = None # DOSNT WORK

class EncryptedModel(Model):
encrypted_field: EncryptedString #WORKS WELL
embedded_model: List[Union[Model1]] = [] #DOSNT WORK
non_list_union: Model1`

Expected behavior

... A clear and concise description of what you expected to happen. ...

I expect the bson method to be invoked in every case for a custom type

Environment

  • ODMantic version: 0.9.2
...
  • Version of additional modules (if relevant):
    • ...

Additional context

Add any other context about the problem here.

@nikhildigde nikhildigde added the bug Something isn't working label May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant