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

fix: DataDict subscript crash on iOS <= 14.4 #2784

Merged
merged 4 commits into from
Jan 19, 2023
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
4 changes: 2 additions & 2 deletions Sources/ApolloAPI/DataDict.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public struct DataDict: Hashable {
}

@inlinable public subscript<T: AnyScalarType & Hashable>(_ key: String) -> T {
get { _data[key] as! T }
get { _data[key]?.base as! T }
set { _data[key] = newValue }
_modify {
var value = _data[key] as! T
Expand Down Expand Up @@ -76,7 +76,7 @@ extension Array: SelectionSetEntityValue where Element: SelectionSetEntityValue
guard let fieldData = fieldData as? [AnyHashable?] else {
fatalError("\(Self.self) expected list of data for entity.")
}
self = fieldData.map { Element.init(fieldData:$0, variables: variables) }
self = fieldData.map { Element.init(fieldData:$0?.base as? AnyHashable, variables: variables) }
}

@inlinable public var _fieldData: AnyHashable { map(\._fieldData) }
Expand Down
44 changes: 22 additions & 22 deletions Tests/ApolloTests/JSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@ class JSONTests: XCTestCase {

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
}
}
"""
{
"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)
Expand All @@ -72,28 +72,28 @@ class JSONTests: XCTestCase {

let stringFromReserialized = try XCTUnwrap(String(bytes: reserialized, encoding: .utf8))
XCTAssertEqual(stringFromReserialized, """
{"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}}}
""")
{"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 nsNull: JSONObject = ["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 optionalNSNull: JSONObject = ["aWeirdNull": Optional.some(NSNull())]
let serialized = try JSONSerializationFormat.serialize(value: optionalNSNull as JSONObject)
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 doubleOptional: JSONObject = ["aWeirdNull": Optional.some(Optional<Int>.none)]
let serialized = try JSONSerializationFormat.serialize(value: doubleOptional as JSONObject)
let stringFromSerialized = try XCTUnwrap(String(data: serialized, encoding: .utf8))

Expand Down
27 changes: 26 additions & 1 deletion Tests/ApolloTests/SelectionSetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SelectionSetTests: XCTestCase {
expect(actual.name).to(equal("Johnny Tsunami"))
}

func test__selection_givenOptionalField_givenNilValue__returnsNil() {
func test__selection_givenOptionalField_missingValue__returnsNil() {
// given
class Hero: MockSelectionSet, SelectionSet {
typealias Schema = MockSchemaMetadata
Expand All @@ -55,6 +55,31 @@ class SelectionSetTests: XCTestCase {
expect(actual.name).to(beNil())
}

func test__selection_givenOptionalField_givenNilValue__returnsNil() {
// given
class Hero: MockSelectionSet, SelectionSet {
typealias Schema = MockSchemaMetadata

override class var __selections: [Selection] {[
.field("__typename", String.self),
.field("name", String?.self)
]}

var name: String? { __data["name"] }
}

let object: JSONObject = [
"__typename": "Human",
"name": String?.none
]

// when
let actual = Hero(data: DataDict(object, variables: nil))

// then
expect(actual.name).to(beNil())
}

// MARK: Scalar - Nested Array Tests

func test__selection__nestedArrayOfScalar_nonNull_givenValue__returnsValue() {
Expand Down