Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions aiohttp_admin/backends/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections.abc import Sequence
from datetime import date, datetime, time
from enum import Enum
from uuid import UUID
from functools import cached_property, partial
from types import MappingProxyType
from typing import Any, Generic, Literal, Optional, TypeVar, final
Expand Down Expand Up @@ -47,6 +48,8 @@ def default(self, o: object) -> Any:
return o.value
if isinstance(o, bytes):
return o.decode(errors="replace")
if isinstance(o, UUID):
return str(o)

return super().default(o)

Expand Down
40 changes: 40 additions & 0 deletions tests/test_backends_sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import uuid
from collections.abc import Awaitable, Callable
from datetime import date, datetime
from typing import Optional, Union
Expand Down Expand Up @@ -139,6 +140,45 @@ class TestModel(base): # type: ignore[misc,valid-type]
assert await resp.json() == {"data": {"id": "2", "data": {"id": 2, "binary": "\x01�\x02"}}}


async def test_uuid(
base: DeclarativeBase, aiohttp_client: Callable[[web.Application], Awaitable[_Client]],
login: _Login
) -> None:
class TestModel(base): # type: ignore[misc,valid-type]
__tablename__ = "test"
id: Mapped[int] = mapped_column(primary_key=True)
uid: Mapped[uuid.UUID]

app = web.Application()
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
db = async_sessionmaker(engine, expire_on_commit=False)
async with engine.begin() as conn:
await conn.run_sync(base.metadata.create_all)
async with db.begin() as sess:
sess.add(TestModel(uid=uuid.UUID("12345678-1234-5678-1234-567812345678")))

schema: aiohttp_admin.Schema = {
"security": {
"check_credentials": check_credentials,
"secure": False
},
"resources": ({"model": SAResource(engine, TestModel)},)
}
app[admin] = aiohttp_admin.setup(app, schema)

admin_client = await aiohttp_client(app)
assert admin_client.app
h = await login(admin_client)

# UUID values must serialize (Encoder previously raised TypeError -> 500).
url = app[admin].router["test_get_one"].url_for()
async with admin_client.get(url, params={"id": 1}, headers=h) as resp:
assert resp.status == 200
assert await resp.json() == {
"data": {"id": "1",
"data": {"id": 1, "uid": "12345678-1234-5678-1234-567812345678"}}}


def test_fk(base: type[DeclarativeBase], mock_engine: AsyncEngine) -> None:
class TestModel(base): # type: ignore[misc,valid-type]
__tablename__ = "dummy"
Expand Down
Loading