MongoEngine / MongoEngine/mongoengine

Document not working well with Fastapi & Pydantic

Open
#2,793 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

I am trying to integrate Mongoengine ORM with Fastapi but am experiencing an issue when trying to return routes:

demo model:

class ModelName(Document):
    name = StringField(required=True)
    title = StringField(required=True)

route integration:

router = APIRouter()

@router.get("")
def some_route():
    return ModelName.objects()

what is returned:

{
  "_cls": "ModelName"
}

When trying to fix the issue, I realized that pydantic tries serializing the document returned but fails due to the __dict__ method not returning serialized data properly. I also tried working with `.to_mongo()', but pydantic failed to serialize the ObjectID fields.

I read some posts on stack overflow and the suggested solution was to return .to_json() which can be returned as a JSON string, but due to the number of routes making use of this, it would be a hassle to update every single route to return .to_json, also the frontend dev would have to then parse all responses from the API back to JSON which is unnecessary

I ended up having to create an abstract document with a fix to the __dict__ method:

class CustomDocument(Document):
    meta = {"abstract": True}

    @property
    def __dict__(self):
        data = {}  # initialize an empty dictionary to store the converted data

        # iterate over key-value pairs in the to_mongo() result
        for key, value in self.to_mongo().items():
            # check if the value is an ObjectId and convert it to a string if necessary
            value = str(value) if isinstance(value, ObjectId) else value

            # update the dictionary with the key-value pair
            data.update({key: value})

        return data

class ModelName(CustomDocument):
    name = StringField(required=True)
    title = StringField(required=True)

returned:

[
    {
        "_id": "659be429314d05a57b5e7dad"
        "name": "some text",
        "title": "some text"
    }
]

I'd love to know if there is already a solution for this, or possibly one already being worked on. If there isn't, I'll create a pull request with a possible fix to make things easier.

Thank you.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the shown FastAPI APIRouter route that returns ModelName.objects(), then inspect how FastAPI and Pydantic handle MongoEngine Documents, dict, and to_mongo(). Compare the resulting response with the expected JSON containing _id, name, and title. Done should include a documented, compatible serialization behavior or a clearly scoped fix with regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, mongodb, python
Domain
api, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.