Skip to content

Commit

Permalink
fix: ObjectData type check (apollographql/apollo-ios-dev#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
calvincestari authored and gh-action-runner committed Aug 13, 2024
1 parent 65d7a19 commit 1da33d3
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions 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]

@usableFromInline internal static let _boolTrue = AnyHashable(true)
@usableFromInline internal static let _boolFalse = AnyHashable(false)

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

0 comments on commit 1da33d3

Please sign in to comment.