xq icon indicating copy to clipboard operation
xq copied to clipboard

Fix #160: Anchor IsJSON regex to prevent false positives on CDATA

Open bukzor opened this issue 2 months ago • 1 comments

Summary

Fixes part of issue #160 by anchoring the IsJSON() regex to prevent false positives when detecting format.

Problem: The IsJSON() function uses an unanchored regex \s*[{\[] which matches a [ character anywhere in the input. This causes XML documents containing CDATA sections like <![CDATA[text]]> to be incorrectly detected as JSON format.

Solution: Changed the regex to ^\s*[{\[] to anchor it to the start of the input string. Now only inputs that actually begin with { or [ (after optional whitespace) will be detected as JSON.

Changes

  • internal/utils/utils.go: Modified IsJSON() to use anchored regex ^\s*[{\[]
  • cmd/root_test.go: Added TestFormatDetection test to verify CDATA sections are correctly detected as XML format

Test Plan

  • [x] Added test case TestFormatDetection/CDATA_should_be_detected_as_XML_not_JSON
  • [x] Verified test fails without the fix (shows CDATA detected as JSON)
  • [x] Verified test passes with the fix (CDATA now correctly detected as XML)
  • [x] All existing unit tests pass (go test ./...)

Additional Context

This is part 2 of a 4-part fix for issue #160. The other PRs address:

  1. HTML text escaping (PR #161 - already submitted)
  2. IsJSON regex anchor (this PR)
  3. CDATA support in NodeToJSON
  4. Exhaustive switch statements for node types

🤖 Generated with Claude Code

bukzor avatar Oct 22 '25 16:10 bukzor

:warning: Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 80.57%. Comparing base (ec5a59a) to head (9a5c191). :exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #163   +/-   ##
=======================================
  Coverage   80.57%   80.57%           
=======================================
  Files           5        5           
  Lines         690      690           
=======================================
  Hits          556      556           
  Misses         92       92           
  Partials       42       42           

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov-commenter avatar Nov 14 '25 17:11 codecov-commenter