-
Notifications
You must be signed in to change notification settings - Fork 210
Fix broken errors thrown by JSONDecoder when parsing decimal as Int
#1606
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
base: main
Are you sure you want to change the base?
Conversation
…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
f93383c to
33761ab
Compare
|
@swift-ci please test |
| static private func _slowpath_unwrapFixedWidthInteger<T: FixedWidthInteger>(as type: T.Type, json5: Bool, numberBuffer: BufferView<UInt8>, fullSource: BufferView<UInt8>, digitBeginning: BufferViewIndex<UInt8>, for codingPathNode: _CodingPathNode, _ additionalKey: (some CodingKey)?) throws -> T { | ||
| // Helper function to create number conversion error | ||
| func createNumberConversionError() -> DecodingError { | ||
| #if FOUNDATION_FRAMEWORK |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to preserve this existing (broken) behavior for FOUNDATION_FRAMEWORK as long as underlyingError's error code is untouched... @kperryua what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@itingliu Thanks for your review. Just to confirm, are you saying that underlyingError should always be
nil on all platforms in this case?
There is existing code that switches the presence of underlyingError based on the FOUNDATION_FRAMEWORK flag, which I used as a reference. I don't fully understand the intent behind it, but I think it would be better to keep the behavior consistent.
|
@swift-ci please test |
|
Sorry, I meant to click "comment" but clicked "close" accidentally. |
|
@swift-ci please test |
fixes: #1604
This PR fixes a regression in
JSONDecoderwhere attempting to decode incompatible numeric values (e.g., decimal123.45asInt, or negative-123asUInt) returns incorrect error information. The bug affects both thecodingPath(empty instead of correct path) anddebugDescription(generic message instead of specific error details).See more detail: #1604