Skip to content

Conversation

chyroc
Copy link
Contributor

@chyroc chyroc commented Sep 29, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of multipart file uploads: the form field key is now chosen based on the detected file type or the “filename” key, with a safe default to “file” when unspecified. This prevents upload failures due to missing or mismatched keys and improves compatibility with third-party clients.
    • Ensures more consistent and predictable behavior for file uploads across endpoints.

@chyroc chyroc added the bug Something isn't working label Sep 29, 2025
Copy link

coderabbitai bot commented Sep 29, 2025

Walkthrough

Adjusted file upload key resolution in parseRawRequestReqBody within request.go: fileKey is now set only when matching FileTypes, io.Reader, or key "filename"; otherwise it falls back to "file". This changes how multipart form-data keys are determined for file uploads.

Changes

Cohort / File(s) Change Summary
Multipart upload key resolution
request.go
Refined logic for setting multipart file field name: conditional assignment based on type/key, plus a default fallback to "file" when unspecified. Removed unconditional pre-assignment to JSON tag key.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Caller
  participant RB as RequestBuilder
  participant PR as parseRawRequestReqBody
  participant MP as MultipartBuilder

  Caller->>RB: Build request with file payload
  RB->>PR: parseRawRequestReqBody(isFile=true, key, value)
  PR->>PR: Determine fileKey
  alt value is FileTypes
    PR->>PR: fileKey = key (from tag)
  else value is io.Reader
    PR->>PR: fileKey = key (from tag)
  else key == "filename"
    PR->>PR: fileKey = "filename"
  else
    PR->>PR: fileKey = "file" (fallback)
  end
  PR->>MP: Add file part with fileKey
  MP-->>RB: Multipart body
  RB-->>Caller: Prepared HTTP request
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • feat: Add voiceprint api #68 — Also modifies multipart/file-upload parsing in request.go, particularly around parseRawRequestReqBody and key handling.

Poem

I nibble bytes with whiskered grace,
A tiny key now finds its place—
If none is named, “file” will do,
Hops through forms, consistent too.
In fields and parts I softly tread,
Carrots cached, no crumbs of dread.
Ship it quick, then off to bed! 🥕🐇

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly describes the fix to the fileKey assignment logic within the file upload utility, directly reflecting the main change of the pull request without extraneous information.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-upload-file-utils

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d5e3dc1 and 1e04333.

📒 Files selected for processing (1)
  • request.go (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Test success
🔇 Additional comments (2)
request.go (2)

287-312: LGTM! The fileKey assignment logic is now correct.

The fix properly ensures that fileKey is only assigned when a field actually contains file data (implements FileTypes or io.Reader), rather than being set for every json-tagged field. This prevents incorrect form field names in multipart uploads when non-file fields are processed before file fields.


324-327: Good defensive fallback for the file key.

The default to "file" ensures that multipart uploads always have a valid form field name, even when no explicit file field is found. This maintains backward compatibility and prevents potential errors from empty field names.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🧪 Early access (Sonnet 4.5): enabled

We are currently testing the Sonnet 4.5 model, which is expected to improve code review quality. However, this model may lead to increased noise levels in the review comments. Please disable the early access features if the noise level causes any inconvenience.

Note:

  • Public repositories are always opted into early access features.
  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.

Comment @coderabbitai help to get the list of available commands and usage tips.

@chyroc chyroc merged commit ea74ba2 into main Sep 29, 2025
16 checks passed
@chyroc chyroc deleted the fix-upload-file-utils branch September 29, 2025 23:53
@chyroc chyroc mentioned this pull request Sep 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant