Skip to content

fix(BL): Saved Queries Uploads Not Using Ingest File Checks BED-7040#2786

Open
ALCooper12 wants to merge 2 commits into
mainfrom
BED-7040
Open

fix(BL): Saved Queries Uploads Not Using Ingest File Checks BED-7040#2786
ALCooper12 wants to merge 2 commits into
mainfrom
BED-7040

Conversation

@ALCooper12
Copy link
Copy Markdown
Contributor

@ALCooper12 ALCooper12 commented May 18, 2026

Description

  • Updated helper functions inside streamdecoder.go and saved_queries.go in order to utilize BOM normalization for the ImportSavedQueries endpoint
  • Added 2 more unit test cases for the ImportSavedQueries endpoint

Motivation and Context

This PR addresses:

Why is this change required? What problem does it solve?

  • At the moment, whenever someone tries to upload a custom query stored in a JSON file with UTF-8 BOM or a ZIP file containing JSON with UTF-8 BOM an error message is displayed. The changes introduced add another form of file checking/validation that utilizes BOM normalization so that uploading individual or zip files containing JSON with UTF-8 BOM are accepted with no error messages displayed

How Has This Been Tested?

  • Added more unit tests and tested via Bruno

Screenshots (optional):

  • Still need to add these***

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling and normalization of UTF-8 BOM when importing saved queries from JSON and ZIP, with clearer error responses for malformed/unnormalizable JSON.
  • Tests

    • Added tests verifying successful import of JSON and ZIP payloads that include UTF-8 BOMs and asserting correct import counts.

Review Change Stack

@ALCooper12 ALCooper12 self-assigned this May 18, 2026
@ALCooper12 ALCooper12 added bug Something isn't working api A pull request containing changes affecting the API code. labels May 18, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 18, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: f57e9c9e-878e-474e-8563-b786ff677187

📥 Commits

Reviewing files that changed from the base of the PR and between 50dc753 and ab9c00e.

📒 Files selected for processing (2)
  • cmd/api/src/api/v2/saved_queries.go
  • cmd/api/src/services/upload/streamdecoder.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • cmd/api/src/api/v2/saved_queries.go
  • cmd/api/src/services/upload/streamdecoder.go

📝 Walkthrough

Walkthrough

Adds UTF-8 BOM normalization to saved-query imports: the stream ZIP reader normalizes ZIP entry contents, the JSON import path normalizes request bodies, and tests validate both BOM scenarios.

Changes

UTF-8 BOM Normalization for Saved Query Imports

Layer / File(s) Summary
UTF-8 BOM normalization in ZIP stream reader
cmd/api/src/services/upload/streamdecoder.go
ReadZippedFile normalizes ZIP entry contents with bomenc.NormalizeToUTF8 before reading, returning wrapped errors on normalization failure.
UTF-8 BOM normalization in saved queries JSON import
cmd/api/src/api/v2/saved_queries.go
extractImportQueriesFromJsonFile normalizes the incoming JSON request body to UTF-8 before reading/unmarshalling; ImportSavedQueries maps normalization failures (failed to normalize json file) to 400 responses.
Test coverage for UTF-8 BOM handling
cmd/api/src/api/v2/saved_queries_test.go
Added two table-driven tests covering BOM-prefixed JSON import (single JSON payload) and BOM-prefixed ZIP entries, asserting expected import counts.

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: adding BOM normalization to saved query uploads to fix a bug where UTF-8 BOM-prefixed JSON files were being rejected. It references the ticket (BED-7040) and is specific and concise.
Description check ✅ Passed The description covers the key required sections: it explains what was changed (BOM normalization in helper functions and added tests), the motivation (addresses BED-7040, fixes UTF-8 BOM upload errors), how it was tested (unit tests and Bruno), and properly identifies it as a bug fix. However, the author notes screenshots are still needed ('Still need to add these***'), representing incomplete documentation.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch BED-7040

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

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

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@cmd/api/src/api/v2/saved_queries.go`:
- Around line 413-415: The NormalizeToUTF8 failure is currently propagated as a
generic error and becomes a 500; change ImportSavedQueries to treat
NormalizeToUTF8 normalization errors as a client bad request (400).
Specifically, when calling bomenc.NormalizeToUTF8 (and where you return
fmt.Errorf("failed to normalize json file: %w", err)), convert or wrap that
error into the API's BadRequest/user input error type and add a case in the
ImportSavedQueries error-handling switch to map that normalization error to a
400 response (referencing the NormalizeToUTF8 call and the "failed to normalize
json file" message to locate the code).

In `@cmd/api/src/services/upload/streamdecoder.go`:
- Around line 379-385: The ZIP entry opened via zf.Open() is not closed if
bomenc.NormalizeToUTF8(f) returns an error because defer f.Close() is registered
after the normalization; update the logic in streamdecoder.go around zf.Open(),
bomenc.NormalizeToUTF8, and the defer to ensure f.Close() is invoked on every
path (e.g., call defer f.Close() immediately after successful zf.Open() or
explicitly close f before returning on normalization error) so that the ZIP
entry is never leaked.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 629c529f-0359-4598-9e72-3ff54af452f4

📥 Commits

Reviewing files that changed from the base of the PR and between 36c383e and 50dc753.

📒 Files selected for processing (3)
  • cmd/api/src/api/v2/saved_queries.go
  • cmd/api/src/api/v2/saved_queries_test.go
  • cmd/api/src/services/upload/streamdecoder.go

Comment thread cmd/api/src/api/v2/saved_queries.go
Comment thread cmd/api/src/services/upload/streamdecoder.go Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api A pull request containing changes affecting the API code. bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant