art049 / odmantic

Sync and Async ODM (Object Document Mapper) for MongoDB based on python type hints
http://art049.github.io/odmantic
ISC License
1.07k stars 94 forks source link

FastAPI schema can not show the fields in parent odmantic.Model class #358

Open SolardiaX opened 1 year ago

SolardiaX commented 1 year ago

Bug

# odmantic model
class User(odmantic.Model):
    username: str
    password: str

# model in fastapi for response
class CurrentUser(User):
    token: str

class LoginForm(pydantic.Model):
    username: str = pydantic.Field(description="Username")
    password: str = pydantic.Field(description="Password")

@router.post("/login", description="Method to login user",
             response_model=CurrentUser,
             response_model_exclude_none=True)
async def login(request: Request, db: AsyncEngine = Depends(get_engine), form = Body()) -> CurrentUser {
  ...
}

Current Behavior

The response model in FastAPI swagger for CurrentUser only has the token field, username and password in parent class is missing.

Expected behavior

The response model in FastAPI swagger for CurrentUser should contains username, password and token.

Environment

pydantic version: 1.10.9 pydantic compiled: True install path: ... python version: 3.11.3 (main, Apr 7 2023, 20:13:31) [Clang 14.0.0 (clang-1400.0.29.202)] platform: macOS-13.4-arm64-arm-64bit optional deps. installed: ['typing-extensions']

Additional context

tristantoupin commented 6 months ago

I've got the same behavior. Here's my classes:

from odmantic import Field, Model

class CoreModel(Model):
    created_at: int = Field(
        default_factory=lambda: int(time.time()),
        description="Unix timestamp when the user was created",
    )
    updated_at: int = Field(
        default_factory=lambda: int(time.time()),
        description="Unix timestamp of the last update",
    )

class User(CoreModel):
    first_name: str
    last_name: str
tristantoupin commented 6 months ago

@SolardiaX any fixes/workaround?