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: ObjectData type check #459

Merged
merged 5 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
85 changes: 85 additions & 0 deletions Tests/ApolloTests/CacheKeyResolutionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,91 @@ class CacheKeyResolutionTests: XCTestCase {
))
}

func test__schemaConfiguration__givenData_asInt_equalToBoolFalse_shouldNotConvertToBool() {
MockSchemaMetadata.stub_objectTypeForTypeName = { _ in nil }
MockSchemaMetadata.stub_cacheKeyInfoForType_Object = { (_, json) in
return try? CacheKeyInfo(jsonValue: json["id"])
}

let object: JSONObject = [
"__typename": "Omega",
"id": 0
]

let objectDict = NetworkResponseExecutionSource().opaqueObjectDataWrapper(for: object)
let actual = MockSchemaMetadata.cacheKey(for: objectDict)

expect(actual).to(equal("Omega:0"))
}

func test__schemaConfiguration__givenData_asInt_equalToBoolTrue_shouldNotConvertToBool() {
MockSchemaMetadata.stub_objectTypeForTypeName = { _ in nil }
MockSchemaMetadata.stub_cacheKeyInfoForType_Object = { (_, json) in
return try? CacheKeyInfo(jsonValue: json["id"])
}

let object: JSONObject = [
"__typename": "Omega",
"id": 1
]

let objectDict = NetworkResponseExecutionSource().opaqueObjectDataWrapper(for: object)
let actual = MockSchemaMetadata.cacheKey(for: objectDict)

expect(actual).to(equal("Omega:1"))
}

func test__schemaConfiguration__givenData_asInt_outsideBoolRange_shouldReturnCacheReference() {
MockSchemaMetadata.stub_objectTypeForTypeName = { _ in nil }
MockSchemaMetadata.stub_cacheKeyInfoForType_Object = { (_, json) in
return try? CacheKeyInfo(jsonValue: json["id"])
}

let object: JSONObject = [
"__typename": "Omega",
"id": 2
]

let objectDict = NetworkResponseExecutionSource().opaqueObjectDataWrapper(for: object)
let actual = MockSchemaMetadata.cacheKey(for: objectDict)

expect(actual).to(equal("Omega:2"))
}

func test__schemaConfiguration__givenData_asBool_true_shouldNotConvertToInt() {
MockSchemaMetadata.stub_objectTypeForTypeName = { _ in nil }
MockSchemaMetadata.stub_cacheKeyInfoForType_Object = { (_, json) in
return try? CacheKeyInfo(jsonValue: json["id"])
}

let object: JSONObject = [
"__typename": "Omega",
"id": true
]

let objectDict = NetworkResponseExecutionSource().opaqueObjectDataWrapper(for: object)
let actual = MockSchemaMetadata.cacheKey(for: objectDict)

expect(actual).to(beNil())
}

func test__schemaConfiguration__givenData_asBool_false_shouldNotConvertToInt() {
MockSchemaMetadata.stub_objectTypeForTypeName = { _ in nil }
MockSchemaMetadata.stub_cacheKeyInfoForType_Object = { (_, json) in
return try? CacheKeyInfo(jsonValue: json["id"])
}

let object: JSONObject = [
"__typename": "Omega",
"id": false
]

let objectDict = NetworkResponseExecutionSource().opaqueObjectDataWrapper(for: object)
let actual = MockSchemaMetadata.cacheKey(for: objectDict)

expect(actual).to(beNil())
}

func test__multipleSchemaConfigurations_withDifferentCacheKeyProvidersDefinedInExtensions_shouldReturnDifferentCacheKeys() {
let object: JSONObject = [
"__typename": "MockSchemaObject",
Expand Down
23 changes: 13 additions & 10 deletions apollo-ios/Sources/ApolloAPI/ObjectData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public struct ObjectData {
public let _transformer: any _ObjectData_Transformer
public let _rawData: [String: AnyHashable]

public static let _boolTrue = AnyHashable(true)
public static let _boolFalse = AnyHashable(false)
calvincestari marked this conversation as resolved.
Show resolved Hide resolved

public init(
_transformer: any _ObjectData_Transformer,
_rawData: [String: AnyHashable]
Expand All @@ -22,18 +25,18 @@ public struct ObjectData {
@inlinable public subscript(_ key: String) -> (any ScalarType)? {
guard let rawValue = _rawData[key] else { return nil }
var value: AnyHashable = rawValue
// Attempting cast to `Int` to ensure we always use `Int` vs `Int32` or `Int64` for consistency and ScalarType casting,
// also need to attempt `Bool` cast first to ensure a bool doesn't get inadvertently converted to `Int`
switch value {
case let boolVal as Bool:

// This check is based on AnyHashable using a canonical representation of the type-erased value so
// instances wrapping the same value of any type compare as equal. Therefore while Int(1) and Int(0)
// might be representable as Bool they will never equal Bool(true) nor Bool(false).
if let boolVal = value as? Bool, (value == Self._boolTrue || value == Self._boolFalse) {
value = boolVal
case let intVal as Int:
value = intVal
default:
break

// Cast to `Int` to ensure we always use `Int` vs `Int32` or `Int64` for consistency and ScalarType casting
} else if let intValue = value as? Int {
value = intValue
}

return _transformer.transform(value)
}

Expand Down
Loading