fastapi-pagination icon indicating copy to clipboard operation
fastapi-pagination copied to clipboard

Beanie Extension: ValidationError when using projection and aggregation

Open svituz opened this issue 9 months ago • 3 comments

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}]}},

svituz avatar Apr 03 '25 16:04 svituz

Hi @svituz,

We released new version 0.14.0, could you please check if this issue is still present?

uriyyo avatar Sep 03 '25 10:09 uriyyo

Hi @svituz ,

Sorry for bothering you, any updates?

uriyyo avatar Sep 27 '25 09:09 uriyyo

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.

svituz avatar Sep 30 '25 08:09 svituz