Skip to content

Commit

Permalink
Fix bug with AnyHashable coercion (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyMDev authored and runner committed Oct 10, 2023
1 parent bd973ab commit 5529fd2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ class GraphQLExecutor_SelectionSetMapper_FromResponse_Tests: XCTestCase {
class GivenSelectionSet: MockSelectionSet {
override class var __selections: [Selection] { [.field("favorites", [String]?.self)] }
}
let object: JSONObject = ["favorites": NSNull()]
let object: JSONObject = ["favorites": DataDict._NullValue]

// when
let data = try readValues(GivenSelectionSet.self, from: object)
Expand Down
25 changes: 25 additions & 0 deletions Tests/ApolloTests/SelectionSetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,31 @@ class SelectionSetTests: XCTestCase {
expect(actual.friend).to(beNil())
}

func test__selection_givenOptionalEntityField_givenNullValue__returnsNil() {
// given
class Hero: MockSelectionSet {
typealias Schema = MockSchemaMetadata

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

var friend: Hero? { __data["friend"] }
}

let object: JSONObject = [
"__typename": "Human",
"friend": DataDict._NullValue
]

// when
let actual = try! Hero(data: object)

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

// MARK: Entity - Array Tests

func test__selection__arrayOfEntity_nonNull_givenValue__returnsValue() {
Expand Down
6 changes: 3 additions & 3 deletions apollo-ios/Sources/ApolloAPI/DataDict.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ extension DataDict {
/// In iOS versions 14.4 and lower, `AnyHashable` coercion does not work. On these platforms,
/// we need to do some additional unwrapping and casting of the values to avoid crashes and other
/// run time bugs.
public static var _AnyHashableCanBeCoerced: Bool {
public static let _AnyHashableCanBeCoerced: Bool = {
if #available(iOS 14.5, *) {
return true
} else {
return false
}
}
}()

}

Expand Down Expand Up @@ -195,7 +195,7 @@ extension Optional: SelectionSetEntityValue where Wrapped: SelectionSetEntityVal
case .none:
self = .none
case .some(let hashable):
if DataDict._AnyHashableCanBeCoerced && hashable == DataDict._NullValue {
if !DataDict._AnyHashableCanBeCoerced && hashable == DataDict._NullValue {
self = .none
} else if let optional = hashable.base as? Optional<AnyHashable>, optional == nil {
self = .none
Expand Down

0 comments on commit 5529fd2

Please sign in to comment.