geldata / geldata/gel-python

Model variants aren't working as FastAPI `response_model`

Open
#801 0 comments 0 reactions 0 assignees View on GitHub
bug ORM
Dominant language
Python
Stars
415
Forks
50
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
Using a model derived from a `__variant__` as `response_model` causes `object has no attribute '__gel_changed_fields__'` PydanticSerializationError.

**Reproduction**

1. create a new venv
2. `pip install "fastapi[standard]"`
3. for the gel-python master branch `pip install -Uve ".[test]"`
4. `gel init`
5. Use this schema:
```edgeql
module default {
type User {
required name: str {
constraint exclusive;
}
multi friends: User;
}
}
```
5. `gel migration create`
6. `gel migrate`
7. `gel query 'insert User{name := "Alice"}'`
8. `gel generate py/models` (this is so FastAPI code has models to import)
9. create `main.py`
```python
import fastapi
import gel.fastapi
import uuid

from pydantic import BaseModel
from models import default

app = fastapi.FastAPI()
g = gel.fastapi.gelify(app)


class MyUser(BaseModel):
id: uuid.UUID
name: str
email: str


class BaseUser(default.User.__variants__.Base):
name: default.User.__typeof__.name
email: default.User.__typeof__.email


@app.get("/myuser/", response_model=list[MyUser])
async def get_users(name: str):
db = g.client
q = default.User.filter(
name=name,
)

return await db.query(q)


@app.get("/baseuser/", response_model=list[BaseUser])
async def broken_get_users(name: str):
db = g.client
q = default.User.filter(
name=name,
)

return await db.query(q)
```

Try these 2 endpoints:
- ` curl -X 'GET' 'http://127.0.0.1:8000/myuser/?name=Alice' -H 'accept: application/json'`
- ` curl -X 'GET' 'http://127.0.0.1:8000/baseuser/?name=Alice' -H 'accept: application/json'`

The baseuser one should cause an error:
```
...
pydantic_core._pydantic_core.PydanticSerializationError: Error calling function ``: AttributeError: 'BaseUser' object has no attribute '__gel_changed_fields__'
```

**Expected behavior**
There should not be any difference in how these endpoints work.

**Versions (please complete the following information):**

- OS: Ubuntu
- EdgeDB version: Gel 6.9+77bc1f3
- EdgeDB CLI version: repl 7.7.0+3788f53
- `gel-python` version: master (c7aea17d877961ae81b1e661d66746aadab1ffff)
- Python version: 3.12.9

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.