-
Notifications
You must be signed in to change notification settings - Fork 86
fix: Prevent negative count in SimpleJsonFormatter #184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
en-milie
merged 1 commit into
Endava:master
from
ericfitz:fix/json-formatter-negative-indent
Dec 4, 2025
Merged
fix: Prevent negative count in SimpleJsonFormatter #184
en-milie
merged 1 commit into
Endava:master
from
ericfitz:fix/json-formatter-negative-indent
Dec 4, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fixed IllegalArgumentException when formatting JSON with extra closing brackets. The formatter was calling String.repeat() with a negative value when indentLevel reached 0 and tried to calculate (indentLevel - 1). Changes: - Added Math.max(0, indentLevel - 1) guard in SimpleJsonFormatter.java:57 - Added 3 test cases for malformed JSON edge cases - All 8 tests now pass Fixes error when generating HTML reports for responses with malformed JSON. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
Author
|
Example exception: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes
IllegalArgumentException: count is negative: -1that occurs when CATS generates HTML reports for API responses containing malformed JSON with extra closing brackets.Root Cause
The
SimpleJsonFormatter.formatJsonString()method was callingString.repeat(indentLevel - 1)on line 57 without checking ifindentLevelwas 0, resulting in a negative repeat count.This occurred when:
{"key":"value"}})indentLevelcounter reached 0 before all closing brackets were processedChanges
Math.max(0, indentLevel - 1)guard in SimpleJsonFormatter.java:57shouldHandleMalformedJsonWithExtraClosingBrackets()- tests{...}}shouldHandleMalformedJsonWithExtraClosingSquareBrackets()- tests[...]]shouldHandleNestedStructures()- ensures normal nested JSON still worksTest Results
All tests pass, including the 3 new edge case tests.
Impact
MustacheExceptionwhen generating HTML reports for malformed JSON responsesFiles Changed
src/main/java/com/endava/cats/util/SimpleJsonFormatter.java- 1 line changedsrc/test/java/com/endava/cats/util/SimpleJsonFormatterTest.java- 23 lines added🤖 Generated with Claude Code
Co-Authored-By: Claude noreply@anthropic.com