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

Ironing JSONSerialization quirks #1478

Merged
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
24 changes: 8 additions & 16 deletions Sources/Apollo/JSONStandardTypeConversions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,28 +105,20 @@ 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.
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")
}
}
}

extension NSDictionary: JSONEncodable {
public var jsonValue: JSONValue { self }
}

extension NSNull: JSONEncodable {
public var jsonValue: JSONValue { self }
}

extension Dictionary: JSONEncodable {
public var jsonValue: JSONValue {
return jsonObject
Expand Down
26 changes: 25 additions & 1 deletion Tests/ApolloTests/JSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,31 @@ class JSONTests: XCTestCase {

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}}}
{"a_dict":{"a_bool":true,"a_null":null,"an_array":["one","two","three"],"another_dict":{"a_double":23.100000000000001,"a_string":"LOL wat","an_int":8}}}
""")
}

func testEncodingNSNullDoesNotCrash() throws {
let nsNull = ["aWeirdNull": NSNull()]
let serialized = try JSONSerializationFormat.serialize(value: nsNull)
let stringFromSerialized = try XCTUnwrap(String(data: serialized, encoding: .utf8))

XCTAssertEqual(stringFromSerialized, #"{"aWeirdNull":null}"#)
}

func testEncodingOptionalNSNullDoesNotCrash() throws {
let optionalNSNull = ["aWeirdNull": Optional.some(NSNull())]
let serialized = try JSONSerializationFormat.serialize(value: optionalNSNull)
let stringFromSerialized = try XCTUnwrap(String(data: serialized, encoding: .utf8))

XCTAssertEqual(stringFromSerialized, #"{"aWeirdNull":null}"#)
}

func testEncodingDoubleOptionalsDoesNotCrash() throws {
let doubleOptional = ["aWeirdNull": Optional.some(Optional<Int>.none)]
let serialized = try JSONSerializationFormat.serialize(value: doubleOptional)
let stringFromSerialized = try XCTUnwrap(String(data: serialized, encoding: .utf8))

XCTAssertEqual(stringFromSerialized, #"{"aWeirdNull":null}"#)
}
}