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-10781] Decode an encoded double optional fails #3409

swift-ci opened this issue May 27, 2019 · 1 comment

[SR-10781] Decode an encoded double optional fails #3409

swift-ci opened this issue May 27, 2019 · 1 comment


Copy link

@swift-ci swift-ci commented May 27, 2019

Previous ID SR-10781
Radar None
Original Reporter ChaosCoder (JIRA User)
Type Bug

Swift 5.0

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

md5: 076bfab4f40c06914f7da38d175cf6d4

Issue Description:

I'm trying to encode and decode a Codable struct that has a double optional (e.g. name: String??).

This way, encoding the struct with the JSONEncoder, I get a json with:

  • the property and its value in it, if it is .some: { name: "123" }
  • nothing, if it is .none: {}
  • the property and null, if it is .some(.none): { name: null }

Trying to decode this json again does not handle the third case correctly, the decoded value is .none (like the second case):

import Foundation

struct S: Codable {
    let name: String??

let a = S(name: "123")
let jsonA = try! JSONEncoder.init().encode(a) // { name: "123" }
let a2 = try! JSONDecoder().decode(S.self, from: jsonA)
assert( == .some("123"))

let b = S(name: nil)
let jsonB = try! JSONEncoder.init().encode(b) // {}
let b2 = try! JSONDecoder().decode(S.self, from: jsonB)
assert( == .none)

let c = S(name: .some(nil)) // or .some(.none)
let jsonC = try! JSONEncoder.init().encode(c) // { name: null }
let c2 = try! JSONDecoder().decode(S.self, from: jsonC)
assert( == .some(.none)) // fails the assertion, because == .none

The problem is the last line, is .none and not .some(.none) as expected.

Copy link

@belkadan belkadan commented May 28, 2019

I don't think that's what the default synthesized init(coder:) does. It seems like reasonable behavior for it to do so, though.

@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
None yet

No branches or pull requests

2 participants