RUN-2569: Fix ClassCastException in HttpBuilder.setHeaders for non-String header values#42
Conversation
…569) HttpBuilder.setHeaders parsed the headers config string with Gson, then fell back to SnakeYAML. Both libraries place non-String values into the resulting map for JSON/YAML numeric and boolean scalars. The map was then iterated with Map.Entry<String,String>, which compiled because of generic type erasure but threw ClassCastException at runtime when the value was e.g. Integer (the case reported in #32 and PagerDuty ticket RUN-2569). Restructure setHeaders to delegate parsing to a private parseHeaders helper that returns Map<String,Object> after explicit `instanceof Map` checks (no more "exceptions as control flow"), and route every value through a new headerValueToString helper. The helper renders whole-number Double values via Long.toString so JSON like {"Content-Length":0} emits "0" instead of "0.0" (Gson parses every JSON number as Double when the target type is HashMap.class). Non-scalar values (lists, nested maps) are stringified via String.valueOf instead of throwing. Adds unit tests in HttpBuilderTest covering: YAML/JSON integer, plain string, mixed types, boolean, list stringification, unparseable input log path, and direct tests of headerValueToString for whole-number and fractional Doubles. ./gradlew clean build green on Java 17; HttpBuilderTest 14/14. Co-authored-by: Cursor <cursoragent@cursor.com>
There was a problem hiding this comment.
Pull request overview
Fixes a runtime ClassCastException in HttpBuilder.setHeaders when header configuration is parsed from YAML/JSON into non-String scalar values (e.g. integers/booleans), and adds targeted tests to pin the corrected behavior.
Changes:
- Refactors
setHeadersto parse intoMap<String, Object>and stringify header values safely viaheaderValueToString. - Adds
parseHeadershelper to accept either JSON or YAML maps and avoid crashing on non-map inputs. - Expands
HttpBuilderTestcoverage for YAML/JSON numeric/boolean/list header values and unparseable input behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/main/java/edu/ohio/ais/rundeck/HttpBuilder.java | Refactors header parsing/coercion to avoid ClassCastException for non-String header values. |
| src/test/java/edu/ohio/ais/rundeck/HttpBuilderTest.java | Adds regression/unit tests covering numeric/boolean/list header values and parsing failure behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+437
to
+439
| for (Map.Entry<String, Object> entry : map.entrySet()) { | ||
| request.setHeader(entry.getKey(), headerValueToString(entry.getValue())); | ||
| } |
Comment on lines
+463
to
472
| static String headerValueToString(Object value) { | ||
| if (value instanceof Double) { | ||
| double d = (Double) value; | ||
| if (!Double.isInfinite(d) && !Double.isNaN(d) && d == Math.floor(d) | ||
| && d >= Long.MIN_VALUE && d <= Long.MAX_VALUE) { | ||
| return Long.toString((long) d); | ||
| } | ||
| } | ||
| return String.valueOf(value); | ||
| } |
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
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
HttpBuilder.setHeadersparsed header config via Gson/SnakeYAML, which returnMap<String, Object>with non-String values (e.g.Integer,Boolean) for numeric/boolean scalarsMap.Entry<String, String>compiled due to type erasure but threwClassCastExceptionat runtime (e.g.{"Content-Length": 0})setHeadersto use aparseHeadershelper returningMap<String, Object>with explicitinstanceof Mapchecks, and aheaderValueToStringhelper that coerces all values to String safelyDoublevalues (Gson parses all JSON numbers asDouble) are rendered viaLong.toStringso{"Content-Length": 0}emits"0"instead of"0.0"String.valueOfTest plan
./gradlew clean buildpasses (Java 17)HttpBuilderTest— 14/14 tests green, covering: YAML/JSON integer, plain string, mixed types, boolean, list stringification, unparseable input log path, and direct tests ofheaderValueToStringContent-Length,Retry-After) run withoutClassCastExceptionon Rundeck 5.3.0+References
🤖 Generated with Claude Code