Skip to content

fix: implement Speakeasy hook to fix multipart file field names #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

chris-freeman-glean
Copy link

Fix multipart file field names with Speakeasy SDK hook

Problem

Speakeasy generates incorrect code that adds "[]" suffix to file field names in multipart forms:

# Generated (incorrect):
files.append((f_name + "[]", (file_name, content, content_type)))

# Expected:
files.append((f_name, (file_name, content, content_type)))

The "[]" suffix should only apply to regular form field arrays, not file fields.

Solution

Implemented an SDKInitHook that patches serialize_multipart_form at runtime:

  1. Wraps the original function during SDK initialization
  2. Post-processes the files list to remove "[]" suffix from file fields only
  3. Identifies file fields by their tuple structure: (filename, content, [content_type])
  4. Preserves "[]" suffix for regular form field arrays

Files Changed

  • src/glean/api_client/_hooks/multipart_fix_hook.py - Hook implementation
  • src/glean/api_client/_hooks/registration.py - Hook registration
  • tests/test_multipart_fix_hook.py - Test coverage

Testing

Run python -m pytest tests/test_multipart_fix_hook.py -v

Notes

  • Hook runs automatically on SDK initialization
  • Generated code in forms.py still contains the original bug
  • Fix applies at runtime during multipart serialization
  • No breaking changes to public APIs

- Add MultipartFileFieldFixHook that patches serialize_multipart_form at runtime
- Fix issue where Speakeasy generates incorrect '[]' suffix on file field names
- Hook removes '[]' suffix only from file fields, preserves it for form arrays
- Add comprehensive tests covering file field detection and patching logic

Fixes multipart form uploads by intercepting serialization before HTTP requests.
The hook survives Speakeasy code regeneration since it patches at SDK init time.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant