From a5f8d849b992d23a5b61a92909a9a91b9409aaa4 Mon Sep 17 00:00:00 2001 From: peterstone2017 <12449837+YunchuWang@users.noreply.github.com> Date: Sun, 6 Oct 2024 16:52:33 -0700 Subject: [PATCH 1/5] fix: correctly deserialize blob length --- azure/functions/blob.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure/functions/blob.py b/azure/functions/blob.py index b75500ab..efdca506 100644 --- a/azure/functions/blob.py +++ b/azure/functions/blob.py @@ -113,7 +113,7 @@ def decode(cls, data: meta.Datum, *, trigger_metadata) -> Any: trigger_metadata, 'Properties', python_type=dict) if properties: blob_properties = properties - length = properties.get('Length') + length = properties.get('ContentLength') length = int(length) if length else None else: blob_properties = None From 58de3dbd93c1c425926a14e6e3f9de7087aa694a Mon Sep 17 00:00:00 2001 From: wangbill <12449837+YunchuWang@users.noreply.github.com> Date: Mon, 7 Oct 2024 10:27:08 -0700 Subject: [PATCH 2/5] fix: tests --- tests/test_blob.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_blob.py b/tests/test_blob.py index 3d9ed846..ccb43a55 100644 --- a/tests/test_blob.py +++ b/tests/test_blob.py @@ -84,7 +84,7 @@ def test_blob_input_with_metadata_no_blob_properties(self): self.assertEqual(result.metadata, None) def test_blob_input_with_metadata_no_trigger_metadata(self): - sample_blob_properties = '{"Length": "12"}' + sample_blob_properties = '{"ContentLength": "12"}' datum: Datum = Datum(value=b'blob_content', type='bytes') trigger_metadata: Dict[str, Any] = { 'Properties': Datum(sample_blob_properties, 'json'), @@ -115,7 +115,7 @@ def test_blob_input_with_metadata_with_trigger_metadata(self): "LeaseStatus": 2, "LeaseState": 1, "LeaseDuration": 0, - "Length": "12" + "ContentLength": "12" }''' datum: Datum = Datum(value=b'blob_content', type='bytes') trigger_metadata: Dict[str, Any] = { @@ -139,7 +139,7 @@ def test_blob_input_with_metadata_with_trigger_metadata(self): def test_blob_input_with_metadata_with_incorrect_trigger_metadata(self): sample_metadata = 'Hello World' - sample_blob_properties = '''{"Length": "12"}''' + sample_blob_properties = '''{"ContentLength": "12"}''' datum: Datum = Datum(value=b'blob_content', type='bytes') trigger_metadata: Dict[str, Any] = { 'Metadata': Datum(sample_metadata, 'string'), From 6e5bdd20266affe665eccfc90a5aa9ad6bf28cec Mon Sep 17 00:00:00 2001 From: peterstone2017 <12449837+YunchuWang@users.noreply.github.com> Date: Tue, 8 Oct 2024 09:08:49 -0700 Subject: [PATCH 3/5] fix: support ext bundle 2-3 which uses length --- azure/functions/blob.py | 2 +- tests/test_blob.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/azure/functions/blob.py b/azure/functions/blob.py index efdca506..d1ef5e5f 100644 --- a/azure/functions/blob.py +++ b/azure/functions/blob.py @@ -113,7 +113,7 @@ def decode(cls, data: meta.Datum, *, trigger_metadata) -> Any: trigger_metadata, 'Properties', python_type=dict) if properties: blob_properties = properties - length = properties.get('ContentLength') + length = properties.get('ContentLength') or properties.get('Length') length = int(length) if length else None else: blob_properties = None diff --git a/tests/test_blob.py b/tests/test_blob.py index ccb43a55..c619ffb2 100644 --- a/tests/test_blob.py +++ b/tests/test_blob.py @@ -228,3 +228,32 @@ def read(self) -> Datum: check_output_type = afb.BlobConverter.check_output_type_annotation self.assertTrue(check_output_type(CustomOutput)) + + def test_blob_input_with_metadata_with_length(self): + sample_blob_properties = '{"Length": "12"}' + datum: Datum = Datum(value=b'blob_content', type='bytes') + trigger_metadata: Dict[str, Any] = { + 'Properties': Datum(sample_blob_properties, 'json') + } + result: InputStream = afb. \ + BlobConverter.decode(data=datum, trigger_metadata=trigger_metadata) + + # Verify result metadata + self.assertIsInstance(result, InputStream) + self.assertEqual(result.length, len(b'blob_content')) + + def test_blob_input_with_metadata_with_both_length(self): + sample_blob_properties = '''{ + "ContentLength": "12", + "Length": "10" + }''' + datum: Datum = Datum(value=b'blob_content', type='bytes') + trigger_metadata: Dict[str, Any] = { + 'Properties': Datum(sample_blob_properties, 'json') + } + result: InputStream = afb. \ + BlobConverter.decode(data=datum, trigger_metadata=trigger_metadata) + + # Verify result metadata + self.assertIsInstance(result, InputStream) + self.assertEqual(result.length, len(b'blob_content')) \ No newline at end of file From 7527878de6a541411b0ec272e95ac69c75ce438d Mon Sep 17 00:00:00 2001 From: peterstone2017 <12449837+YunchuWang@users.noreply.github.com> Date: Tue, 8 Oct 2024 10:47:39 -0700 Subject: [PATCH 4/5] fix: linting --- azure/functions/blob.py | 3 ++- tests/test_blob.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/azure/functions/blob.py b/azure/functions/blob.py index d1ef5e5f..7c34f1a9 100644 --- a/azure/functions/blob.py +++ b/azure/functions/blob.py @@ -113,7 +113,8 @@ def decode(cls, data: meta.Datum, *, trigger_metadata) -> Any: trigger_metadata, 'Properties', python_type=dict) if properties: blob_properties = properties - length = properties.get('ContentLength') or properties.get('Length') + length = properties.get('ContentLength') or \ + properties.get('Length') length = int(length) if length else None else: blob_properties = None diff --git a/tests/test_blob.py b/tests/test_blob.py index c619ffb2..761281a9 100644 --- a/tests/test_blob.py +++ b/tests/test_blob.py @@ -256,4 +256,4 @@ def test_blob_input_with_metadata_with_both_length(self): # Verify result metadata self.assertIsInstance(result, InputStream) - self.assertEqual(result.length, len(b'blob_content')) \ No newline at end of file + self.assertEqual(result.length, len(b'blob_content')) From d489376c91800a98cea30807b475288bad9471b4 Mon Sep 17 00:00:00 2001 From: hallvictoria Date: Thu, 17 Oct 2024 10:57:43 -0500 Subject: [PATCH 5/5] test if neither value provided --- tests/test_blob.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/tests/test_blob.py b/tests/test_blob.py index 761281a9..adb1f16a 100644 --- a/tests/test_blob.py +++ b/tests/test_blob.py @@ -97,7 +97,7 @@ def test_blob_input_with_metadata_no_trigger_metadata(self): # Verify result metadata self.assertIsInstance(result, InputStream) self.assertEqual(result.name, 'blob_trigger_name') - self.assertEqual(result.length, len(b'blob_content')) + self.assertEqual(result.length, 12) self.assertEqual(result.uri, 'https://test.io/blob_trigger') self.assertEqual(result.blob_properties, json.loads(sample_blob_properties)) @@ -130,7 +130,7 @@ def test_blob_input_with_metadata_with_trigger_metadata(self): # Verify result metadata self.assertIsInstance(result, InputStream) self.assertEqual(result.name, 'blob_trigger_name') - self.assertEqual(result.length, len(b'blob_content')) + self.assertEqual(result.length, 12) self.assertEqual(result.uri, 'https://test.io/blob_trigger') self.assertEqual(result.blob_properties, json.loads(sample_blob_properties)) @@ -153,7 +153,7 @@ def test_blob_input_with_metadata_with_incorrect_trigger_metadata(self): # Verify result metadata self.assertIsInstance(result, InputStream) self.assertEqual(result.name, 'blob_trigger_name') - self.assertEqual(result.length, len(b'blob_content')) + self.assertEqual(result.length, 12) self.assertEqual(result.uri, 'https://test.io/blob_trigger') self.assertEqual(result.blob_properties, json.loads(sample_blob_properties)) @@ -240,7 +240,7 @@ def test_blob_input_with_metadata_with_length(self): # Verify result metadata self.assertIsInstance(result, InputStream) - self.assertEqual(result.length, len(b'blob_content')) + self.assertEqual(result.length, 12) def test_blob_input_with_metadata_with_both_length(self): sample_blob_properties = '''{ @@ -254,6 +254,20 @@ def test_blob_input_with_metadata_with_both_length(self): result: InputStream = afb. \ BlobConverter.decode(data=datum, trigger_metadata=trigger_metadata) - # Verify result metadata + # Verify result metadata. + # This should be 12, since we check for ContentLength first self.assertIsInstance(result, InputStream) - self.assertEqual(result.length, len(b'blob_content')) + self.assertEqual(result.length, 12) + + def test_blob_input_with_metadata_with_no_length(self): + sample_blob_properties = '''{}''' + datum: Datum = Datum(value=b'blob_content', type='bytes') + trigger_metadata: Dict[str, Any] = { + 'Properties': Datum(sample_blob_properties, 'json') + } + result: InputStream = afb. \ + BlobConverter.decode(data=datum, trigger_metadata=trigger_metadata) + + # Verify result metadata. + self.assertIsInstance(result, InputStream) + self.assertEqual(result.length, None)