Beanie Extension: ValidationError when using projection and aggregation
Using Beanie with MongoDB, I get a pydantic_core._pydantic_core.ValidationError when performing an AggretionQuery with a projection model.
My environment is:
python: 3.21
fastapi = { extras = ["all"], version = "^0.115.4" }
motor = "^3.6.0"
pydantic = "2.10.1"
pydantic-mongo = "^2.3.0"
beanie = "1.28.0"
fastapi-pagination = "0.12.34"
The problem seems to be in Beanie appending the $project pipeline at the end of the aggregation pipeline: return match_pipeline + self.aggregation_pipeline + projection_pipeline.
With pagination and projection, the aggregation pipeline results in:
[
...
{"$facet": {"metadata": [{"$count": "total"}], "data": [{"$limit": 2}, {"$skip": 0}]}},
{"$project": {'_id': 1, 'name': 1, 'type': 1}}, # assuming my model has name and type attributes
]
With this aggregation query, MongoDB returns [{}].
Then fastapi-pagination tries to map the results from mongo to the projection and it raises ValidationError with the required fields missing.
Notice that swapping the two pipelines would success
...
{"$project": {'_id': 1, 'name': 1, 'type': 1}},
{"$facet": {"metadata": [{"$count": "total"}], "data": [{"$limit": 2}, {"$skip": 0}]}},
Hi @svituz,
We released new version 0.14.0, could you please check if this issue is still present?
Hi @svituz ,
Sorry for bothering you, any updates?
Hi @uriyyo. Sorry, I didn't have the time to try the new version. I will let you know as soon as I try it.