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

Add workaround for Optional JSONEncoding errors #1317

Merged
merged 3 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Sources/Apollo/JSONStandardTypeConversions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ extension Optional: JSONEncodable {
return NSNull()
case .some(let wrapped as JSONEncodable):
return wrapped.jsonValue

// WORKAROUND: For reasons I don't totally understand, when the underlying type is `Any`,
// even though all of these conform to `JSONEncodable`, the `as JSONEncodable` above
// fails, and we need to handle them individually.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If anyone's got any bright ideas why this fails, I'm all ears.

case .some(let wrapped as String):
return wrapped.jsonValue
case .some(let wrapped as Int):
return wrapped.jsonValue
case .some(let wrapped as Double):
return wrapped.jsonValue
case .some(let wrapped as Bool):
return wrapped.jsonValue
case .some(let wrapped as [String: Any?]):
return wrapped.jsonValue
case .some(let wrapped as [Any?]):
return wrapped.jsonValue
default:
fatalError("Optional is only JSONEncodable if Wrapped is")
}
Expand All @@ -129,6 +145,16 @@ extension Dictionary: JSONEncodable {
}
}

extension Dictionary: JSONDecodable {
public init(jsonValue value: JSONValue) throws {
guard let dictionary = value as? Dictionary else {
throw JSONDecodingError.couldNotConvert(value: value, to: Dictionary.self)
}

self = dictionary
}
}

extension Array: JSONEncodable {
public var jsonValue: JSONValue {
return map() { element -> (JSONValue) in
Expand Down
35 changes: 35 additions & 0 deletions Tests/ApolloTests/JSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,39 @@ class JSONTests: XCTestCase {
XCTAssertFalse(value ~= JSONDecodingError.nullValue)
XCTAssertFalse(value ~= JSONDecodingError.missingValue)
}

func testJSONDictionaryEncodingAndDecoding() throws {
let jsonString = """
{
"a_dict": {
"a_bool": true,
"another_dict" : {
"a_double": 23.1,
"an_int": 8,
"a_string": "LOL wat"
},
"an_array": [
"one",
"two",
"three"
],
"a_null": null
}
}
"""
let data = try XCTUnwrap(jsonString.data(using: .utf8))
let json = try JSONSerializationFormat.deserialize(data: data)
XCTAssertNotNil(json)

let dict = try Dictionary<String, Any?>(jsonValue: json)
XCTAssertNotNil(dict)

let reserialized = try JSONSerializationFormat.serialize(value: dict)
XCTAssertNotNil(reserialized)

let stringFromReserialized = try XCTUnwrap(String(bytes: reserialized, encoding: .utf8))
XCTAssertEqual(stringFromReserialized, """
{"a_dict":{"a_bool":1,"a_null":null,"an_array":["one","two","three"],"another_dict":{"a_double":23.100000000000001,"a_string":"LOL wat","an_int":8}}}
""")
}
}