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-10464] Optional URL value with Decodable protocol failing #3445

Open
swift-ci opened this issue Apr 12, 2019 · 5 comments
Open

[SR-10464] Optional URL value with Decodable protocol failing #3445

swift-ci opened this issue Apr 12, 2019 · 5 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-10464
Radar None
Original Reporter szuniverse (JIRA User)
Type Bug
Environment

Swift 4.2
Xcode 10.1 (10B61)

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug, Codable
Assignee bendjones (JIRA)
Priority Medium

md5: d568d9a734aa22a2960bae6ad83bfe20

Issue Description:

struct MyStruct: Decodable {
    let id: String
    let homepageUrl: URL?

    //    private enum CodingKeys: String, CodingKey {
    //        case id, homepageUrl = "homepage_url"
    //    }
}

let dict = [
    "id": "1",
    "homepage_url": ""
]

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase

let data = try JSONSerialization.data(withJSONObject: dict)
let myStruct = try decoder.decode(MyStruct.self, from: data)
print("m: \(myStruct.id) - \(String(describing: myStruct.homepageUrl))")

In this case the error message is `Invalid URL string.` which is correct because it's an empty string so its not a valid URL, but the `homepageUrl` is optional, so it should produce a nil value instead of failing.

If the `MyStruct` is using `CodingKeys` (uncomment that part of the code), then its working fine with empty string in dictionary.

I think its a swift bug because: If `keyDecodingStrategy` is `convertFromSnakeCase` then it should produce nil without using `CodingKeys`.

@belkadan
Copy link

I don't think the empty string is considered the same as a null value, and Optional does not mean "ignore errors" for Codable's purposes. cc bendjones (JIRA User)

@swift-ci
Copy link
Contributor Author

Comment by Szurma David (JIRA)

yeah thats true, but the weird thing is that if the struct using CodingKeys the behaviour is not the same. I don't think that the CodingKeys usage should modify the result if the keyDecodingStrategy is convertFromSnakeCase

@belkadan
Copy link

Ah, I see. Sorry for not reading closely!

@swift-ci
Copy link
Contributor Author

swift-ci commented Jun 3, 2019

Comment by Szurma David (JIRA)

Any update?

@poya
Copy link

poya commented Oct 22, 2021

I guess the reason it's "working fine with empty string in dictionary" when you add CodingKeys is because the decoder then no longer matches "homepage_url" key to your homepageUrl property. That can be validated by putting a valid URL string in your json and your homepageUrl would still be nil. With the decoder set to .convertFromSnakeCase snake-case keys are converted to camel-case keys, and that means that the manual snake-case mapping you do in your CodingKeys results in a mismatch and hence homepageUrl will never be set. Keeping the CodingKeys and removing the .convertFromSnakeCase you match again, and the `Invalid URL String` error would return.

@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