From d95a2fdb250f0b91015901b8f8206664a67a2c40 Mon Sep 17 00:00:00 2001 From: Yun Zheng Hu Date: Mon, 11 Aug 2025 20:41:26 +0000 Subject: [PATCH 1/3] Fix JsonRecordPacker when bytes field is None --- flow/record/jsonpacker.py | 3 ++- tests/packer/test_json_packer.py | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/flow/record/jsonpacker.py b/flow/record/jsonpacker.py index 0984446e..6f1d85c6 100644 --- a/flow/record/jsonpacker.py +++ b/flow/record/jsonpacker.py @@ -97,7 +97,8 @@ def unpack_obj(self, obj: Any) -> RecordDescriptor | Record | Any: del obj["_type"] for field_type, field_name in record_descriptor.get_field_tuples(): if field_type == "bytes": - obj[field_name] = base64.b64decode(obj[field_name]) + value = obj[field_name] + obj[field_name] = base64.b64decode(value) if value is not None else None return record_descriptor.recordType(**obj) if _type == "recorddescriptor": data = obj["_data"] diff --git a/tests/packer/test_json_packer.py b/tests/packer/test_json_packer.py index 291f7416..7a139c5a 100644 --- a/tests/packer/test_json_packer.py +++ b/tests/packer/test_json_packer.py @@ -112,3 +112,29 @@ def test_record_pack_surrogateescape() -> None: # pack the json string back to a record and make sure it is the same as before assert packer.unpack(data) == record + + +def test_json_packer_bytes_type() -> None: + TestRecord = RecordDescriptor( + "test/bytes", + [ + ("bytes", "data"), + ], + ) + + packer = JsonRecordPacker() + + record = TestRecord(b"hello world") + data = packer.pack(record) + assert data.startswith('{"data": "aGVsbG8gd29ybGQ="') + assert packer.unpack(data) == record + + record = TestRecord(data=None) + data = packer.pack(record) + assert data.startswith('{"data": null') + assert packer.unpack(data) == record + + record = TestRecord(data=b"") + data = packer.pack(record) + assert data.startswith('{"data": ""') + assert packer.unpack(data) == record From e4e7985e8da854e6320667a2a8589815fa2ad1bd Mon Sep 17 00:00:00 2001 From: Yun Zheng Hu Date: Mon, 11 Aug 2025 20:52:32 +0000 Subject: [PATCH 2/3] Fix DeprecationWarning in sqlite test Fixes DeprecationWarning: The default datetime adapter is deprecated as of Python 3.12. --- tests/adapter/test_sqlite_duckdb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/adapter/test_sqlite_duckdb.py b/tests/adapter/test_sqlite_duckdb.py index 51f82dea..956caa30 100644 --- a/tests/adapter/test_sqlite_duckdb.py +++ b/tests/adapter/test_sqlite_duckdb.py @@ -173,7 +173,7 @@ def test_read_from_sqlite(tmp_path: Path, db: Database) -> None: """ INSERT INTO 'test/record' VALUES (?, ?, ?, ?) """, - (f"record{i}", f"foobar{i}".encode(), datetime(2023, 10, i, 13, 37, tzinfo=timezone.utc), 3.14 + i), + (f"record{i}", f"foobar{i}".encode(), datetime(2023, 10, i, 13, 37, tzinfo=timezone.utc).isoformat(), 3.14 + i), ) # Read the SQLite database using flow.record From a396a9c9612794fde7ffcd78aecd6f9177db4a7f Mon Sep 17 00:00:00 2001 From: Yun Zheng Hu Date: Mon, 11 Aug 2025 21:20:27 +0000 Subject: [PATCH 3/3] Fix lint --- tests/adapter/test_sqlite_duckdb.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/adapter/test_sqlite_duckdb.py b/tests/adapter/test_sqlite_duckdb.py index 956caa30..6a5ab26d 100644 --- a/tests/adapter/test_sqlite_duckdb.py +++ b/tests/adapter/test_sqlite_duckdb.py @@ -169,11 +169,12 @@ def test_read_from_sqlite(tmp_path: Path, db: Database) -> None: """ ) for i in range(1, 30): + dt_isoformat = datetime(2023, 10, i, 13, 37, tzinfo=timezone.utc).isoformat() con.execute( """ INSERT INTO 'test/record' VALUES (?, ?, ?, ?) """, - (f"record{i}", f"foobar{i}".encode(), datetime(2023, 10, i, 13, 37, tzinfo=timezone.utc).isoformat(), 3.14 + i), + (f"record{i}", f"foobar{i}".encode(), dt_isoformat, 3.14 + i), ) # Read the SQLite database using flow.record