From 147b606f04ff647165e478eef8040f881822221d Mon Sep 17 00:00:00 2001 From: JSCU-CNI <121175071+JSCU-CNI@users.noreply.github.com> Date: Thu, 1 Aug 2024 09:45:48 +0200 Subject: [PATCH 1/3] cast boolean fields to bool --- flow/record/jsonpacker.py | 7 ++++++- tests/test_json_packer.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/flow/record/jsonpacker.py b/flow/record/jsonpacker.py index 9004482..216f4b2 100644 --- a/flow/record/jsonpacker.py +++ b/flow/record/jsonpacker.py @@ -41,15 +41,20 @@ def pack_obj(self, obj): if obj._desc.identifier not in self.descriptors: self.register(obj._desc, True) serial = obj._asdict() + if self.pack_descriptors: serial["_type"] = "record" serial["_recorddescriptor"] = obj._desc.identifier - # PYTHON2: Because "bytes" are also "str" we have to handle this here for field_type, field_name in obj._desc.get_field_tuples(): + # PYTHON2: Because "bytes" are also "str" we have to handle this here if field_type == "bytes" and isinstance(serial[field_name], str): serial[field_name] = base64.b64encode(serial[field_name]).decode() + # Boolean field types should be case to a bool instead of staying ints + elif field_type == "boolean" and isinstance(serial[field_name], int): + serial[field_name] = bool(serial[field_name]) + return serial if isinstance(obj, RecordDescriptor): serial = { diff --git a/tests/test_json_packer.py b/tests/test_json_packer.py index 8b6119d..9ff98ee 100644 --- a/tests/test_json_packer.py +++ b/tests/test_json_packer.py @@ -69,3 +69,18 @@ def test_record_descriptor_not_found(): packer = JsonRecordPacker() with pytest.raises(RecordDescriptorNotFound, match="No RecordDescriptor found for: .*test/descriptor_not_found"): packer.unpack(data) + + +def test_record_pack_bool_regression() -> None: + TestRecord = RecordDescriptor( + "test/record_pack_bool", + [ + ("varint", "some_varint"), + ("uint16", "some_uint"), + ("boolean", "some_boolean"), + ], + ) + + packer = JsonRecordPacker() + data = packer.pack(TestRecord(some_varint=1, some_uint=0, some_boolean=False)) + assert data.startswith('{"some_varint": 1, "some_uint": 0, "some_boolean": false, ') From 3ebbc4e4b98f04ee60f0c55c9f6257aac4b05ead Mon Sep 17 00:00:00 2001 From: JSCU-CNI <121175071+JSCU-CNI@users.noreply.github.com> Date: Thu, 1 Aug 2024 09:56:57 +0200 Subject: [PATCH 2/3] also test unpack --- tests/test_json_packer.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/test_json_packer.py b/tests/test_json_packer.py index 9ff98ee..2ea2883 100644 --- a/tests/test_json_packer.py +++ b/tests/test_json_packer.py @@ -81,6 +81,12 @@ def test_record_pack_bool_regression() -> None: ], ) + record = TestRecord(some_varint=1, some_uint=0, some_boolean=False) packer = JsonRecordPacker() - data = packer.pack(TestRecord(some_varint=1, some_uint=0, some_boolean=False)) + + # pack to json string and check if some_boolean is false instead of 0 + data = packer.pack(record) assert data.startswith('{"some_varint": 1, "some_uint": 0, "some_boolean": false, ') + + # pack the json string back to a record and make sure it is the same as before + assert packer.unpack(data) == record From 9bfb62a33dd47d8afad27f203741a2699667fd27 Mon Sep 17 00:00:00 2001 From: Computer Network Investigation <121175071+JSCU-CNI@users.noreply.github.com> Date: Thu, 1 Aug 2024 11:45:30 +0200 Subject: [PATCH 3/3] Update flow/record/jsonpacker.py Co-authored-by: Erik Schamper <1254028+Schamper@users.noreply.github.com> --- flow/record/jsonpacker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flow/record/jsonpacker.py b/flow/record/jsonpacker.py index 216f4b2..96c646e 100644 --- a/flow/record/jsonpacker.py +++ b/flow/record/jsonpacker.py @@ -51,7 +51,7 @@ def pack_obj(self, obj): if field_type == "bytes" and isinstance(serial[field_name], str): serial[field_name] = base64.b64encode(serial[field_name]).decode() - # Boolean field types should be case to a bool instead of staying ints + # Boolean field types should be cast to a bool instead of staying ints elif field_type == "boolean" and isinstance(serial[field_name], int): serial[field_name] = bool(serial[field_name])