Skip to content

Conversation

@hiragram
Copy link

@hiragram hiragram commented Nov 17, 2025

fixes: #1604

This PR fixes a regression in JSONDecoder where attempting to decode incompatible numeric values (e.g., decimal 123.45 as Int, or negative -123 as UInt) returns incorrect error information. The bug affects both the codingPath (empty instead of correct path) and debugDescription (generic message instead of specific error details).

See more detail: #1604

…iftlang#1604)

This commit fixes the regression where JSONDecoder returns incorrect error
information when attempting to decode a decimal value (e.g., 123.45) as Int.

**Root Cause (from git history investigation):**
- Initial implementation (commit 34c45c1, 2023-03-15) had correct error handling
- BufferView refactoring (commit 1ab3832, 2023-04-03) accidentally removed the
  proper DecodingError.dataCorrupted throw statements
- This caused JSONError.numberIsNotRepresentableInSwift to be caught at the
  top level and converted to a generic error with empty codingPath

**Changes:**
- Restored proper error handling in `_slowpath_unwrapFixedWidthInteger`
- Changed JSONError.numberIsNotRepresentableInSwift throws to
  DecodingError.dataCorrupted with correct codingPath and debugDescription
- Added regression test to prevent future breakage

**Before (broken):**
- codingPath: [] (empty)
- debugDescription: "The given data was not valid JSON."

**After (fixed):**
- codingPath: ["foo"]
- debugDescription: "Parsed JSON number <123.45> does not fit in Int."

Fixes swiftlang#1604
Related: swiftlang#274
@omochi
Copy link

omochi commented Nov 18, 2025

@swift-ci please test

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.

JSONDecoder throws a broken error when parsing decimal value as Int

2 participants