Skip to content

Commit a79aa58

Browse files
fix: correctly deserialize blob length (#252)
* fix: correctly deserialize blob length * fix: tests * fix: support ext bundle 2-3 which uses length * fix: linting * test if neither value provided --------- Co-authored-by: hallvictoria <[email protected]>
1 parent 7683200 commit a79aa58

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

azure/functions/blob.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ def decode(cls, data: meta.Datum, *, trigger_metadata) -> Any:
113113
trigger_metadata, 'Properties', python_type=dict)
114114
if properties:
115115
blob_properties = properties
116-
length = properties.get('Length')
116+
length = properties.get('ContentLength') or \
117+
properties.get('Length')
117118
length = int(length) if length else None
118119
else:
119120
blob_properties = None

tests/test_blob.py

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_blob_input_with_metadata_no_blob_properties(self):
8484
self.assertEqual(result.metadata, None)
8585

8686
def test_blob_input_with_metadata_no_trigger_metadata(self):
87-
sample_blob_properties = '{"Length": "12"}'
87+
sample_blob_properties = '{"ContentLength": "12"}'
8888
datum: Datum = Datum(value=b'blob_content', type='bytes')
8989
trigger_metadata: Dict[str, Any] = {
9090
'Properties': Datum(sample_blob_properties, 'json'),
@@ -97,7 +97,7 @@ def test_blob_input_with_metadata_no_trigger_metadata(self):
9797
# Verify result metadata
9898
self.assertIsInstance(result, InputStream)
9999
self.assertEqual(result.name, 'blob_trigger_name')
100-
self.assertEqual(result.length, len(b'blob_content'))
100+
self.assertEqual(result.length, 12)
101101
self.assertEqual(result.uri, 'https://test.io/blob_trigger')
102102
self.assertEqual(result.blob_properties,
103103
json.loads(sample_blob_properties))
@@ -115,7 +115,7 @@ def test_blob_input_with_metadata_with_trigger_metadata(self):
115115
"LeaseStatus": 2,
116116
"LeaseState": 1,
117117
"LeaseDuration": 0,
118-
"Length": "12"
118+
"ContentLength": "12"
119119
}'''
120120
datum: Datum = Datum(value=b'blob_content', type='bytes')
121121
trigger_metadata: Dict[str, Any] = {
@@ -130,7 +130,7 @@ def test_blob_input_with_metadata_with_trigger_metadata(self):
130130
# Verify result metadata
131131
self.assertIsInstance(result, InputStream)
132132
self.assertEqual(result.name, 'blob_trigger_name')
133-
self.assertEqual(result.length, len(b'blob_content'))
133+
self.assertEqual(result.length, 12)
134134
self.assertEqual(result.uri, 'https://test.io/blob_trigger')
135135
self.assertEqual(result.blob_properties,
136136
json.loads(sample_blob_properties))
@@ -139,7 +139,7 @@ def test_blob_input_with_metadata_with_trigger_metadata(self):
139139

140140
def test_blob_input_with_metadata_with_incorrect_trigger_metadata(self):
141141
sample_metadata = 'Hello World'
142-
sample_blob_properties = '''{"Length": "12"}'''
142+
sample_blob_properties = '''{"ContentLength": "12"}'''
143143
datum: Datum = Datum(value=b'blob_content', type='bytes')
144144
trigger_metadata: Dict[str, Any] = {
145145
'Metadata': Datum(sample_metadata, 'string'),
@@ -153,7 +153,7 @@ def test_blob_input_with_metadata_with_incorrect_trigger_metadata(self):
153153
# Verify result metadata
154154
self.assertIsInstance(result, InputStream)
155155
self.assertEqual(result.name, 'blob_trigger_name')
156-
self.assertEqual(result.length, len(b'blob_content'))
156+
self.assertEqual(result.length, 12)
157157
self.assertEqual(result.uri, 'https://test.io/blob_trigger')
158158
self.assertEqual(result.blob_properties,
159159
json.loads(sample_blob_properties))
@@ -228,3 +228,46 @@ def read(self) -> Datum:
228228

229229
check_output_type = afb.BlobConverter.check_output_type_annotation
230230
self.assertTrue(check_output_type(CustomOutput))
231+
232+
def test_blob_input_with_metadata_with_length(self):
233+
sample_blob_properties = '{"Length": "12"}'
234+
datum: Datum = Datum(value=b'blob_content', type='bytes')
235+
trigger_metadata: Dict[str, Any] = {
236+
'Properties': Datum(sample_blob_properties, 'json')
237+
}
238+
result: InputStream = afb. \
239+
BlobConverter.decode(data=datum, trigger_metadata=trigger_metadata)
240+
241+
# Verify result metadata
242+
self.assertIsInstance(result, InputStream)
243+
self.assertEqual(result.length, 12)
244+
245+
def test_blob_input_with_metadata_with_both_length(self):
246+
sample_blob_properties = '''{
247+
"ContentLength": "12",
248+
"Length": "10"
249+
}'''
250+
datum: Datum = Datum(value=b'blob_content', type='bytes')
251+
trigger_metadata: Dict[str, Any] = {
252+
'Properties': Datum(sample_blob_properties, 'json')
253+
}
254+
result: InputStream = afb. \
255+
BlobConverter.decode(data=datum, trigger_metadata=trigger_metadata)
256+
257+
# Verify result metadata.
258+
# This should be 12, since we check for ContentLength first
259+
self.assertIsInstance(result, InputStream)
260+
self.assertEqual(result.length, 12)
261+
262+
def test_blob_input_with_metadata_with_no_length(self):
263+
sample_blob_properties = '''{}'''
264+
datum: Datum = Datum(value=b'blob_content', type='bytes')
265+
trigger_metadata: Dict[str, Any] = {
266+
'Properties': Datum(sample_blob_properties, 'json')
267+
}
268+
result: InputStream = afb. \
269+
BlobConverter.decode(data=datum, trigger_metadata=trigger_metadata)
270+
271+
# Verify result metadata.
272+
self.assertIsInstance(result, InputStream)
273+
self.assertEqual(result.length, None)

0 commit comments

Comments
 (0)