Skip to content

Commit ade847c

Browse files
Fix display of binary data (#759)
1 parent 349df58 commit ade847c

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

aiohttp_admin/backends/abc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def default(self, o: object) -> Any:
3838
return str(o)
3939
if isinstance(o, Enum):
4040
return o.value
41+
if isinstance(o, bytes):
42+
return o.decode(errors="replace")
4143

4244
return super().default(o)
4345

tests/test_backends_sqlalchemy.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,47 @@ def test_table(mock_engine: AsyncEngine) -> None:
6666
}
6767

6868

69+
async def test_binary(
70+
base: DeclarativeBase, aiohttp_client: Callable[[web.Application], Awaitable[TestClient]],
71+
login: _Login
72+
) -> None:
73+
class TestModel(base): # type: ignore[misc,valid-type]
74+
__tablename__ = "test"
75+
id: Mapped[int] = mapped_column(primary_key=True)
76+
binary: Mapped[bytes]
77+
78+
app = web.Application()
79+
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
80+
db = async_sessionmaker(engine, expire_on_commit=False)
81+
async with engine.begin() as conn:
82+
await conn.run_sync(base.metadata.create_all)
83+
async with db.begin() as sess:
84+
sess.add(TestModel(binary=b"foo"))
85+
sess.add(TestModel(binary=b"\x01\xFF\x02"))
86+
87+
schema: aiohttp_admin.Schema = {
88+
"security": {
89+
"check_credentials": check_credentials,
90+
"secure": False
91+
},
92+
"resources": ({"model": SAResource(engine, TestModel)},)
93+
}
94+
app["admin"] = aiohttp_admin.setup(app, schema)
95+
96+
admin_client = await aiohttp_client(app)
97+
assert admin_client.app
98+
h = await login(admin_client)
99+
100+
url = app["admin"].router["test_get_one"].url_for()
101+
async with admin_client.get(url, params={"id": 1}, headers=h) as resp:
102+
assert resp.status == 200
103+
assert await resp.json() == {"data": {"id": 1, "binary": "foo"}}
104+
105+
async with admin_client.get(url, params={"id": 2}, headers=h) as resp:
106+
assert resp.status == 200
107+
assert await resp.json() == {"data": {"id": 2, "binary": "\x01\x02"}}
108+
109+
69110
def test_fk(base: type[DeclarativeBase], mock_engine: AsyncEngine) -> None:
70111
class TestModel(base): # type: ignore[misc,valid-type]
71112
__tablename__ = "dummy"

0 commit comments

Comments
 (0)