Skip to content

Comments

Fix large attachment handling with string keys and custom content_ids#537

Merged
AaronDDM merged 4 commits intomainfrom
adm/fix-campus-attachment-issues
Feb 20, 2026
Merged

Fix large attachment handling with string keys and custom content_ids#537
AaronDDM merged 4 commits intomainfrom
adm/fix-campus-attachment-issues

Conversation

@AaronDDM
Copy link
Contributor

@AaronDDM AaronDDM commented Feb 3, 2026

Summary

Fixes two bugs that caused attachments over 3MB (e.g., 5.4MB files) to fail with errors suggesting "sending a JSON request as multipart/form-data":

  • Bug 1: Size calculation only checked symbol keys - When users pass attachment hashes with string keys ("size" instead of :size), the size was calculated as 0, causing large files to be incorrectly sent as JSON instead of multipart form-data
  • Bug 2: Custom content_id detection failed - The file_upload? method only matched keys like file0, file1 but users can specify custom content_id values (e.g., "my-inline-image"), causing multipart detection to fail

Changes

lib/nylas/utils/file_utils.rb

  • Support both string and symbol keys when calculating attachment size
  • Support both string and symbol keys for file_path in build_form_request

lib/nylas/handler/http_client.rb

  • Detect attachments by checking for singleton methods (original_filename, content_type) rather than key name patterns, supporting custom content_id values

Test plan

  • Added 3 new tests for string key handling in file_utils_spec.rb
  • Added 4 new tests for custom content_id detection in http_client_spec.rb
  • All 108 tests in affected files pass
  • Full test suite passes (252/254 - 2 pre-existing failures in webhooks unrelated to this PR)

This fixes two bugs that caused attachments over 3MB (e.g., 5.4MB) to fail
with "sending a json request as multipart/form-data" errors:

1. Size calculation only checked symbol keys (`:size`) but users may pass
   attachment hashes with string keys (`"size"`). This caused large files
   to be incorrectly sent as JSON instead of multipart form-data.

2. The `file_upload?` detection only matched keys like `file0`, `file1`,
   but users can specify custom `content_id` values. This caused multipart
   detection to fail for attachments with custom content IDs.

Fixes:
- Support both string and symbol keys when calculating attachment size
- Detect attachments by checking for singleton methods (original_filename,
  content_type) rather than key name patterns
- Add empty line after guard clause in http_client.rb
- Remove extra spacing in file_utils_spec.rb comments
- Shorten test description to fit line length limit
…lity

- HTTP client: support streaming attachments and file-like values
- Add send_streaming_attachments_example
- Fix Rubocop offenses (style, layout, gemspec order)
- Replace CGI.parse with URI.decode_www_form for Ruby 3.4+ compatibility
- Modify .rubocop.yml to disable new cops and add specific RSpec rules
- Update rubocop-rspec dependency version in gem_config.rb to ~> 3.5
@AaronDDM AaronDDM merged commit 77de434 into main Feb 20, 2026
8 checks passed
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.

2 participants