Skip to content

Fixing issue where JSON and XML with a % character breaks opening in a new tab#21585

Merged
Benjin merged 1 commit intomainfrom
dev/benjin/openJsonFix
Mar 20, 2026
Merged

Fixing issue where JSON and XML with a % character breaks opening in a new tab#21585
Benjin merged 1 commit intomainfrom
dev/benjin/openJsonFix

Conversation

@Benjin
Copy link
Contributor

@Benjin Benjin commented Mar 12, 2026

Pull Request Template – vscode-mssql

Description

Addresses #20826

XML and JSON containing a % throws when being decoded with the built-in function. Work-around is to return the raw string if this occurs.

Code Changes Checklist

  • New or updated unit tests added
  • All existing tests pass (npm run test)
  • Code follows contributing guidelines
  • Telemetry/logging updated if relevant
  • No regressions or UX breakage

Reviewers: Please read our reviewer guidelines

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a bug where JSON and XML content containing a % character would throw an error when the user clicked to open it in a new tab. The % causes decodeURIComponent to throw a URIError when it encounters a bare % that isn't a valid percent-encoded sequence.

Changes:

  • Added a new decodeQueryResultLinkFragment helper in utils.ts that wraps decodeURIComponent in a try/catch, falling back to the raw fragment on error.
  • Added inline try/catch in mainController.ts for the query-result-link text document content provider (duplicating the helper logic instead of using it).
  • Added unit tests for the new helper function.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
extensions/mssql/src/utils/utils.ts New decodeQueryResultLinkFragment helper with try/catch fallback
extensions/mssql/src/controllers/mainController.ts Inline try/catch added to provideTextDocumentContent (duplicates helper)
extensions/mssql/test/unit/utils.test.ts Unit tests for the new helper function

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +738 to +744
// Attempt to decode the URI fragment
try {
return decodeURIComponent(uri.fragment);
} catch {
// If decoding fails, return the raw fragment as a fallback. Some characters (like '%') can cause decodeURIComponent to throw an error.
return uri.fragment;
}
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

The new decodeQueryResultLinkFragment helper in utils.ts was created for exactly this purpose, but it's not being used here. Instead, the try/catch logic is duplicated inline. This should call decodeQueryResultLinkFragment(uri.fragment) instead of reimplementing the same logic.

Copilot uses AI. Check for mistakes.
Comment on lines +462 to +463
encoded = encodeURIComponent(original);
result = utilUtils.decodeQueryResultLinkFragment(encoded);
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

This test case doesn't actually exercise the fallback path. encodeURIComponent('{"test":"%"}') produces %7B%22test%22%3A%22%25%22%7D, where % is encoded as %25. Decoding %25 succeeds and returns %, so decodeURIComponent never throws here.

To test the fallback, you need to pass a string that contains a bare % that isn't a valid percent-encoded sequence (e.g., '{"test":"%"}' directly without encoding it first). That's the scenario the bug describes—a raw % in the URI fragment.

Suggested change
encoded = encodeURIComponent(original);
result = utilUtils.decodeQueryResultLinkFragment(encoded);
// Pass the raw fragment so that decodeURIComponent throws and the fallback is exercised
result = utilUtils.decodeQueryResultLinkFragment(original);

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

@Benjin Benjin Mar 12, 2026

Choose a reason for hiding this comment

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

This is untrue; the fallback path is hit when debugging.

@github-actions
Copy link

PR Changes

Category Target Branch PR Branch Difference
vscode-mssql VSIX 6412 KB 6412 KB ⚪ 0 KB ( 0% )
sql-database-projects VSIX 7061 KB 7061 KB ⚪ 0 KB ( 0% )
data-workspace VSIX 535 KB 535 KB ⚪ 0 KB ( 0% )

@codecov-commenter
Copy link

codecov-commenter commented Mar 12, 2026

Codecov Report

❌ Patch coverage is 47.36842% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.82%. Comparing base (19f8d96) to head (d92d287).
⚠️ Report is 65 commits behind head on main.

Files with missing lines Patch % Lines
extensions/mssql/src/controllers/mainController.ts 0.00% 7 Missing ⚠️
extensions/mssql/src/utils/utils.ts 75.00% 3 Missing ⚠️

❌ Your patch status has failed because the patch coverage (47.36%) is below the target coverage (70.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #21585      +/-   ##
==========================================
- Coverage   72.82%   72.82%   -0.01%     
==========================================
  Files         331      331              
  Lines       98825    98842      +17     
  Branches     5511     5512       +1     
==========================================
+ Hits        71973    71982       +9     
- Misses      26852    26860       +8     
Files with missing lines Coverage Δ
extensions/mssql/src/utils/utils.ts 85.47% <75.00%> (-0.57%) ⬇️
extensions/mssql/src/controllers/mainController.ts 33.88% <0.00%> (-0.06%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Benjin Benjin merged commit cc67028 into main Mar 20, 2026
7 checks passed
@Benjin Benjin deleted the dev/benjin/openJsonFix branch March 20, 2026 15:46
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.

5 participants