art049 / art049/odmantic

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

Open
#358 2 comments 1 reaction 0 assignees View on GitHub
bug
Dominant language
Python
Stars
1.2k
Forks
97
PR merge metrics
No merged PRs in 30d

Description

# Bug

```python
# 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

- ODMantic version: 0.9.2
- MongoDB version: ...
- Pydantic infos (output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())`):

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']

- Version of additional modules (if relevant):
- ...

**Additional context**

Contributor guide

Open the contributing guide

Research direction

Use the issue's User and CurrentUser models and the @router.post response_model example as the reproduction entry point. Inspect how ODMantic.Model inheritance is converted for FastAPI schema generation, then compare the generated Swagger response schema. Done means the schema for CurrentUser includes username, password, and token.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, mongodb, python
Domain
api, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.