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] Empty BackLink for Optional[BackLink[ADoc]] when document has no back-link #920

Open
b-simjoo opened this issue Apr 21, 2024 · 1 comment

Comments

@b-simjoo
Copy link

b-simjoo commented Apr 21, 2024

Describe the bug
Hi, I'm using FastAPI and Beanie and I have to set of models, one using Beanie for database and other one for API schema. The database has two documents Employee and Department that are linked :

# db.py
class Department(Document):
   id:str
   employees:list[Link["Employee"]]
   ...

class Employee(Document):
   id:str
   department:Optional[BackLink[Department]] = field(original_field="employees", default=None)
   ...

On the other side I have proper Models for both:

# schema.py
class Department(BaseModel):
    id:str
    ...

class Employee(BaseModel):
    id:str
    department:Optional[Department]
    ...

Since I'm using FastAPI, when I return a data instance, it tries to convert from db to schema (using pydantic method Model.model_validate(v,from_attributes=True)).

@router.get("/{emp_id}")
async def get_employee(emp_id: Annotated[str, Path()]) -> schema.Employee:
    return await db.Employee.get(emp_id, fetch_links=True)

Everything works just fine until I try to get an employee that no Department is linked to. Then Employee.department field is an empty BackLink that is not validate or processable for pydantic so it raises a Validation Error:

{'type': 'missing', 'loc': ('response', 'department', 'id'), 'msg': 'Field required', 'input': <beanie.odm.fields.BackLink object at 0x70d990ed0110>, 'url': 'https://errors.pydantic.dev/2.5/v/missing'}

Expected behavior
for fields of type Optional[BackLink[ADoc]] set/return None when there is no back-link.

@b-simjoo b-simjoo changed the title [BUG] Empty BackLink for Optional[BackLink[...]] when document has no back-link [BUG] Empty BackLink for Optional[BackLink[ADoc]] when document has no back-link Apr 21, 2024
@b-simjoo
Copy link
Author

b-simjoo commented Apr 21, 2024

For now I added a pydantic validator on my db model (db.Employee) that fixes the issue

class Employee(Document):
    id:str
    department:Optional[Department]
    @field_validator("department")
    @classmethod
    def validate_backlink(cls, v):
        if isinstance(v, BackLink):
            return None
        return v

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

No branches or pull requests

1 participant