Skip to content

Commit 643f273

Browse files
Fix multipart form hook test failures
- Move forms import to module level to allow proper test patching - Fix _is_file_field_data method to exclude empty strings and None values - Add filename validation to distinguish JSON content from file content - All tests now pass including edge cases for file field detection
1 parent 12b11e2 commit 643f273

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/glean/api_client/_hooks/multipart_fix_hook.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Any, Dict, List, Tuple
44
from .types import SDKInitHook
55
from glean.api_client.httpclient import HttpClient
6+
from glean.api_client.utils import forms
67

78

89
class MultipartFileFieldFixHook(SDKInitHook):
@@ -23,8 +24,6 @@ def sdk_init(self, base_url: str, client: HttpClient) -> Tuple[str, HttpClient]:
2324

2425
def _patch_multipart_serialization(self):
2526
"""Patch the serialize_multipart_form function to fix file field names."""
26-
from glean.api_client.utils import forms
27-
2827
# Store reference to original function
2928
original_serialize_multipart_form = forms.serialize_multipart_form
3029

@@ -71,9 +70,18 @@ def _is_file_field_data(self, file_data: Any) -> bool:
7170
"""
7271
if isinstance(file_data, tuple) and len(file_data) >= 2:
7372
# Check the structure: (filename, content, [optional content_type])
73+
filename = file_data[0]
7474
content = file_data[1]
7575

76+
# If filename is empty, this is likely JSON content, not a file
77+
if filename == "":
78+
return False
79+
7680
# File content is typically bytes, string, or file-like object
81+
# But exclude empty strings and None values
82+
if content is None or content == "":
83+
return False
84+
7785
return (
7886
isinstance(content, (bytes, str))
7987
or hasattr(content, "read") # File-like object

0 commit comments

Comments
 (0)