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 bug with AnyHashable coercion #68

Merged
merged 1 commit into from
Oct 10, 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
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