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] Revision Id is in Responsemodel #648

Closed
ForkGitAboutDre opened this issue Aug 9, 2023 · 9 comments
Closed

[BUG] Revision Id is in Responsemodel #648

ForkGitAboutDre opened this issue Aug 9, 2023 · 9 comments
Labels

Comments

@ForkGitAboutDre
Copy link

Describe the Bug

revision_id is included in genetated Responsebody, it is not the same as the generated Example Value, which does not include revision_id.

To Reproduce

class TestDocument(Document):
    somefield: int

app = FastApi()

app.post("/test/")
async def some_post_endpoint(test: Testdocument) -> TestDocument:
return await test.save()
curl -X 'POST' 'http://localhost:8000/test/ -H 'accept: application/json' -H 'Content-Type: application/json' -d {"some_field": 0}

Expected behavior

The response shouldnt include "revision_id": null

Additional context

fastapi version used: 0.101.0

@ForkGitAboutDre ForkGitAboutDre changed the title [BUG] [BUG] Revision Id is in Responsemodel Aug 9, 2023
@roman-right roman-right added the bug Something isn't working label Aug 10, 2023
@roman-right
Copy link
Member

Hi! Thank you for the catch. I'll check and fix this in the next bug-fixing session this or next week

@roman-right
Copy link
Member

This bug was fixed in #669. Please try

@roman-right roman-right removed the bug Something isn't working label Aug 22, 2023
@ForkGitAboutDre
Copy link
Author

Hmm for me it still says "revision_id": null

I checked out fix/august_2023, that should be the correct branch, right?

@roman-right
Copy link
Member

Yes, this is the correct branch. I'll doublecheck it

@roman-right
Copy link
Member

roman-right commented Sep 9, 2023

I reproduced it.
It looks like some of the cases are not under my control more.

When you use response model (or -> notation), FastAPI provides this model to Pydantic which uses it to parse the result to JSON on the Rust side.

You can fix this either using response_model_exclude parameter:

import motor.motor_asyncio

from beanie import Document, init_beanie
from fastapi import FastAPI


class TestDocument(Document):
    somefield: int = 1


app = FastAPI()


@app.on_event("startup")
async def app_init():
    client = motor.motor_asyncio.AsyncIOMotorClient("mongodb://beanie:beanie@localhost:27017")
    await init_beanie(
        client.beanie_db,
        document_models=[TestDocument],
    )


@app.post("/test/", response_model_exclude={"revision_id"})
async def some_post_endpoint(test: TestDocument) -> TestDocument:
    return await test.save()

or just not set response model:

import motor.motor_asyncio

from beanie import Document, init_beanie
from fastapi import FastAPI


class TestDocument(Document):
    somefield: int = 1


app = FastAPI()


@app.on_event("startup")
async def app_init():
    client = motor.motor_asyncio.AsyncIOMotorClient("mongodb://beanie:beanie@localhost:27017")
    await init_beanie(
        client.beanie_db,
        document_models=[TestDocument],
    )


@app.post("/test/")
async def some_post_endpoint(test: TestDocument):
    return await test.save()

@ForkGitAboutDre
Copy link
Author

Okay, makes sense.

Thanks for the in depth explanation :)

@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity.

@github-actions github-actions bot added the Stale label Oct 11, 2023
@github-actions
Copy link
Contributor

This issue was closed because it has been stalled for 14 days with no activity.

@giubaru
Copy link

giubaru commented Apr 2, 2024

Hi @roman-right! I was wondering if this case is consider as a bug as well, by default is returning revision_id=None
I'm using this version: beanie==1.25.0

from motor.motor_asyncio import AsyncIOMotorClient
from beanie import Document, init_beanie
import os

from dotenv import load_dotenv
load_dotenv()


class ArticleDoc(Document):
    title: str

    class Settings:
        name = 'article_entries'


async def init(conn_str: str = "mongodb://user:pass@host:27017"):
    client = AsyncIOMotorClient(os.environ.get("MONGODB_CONN_STR", conn_str))

    await init_beanie(
        database=client[os.environ.get("MONGODB_NAME", "dev")],
        document_models=[ArticleDoc]
    )


async def main() -> None:
    await init()
    articles = await ArticleDoc.find().to_list()
    print(articles[0])  # id=ObjectId('64cc9cb2d2523f3c6a397ff2') revision_id=None title='Taiwan sales surge in July'

if __name__ == '__main__':
    import asyncio
    asyncio.run(main())

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

No branches or pull requests

3 participants