Skip to content
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

[SR-11780] JSONEncoder can encode a URL that JSONDecoder can't decode #3374

Open
lilyball mannequin opened this issue Nov 13, 2019 · 5 comments
Open

[SR-11780] JSONEncoder can encode a URL that JSONDecoder can't decode #3374

lilyball mannequin opened this issue Nov 13, 2019 · 5 comments

Comments

@lilyball
Copy link
Mannequin

lilyball mannequin commented Nov 13, 2019

Previous ID SR-11780
Radar rdar://problem/57176800
Original Reporter @lilyball
Type Bug
Environment

Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7)
Target: x86_64-apple-darwin18.7.0

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: 474c771f2a7abc910ab783c0bdc3cf2c

Issue Description:

Swift's URL type tries to avoid letting you create an empty URL, but you can still do it with NSURL(string: "")! as URL (personally, I wish URL supported the empty URL case natively). Such an empty URL can be encoded with JSONEncoder, but attempting to decode the results throws a decoding error. This is surprising.

import Foundation
let data = try JSONEncoder().encode(["url": NSURL(string: "")! as URL])
let dict = try JSONDecoder().decode(Dictionary<String, URL>.self, from: data)
Fatal error: Error raised at top level: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "url", intValue: nil)], debugDescription: "Invalid U
RL string.", underlyingError: nil)): file /BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1001.8.63.13/swift/stdlib/public/core/ErrorType.swift, line 200
@beccadax
Copy link
Contributor

@swift-ci create

@theblixguy
Copy link

@parkera Should box_ in JSONEncoder be updated to throw EncodingError.invalidValue if the absoluteString is empty?

@lilyball
Copy link
Mannequin Author

lilyball mannequin commented Jan 9, 2020

As a user, I would be very surprised to find that JSONEncoder.encode(["url": someURL]) could throw an error.

@theblixguy
Copy link

Hmm, we could return NSURL(string: "")! as URL instead of throwing a DecodingError.

@parkera
Copy link
Member

parkera commented Jan 14, 2020

We specifically decided that `URL` shouldn't support the empty string (that's why it's different than `NSURL`), because it brought a lot of complications to the behavior that we didn't like.

I think it is reasonable that JSONDecoder should treat the empty string as an invalid value when it finds it, for consistency.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants